ttc-cases / pydevx-lindjacob

1 stars 0 forks source link

Create a python devcontainer #1

Closed github-actions[bot] closed 2 months ago

github-actions[bot] commented 2 months ago

[!NOTE] ☝️ Learning goals in this issue

  • Install VS Code and Docker Desktop, if you haven't already
  • Open the project in VS Code
  • Add a devcontainer to use for Python programming
  • Reopen your VC Code in the devcontainer

You need a devcontainer - A devcontainer is an environment which is configured to be exactly the same for everyone who contributes to the repo. You can think of it as a virtual machine but in fact it is a Docker container - which is not quite the same, but conceptually they are close enough for you not to care about the difference at this point.

With a devcontainer you don't have to bother about some team members being on Mac and others on Windows - you're all on Linux! And you can set the development up so that everything is configured in the repo, which means that every team member will work in identical environments - no more environments creeps.

🏋️‍♀️ Prerequsite

The following three installations are required on your own PC - other than that there are no requirements to your PC. It can be Mac, Linux or Windows. And even if we're developing Python code. Python is not even installed on your PC.

The remainder of this tutorial assumes you have all three tools installed on your local PC.

A note for Windows users in a corporate setup It's common in corporate setups that even developers do not have admin rights on their own PCs. This is only just another argument why devcontainers are som much more delicious from a DX perspective. But installing these tools may impose a challenge. Try: - Look for them in the Intune company store, if your IT department approved them, they'll be there. - Or you can perhaps get _Admin by request_. - Or contact your IT department at get help.
Another note on line-endings for Windows users in general There's _one_ setting you need to to on your PC after you have installed git and before you start cloning stuff from GitHub. You need to make sure line-endings are handled correct when non-windows environment are added to the equation. From the git manual on ["Formatting and Whitespaces"](https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_formatting_and_whitespace): >... >Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the `core.autocrlf` setting. If you’re on a Windows machine, set it to `true` — this converts LF endings into CRLF when you check out code: > >```shell >git config --global core.autocrlf true >``` > >Run that command from your PowerShell - now your're good to go.

🏋️‍♀️ Exercise

  • [x] 👉 Open your repo in a devcontainer running in Docker on your own PC👈

Open VS Code on your PC

[!TIP] It's nickname is code

From VS Code: Clone the GitHub repository you just created to your PC.

[!TIP] Where to store it? When you clone a repo you will per default create a folder of the same name as the repo on GitHub, but you might want to create some folder structure before that. I usually create a structure in my user ( or home) folder that represents the server at the first level: github|gitlab|azure|gitea|... and then the user or organization on the next level: lakruzz|thetechcollective|... Example:

~ (home)
├────GitHub/            
│    ├───thetechcollective/
│    │   ├───py-devx-intro/ 
│    │   └───thetechcollective.dev/
│    ├───lakruzz/
│    │   ├───codememo/ 
│    │   └───jekyll-memo/
├────GitLab/            
│    ├───denvercoder/
│    │   ├───petshop/ 
...

This way your folder structure on your PC resembles the folder structure on the various server.

At this point the terminal in your IDE is your local PC, you haven't yet created a devcontainer. Let's do that:

From the command palette choose:

>Dev Containers: Add dev Containers Configuration Files...

and then

Add configuration to workspace

Now you will have to choose which flavor to use. Let's setup a Python specific image:

Show all Definitions

Lookup Ubuntu choose version noble (24.04)

Next up you'll have to add som features - use the search option to install

Now hit "OK" and Keep Defaults.

The process will create the files:

.devcontainer/devcontainer.json
.github/dependabot.yml

And you'll see a pop-up that suggests that you reopen the folder to develop in container.

Do that!

[!TIP] If you missed the pop-up you can reopen from the Command Palette

>Dev Containers: Rebuild And Reopen In Container

What happens next is that the Docker container will be started on your PC and your repo mounted into it. The terminal is now running in your docker container - it's Linux!

We're done with this issue - time to wrap up. Now run:

git add -A
git commit -m "Created a devcontainer - Close #1"
git push origin main

🏋️‍♀️ Exercise

  • [x] 👉 Browse the issue on GitHub by running the gh browse <number> command 👈

GitHub has some nice features that are only available on the web interface. An exceptionally nice one is that all mentions to basically anything is available as double-pointed links everywhere.

So sometimes you want to open a web browser on GitHub rather than stying in VS Code.

From the terminal try to run gh browse (see the full documentation) like this:

gh browse 1

...eeeh - you are inside a container and you need to authenticate yourself first so try this (see the full documentation):

gh auth login -h github.com -p https -w

Try the first command again:

gh browse 1

Do you see the reference in the issue to the commit?

Click the link and go to the commit - do you see the link back to the issue?

"...neat eh?"