|
| 1 | +## Step 3: Add Features |
| 2 | + |
| 3 | +You can further customize your codespace by adding container feature, VS Code extensions, VS Code settings, host requirements, and much more. |
| 4 | + |
| 5 | +Let's add the GitHub CLI, extensions to run the python program using VS Code, and a custom script to install some packages when first creating the Codespace. |
| 6 | + |
| 7 | +### ⌨️ Activity: Override the Python Version |
| 8 | + |
| 9 | +1. In VS Code, open the Command Palette (`CTRL`+`SHIFT`+`P`) and select the below command. |
| 10 | + |
| 11 | + ```txt |
| 12 | + Codespaces: Add Dev Container Configuration Files... |
| 13 | + ``` |
| 14 | + |
| 15 | + <img width="350" alt="vs code configure dev container command" src="https://github.com/user-attachments/assets/38265419-47bf-4ac8-a0fd-71ea67e2d6eb" /> |
| 16 | + |
| 17 | +1. Select the option `Modify your active configuration...`. |
| 18 | + |
| 19 | +1. In the list of features, search for and select `Python`. Instead of the defaults, pick `Configure Options`. When it asks for he Python version, select `3.10`. |
| 20 | + |
| 21 | + <!-- <img width="350" alt="select the github cli feature" src="https://github.com/user-attachments/assets/483b53ef-908d-4160-81e6-28fb977423a4" /> --> |
| 22 | + |
| 23 | +1. Navigate to and open the `.devcontainer/devcontainer.json` file. |
| 24 | + |
| 25 | +1. Verify a new entry similar to the below was added. |
| 26 | + |
| 27 | + ```json |
| 28 | + "features": { |
| 29 | + "ghcr.io/devcontainers/features/python:1": { |
| 30 | + "installTools": true, |
| 31 | + "version": "3.10" |
| 32 | + } |
| 33 | + }, |
| 34 | + ``` |
| 35 | + |
| 36 | +### ⌨️ Activity: Add VS Code extensions |
| 37 | + |
| 38 | +1. In the left navigation, select the **Extension** tab. |
| 39 | + |
| 40 | + <img width="200" alt="vs code extensions tab" src="https://github.com/user-attachments/assets/d7941711-e5a9-4871-83f3-c723c203bc70" /> |
| 41 | + |
| 42 | +1. Search for `python` and find entries for `Python` and `Python Debugger`. |
| 43 | + |
| 44 | + <img width="250" alt="python extensions for vs code" src="https://github.com/user-attachments/assets/b34ef808-2023-41f2-8963-85ba04d5684b" /> |
| 45 | + |
| 46 | +1. Right click on each entry and select the `Add to devcontainer.json` option. |
| 47 | + |
| 48 | + <img width="250" alt="add to devcontainer config button" src="https://github.com/user-attachments/assets/30ada390-c8a7-4175-9d83-dfa17fc83de3" /> |
| 49 | + |
| 50 | +1. Navigate to and open the `.devcontainer/devcontainer.json` file. |
| 51 | + |
| 52 | +1. Verify a new entry similar to the below was added. |
| 53 | + |
| 54 | + ```json |
| 55 | + "customizations": { |
| 56 | + "vscode": { |
| 57 | + "extensions": [ |
| 58 | + "ms-python.python", |
| 59 | + "ms-python.debugpy" |
| 60 | + ] |
| 61 | + } |
| 62 | + }, |
| 63 | + ``` |
| 64 | + |
| 65 | +### ⌨️ Activity: Add a custom script |
| 66 | + |
| 67 | +The Dev Container specification provides multiple locations to run [lifecycle scripts](https://containers.dev/implementors/json_reference/#lifecycle-scripts) to further customize your Codespace. Let's add the `postCreateCommand` which runs one time after initial build (or rebuild). |
| 68 | + |
| 69 | +1. Use the VS Code file explorer to create a script file with the below name. |
| 70 | + |
| 71 | + ```txt |
| 72 | + .devcontainer/postCreate.sh |
| 73 | + ``` |
| 74 | + |
| 75 | + Alternately, run the below terminal command to create it. |
| 76 | + |
| 77 | + ```bash |
| 78 | + touch .devcontainer/postCreate.sh |
| 79 | + ``` |
| 80 | + |
| 81 | +1. Open the `.devcontainer/postCreate.sh` file and add the following code, which will install an animation of a steam locomotive. |
| 82 | + |
| 83 | + ```bash |
| 84 | + #!/bin/bash |
| 85 | + |
| 86 | + sudo apt-get update |
| 87 | + sudo apt-get install sl |
| 88 | + echo "export PATH=\$PATH:/usr/games" >> ~/.bashrc |
| 89 | + echo "export PATH=\$PATH:/usr/games" >> ~/.zshrc |
| 90 | + ``` |
| 91 | + |
| 92 | +1. Navigate to and open the `.devcontainer/devcontainer.json` file. |
| 93 | + |
| 94 | +1. Create the `postCreateCommand` entry at the same level (_top level_) as `features`, and `customizations`. |
| 95 | + |
| 96 | + ```json |
| 97 | + "postCreateCommand": "bash .devcontainer/postCreate.sh" |
| 98 | + ``` |
| 99 | + |
| 100 | +1. With our new configuration finished, let's commit the changes. Use VS Code's source control tools or the below terminal command. |
| 101 | + |
| 102 | + ```shell |
| 103 | + git add '.devcontainer/devcontainer.json' |
| 104 | + git add '.devcontainer/postCreate.sh' |
| 105 | + git commit -m 'feat: Add features, extensions, and postCreate script' |
| 106 | + git push |
| 107 | + ``` |
| 108 | + |
| 109 | +1. Open the VS Code Command Palette (`CTRL`+`Shift`+`P`) and run the `Codespaces: Rebuild Container` command. Select the **Rebuild** option. A full build is not necessary. |
| 110 | + |
| 111 | + <img width="350" alt="rebuild codespace command" src="https://github.com/user-attachments/assets/2b72e1a7-68c4-4c8d-8bf1-5727a520fd0e"/> |
| 112 | + |
| 113 | +1. Wait a few minutes for the Codespace to rebuild and VS Code to reconnect. |
| 114 | + |
| 115 | +1. With the customizations committed, Mona will begin checking your work. Give her a moment to provide feedback and the next learning steps. |
| 116 | + |
| 117 | +> [!TIP] |
| 118 | +> You can also configure your account to [install dotfiles](https://docs.github.com/en/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account), allowing you to combine personal configurations with the project's configuration. |
| 119 | +
|
| 120 | +### ⌨️ Activity: (optional) Verify our customizations |
| 121 | + |
| 122 | +Now that you've rebuilt the codespace, let's verify the python extension, python version, and custom script were adjusted correctly in the Codespace. |
| 123 | + |
| 124 | +1. Ensure you are in the Codespace. |
| 125 | + |
| 126 | +1. In the left sidebar, click the extensions tab and verify that the Python extensions are installed and enabled. |
| 127 | + |
| 128 | + <img width="250" alt="python extensions for vs code" src="https://github.com/user-attachments/assets/b34ef808-2023-41f2-8963-85ba04d5684b" /> |
| 129 | + |
| 130 | +1. In the left sidebar, select **Run and Debug** tab and then press the **Start Debugging** icon. VS Code will open the lower panel and show the run logs. |
| 131 | + |
| 132 | + <img width="250" alt="run and debug tab pointing to start button" src="https://github.com/user-attachments/assets/63327b56-b033-4ca1-aa83-93ec076389f5"/> |
| 133 | + |
| 134 | +1. In the lower panel, switch to the **TERMINAL** tab. |
| 135 | + |
| 136 | +1. Run the following command to show the version of the installed GitHub CLI and Python |
| 137 | + |
| 138 | + ```bash |
| 139 | + node --version |
| 140 | + dotnet --version |
| 141 | + python --version |
| 142 | + gh --version |
| 143 | + ``` |
| 144 | + |
| 145 | + > Notice the Python version is different but the others are the same. Adding the feature overwrote the version provided by the universal image. |
| 146 | +
|
| 147 | +1. Run the following command to show the steam locomotive animation. |
| 148 | + |
| 149 | + ```bash |
| 150 | + sl |
| 151 | + ``` |
0 commit comments