Welcome to "Develop code using GitHub Codespaces and Visual Studio Code"! 👋
What's the big deal about using a GitHub Codespace for software development: A codespace is a development environment that's hosted in the cloud. You can customize your project for GitHub Codespaces by committing configuration files to your repository (often known as Configuration-as-Code), which creates a repeatable codespace configuration for all users of your project. Each codespace you create is hosted by GitHub in a Docker container that runs on a virtual machine. You can choose the type of machine you want to use depending on the resources you need.
GitHub offers a range of features to help your development team customize a codespace to reach peak configuration and performance needs. Some features of GitHub Codespaces are:
- Create a codespace from your repository
- Push code from the codespace to your repository
- Use VS Code to develop code
- Customize the codespace with custom images
- Manage the codespace
To begin developing using GitHub Codespaces, you can create a codespace from a template or from any branch or commit in a repository. When you create a codespace from a template, you can start from a blank template or choose a template suitable for the work you're doing.
We recommend opening another browser tab to work through the following activities so you can keep these instructions open for reference.
- Start from the landing page of your repository.
- Click the green
Code
button located in the middle of the page. - Select the
Codespaces
tab on the box that pops up and click theCreate codespaces on main
button. Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background - Verify your codespace is running. The browser should contain a VS code web based editor and a terminal should be present such as the below:
- From inside the codespace in the VS Code explorer window select the
index.html
file. - Replace the h1 header with the below:
<h1>Hello from the codespace!</h1>
- Save the file. Note: The file should autosave.
- Commit the file change by using the VS Code terminal by entering the following commit message:
git commit -a -m "Adding hello from the codespace!"
- Push the changes back to your repository. From the VS Code terminal enter:
git push
- Your code has been pushed to your repository!
- Switch back to the homepage of your repository and view the
index.html
to verify the new code was pushed to your repository.
Wait about 20 seconds then refresh this page for the next step
Nice work! 🎉 You created your first GitHub Codespace and pushed codes using VS Code!
You can configure the dev container for a repository so that any codespace created for that repository will give you a tailored development environment, complete with all the tools and runtimes you need to work on a specific project.
What are Development containers?: Development containers, or dev containers, are Docker containers that are specifically configured to provide a fully featured development environment. Whenever you work in a codespace, you are using a dev container on a virtual machine.
A dev container file is a JSON file that lets you customize the default image that runs your codespace, VS code settings, run custom code, forward ports and much more!
Let's add a devcontainer.json
file and add a custom image.
- Navigating back to your Code tab of your repository, click the Add file dropdown button.
- Click
Create new file
. - Type or paste the folling in the empty text field prompt to name your file.
.devcontainer/devcontainer.json
- In the body of the new .devcontainer/devcontainer.json file, add the following content:
{
// Name this configuration
"name": "Codespace for Skills!",
//Use base codespace image then pull Valet on postCreateCommand,
"image": "mcr.microsoft.com/vscode/devcontainers/universal:latest",
"remoteUser": "codespace",
"overrideCommand": false
}
- Click Commit changes directly to the
main
branch. - Create a new codespace by navigating back to the Code tab of your repository.
- Click the green
Code
button located in the middle of the page. - Click the
Codespaces
tab on the box that pops up. - Click the
+
sign to create a new codespace on the main branch. (notice your other codespace listed) Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background - Verify that your new codespace is is running as you did above.
Note the image being used is the default image provided for GitHub Codespaces. It includes runtimes and tools for Python, Node.js, Docker, and more. See the full list here: https://aka.ms/ghcs-default-image but any custom image can be used by your development team that has the required prerequisites installed on their image for more see: codespace image for more details.
Wait about 20 seconds then refresh this page for the next step
Nice work! 🎉 You created a codespace with a custom image!
You can customize GitHub Codespaces by adding VS code extensions, adding features, setting host requirements, and much more when the codespace is created.
Let's customize some setting in the .devcontainer.json file!
- Navigate to the
.devcontainer/devcontainer.json
file. - Add the following customizations to the body of the file before the last
}
.
,
// Add the IDs of extensions you want installed when the container is created.
"customizations": {
"vscode": {
"extensions": [
"GitHub.copilot"
]
},
"codespaces": {
"openFiles": [
"codespace.md"
]
}
}
- Click Commit changes directly to the main branch.
- Create a new Codespace by navigating to the landing page of your repository.
- Click the
Code
button located in the middle of the page. - Click the
Codespaces
tab on the box that pops up. - Click the
Create codespaces on main
button OR click the+
sign on the tab. This will create a new Codespace on the main branch. Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background - Verify your codespace is is running as you did above.
- The
codespace.md
file should show up in the VS code editor. - The
copilot
extension should show up in the VS code extension list.
This will add a VS code extension as well as open a file on start up of the codespace.
Next lets add some code to run on creation of the Codespace!
- Edit the
.devcontainer/devcontainer.json
file. - Add the following postCreateCommand to the body of the file before the last
}
.
,
"postCreateCommand": "echo '# Writing code on Codespace Creation!' >> codespace.md"
- Click Commit changes directly to the main branch.
- Create a new Codespace by navigating to the landing page of your repository.
- Click the
Code
button located in the middle of the page. - Click the
Codespaces
tab on the box that pops up. - Click the
Create codespaces on main
button OR click the+
sign on the tab. This will create a new Codespace on the main branch. Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background - Verify your codespace is is running as you did above.
- Verify the
codespace.md
file now has the textWriting code on Codespace Creation!
.
Wait about 20 seconds then refresh this page for the next step.
Nicely done customizing your Codespace! 🥳
When using any development environment customizing the settings and tools to your preferences and workflows is an important step. GitHub Codespaces allows for two main ways of personalizing your codespaces which are Settings Sync
with VS Code and dot files
.
Dot files
will be the focus of this activity.
What are dot files
?: Dotfiles are files and folders on Unix-like systems starting with . that control the configuration of applications and shells on your system. You can store and manage your dotfiles in a repository on GitHub.
Let's see how this works!
- Start from the landing page of your repository.
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the
Code, planning, and automation
section of the sidebar, click Codespaces. - Under
Dotfiles
, selectAutomatically install dotfiles
so that GitHub Codespaces automatically installs your dotfiles into every new codespace you create. - Choose YOUR current skills working repository as the repository install dotfiles from.
-
Start from the landing page of your repository.
-
Click the
Code
button located in the middle of the page. -
Click the
Codespaces
tab on the box that pops up. -
Click the
Create codespaces on main
button. Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background -
Verify your codespace is is running. The browser should contain a VS code web based editor a a terminal should be present such as the below:
-
From inside the Codespace in the VS Code explorer window create a new file
setup.sh
. -
Add the following code inside of the file:
#!/bin/bash
sudo apt-get update
sudo apt-get install sl
- Save the file. Note: The file should autosave.
- Commit the file changes. From the VS Code terminal enter:
git add setup.sh --chmod=+x
git commit -m "Adding setup.sh from the codespace!"
- Push the changes back to your repository. From the VS Code terminal enter:
git push
- Switch back to the homepage of your repository and view the
setup.sh
to verify the new code was pushed to your repository. - Close the Codespace web browser tab.
- Click the
Create codespaces on main
button OR click the+
sign on the tab. This will create a new Codespace on the main branch. Wait about 2 minutes for the codespace to spin itself up. Note, its a VM spinning up in the background - Verify your codespace is is running as you did above.
- Verify the
setup.sh
file is present in your VS code editor. - From the VS Code terminal type or paste:
/usr/games/sl
- Enjoy the show!
Wait about 20 seconds then refresh this page for the next step.
Congratulations friend, you've completed this course!
Here's a recap of all the tasks you've accomplished in your repository:
- You've learned how to create a Codespace and push code to your repository from the Codespace.
- You've learned how to use custom images in your Codespace.
- You've learned how to customize your Codespace.
- You've learned how to persoalize your Codespace.
- Developing in a codespace. Learn how to delete, open an existing, connect to a private network, forward ports, and much more inside a Codespace
- Set up your repository. Learn how to set minimum machine spces, add badges, set up a template repo, and much more inside a Codespace.
- Personalize and Customize Codespaces. Learn how to use setting sync, dotfiles, set the default region, set the default editor, and much for your Codespace.
- Prebuild your Codespace
- Manage your Codespace
- Learn more about securing your supply chain by reading: GitHub Codespaces overview.
- We'd love to hear what you thought of this course.
- Learn another GitHub skill.
- Read the Get started with GitHub docs.
- To find projects to contribute to, check out GitHub Explore.
Get help: Review the GitHub status page
© 2022 TBD-copyright-holder • Code of Conduct • CC-BY-4.0 License