Use GitHub Actions to publish your project to a Docker image.
GitHub Actions makes it easier than ever to incorporate continuous delivery (CD) into your repositories. This course will teach you what is needed to test and deliver artifacts that are ready for deployment.
- Who is this for: Developers, DevOps engineers, full stack developers, cloud engineers.
- What you'll learn: Continuous delivery, how to save and access build artifacts, package management, how to publish to GitHub Packages.
- What you'll build: We will build a Docker image that runs a small game.
- Prerequisites: We recommend you first complete the following courses: Hello, GitHub Actions and Continuous Integration.
- How long: This course is five steps long and takes less than 30 minutes to complete.
- Right-click Start course and open the link in a new tab.
- In the new tab, follow the prompts to create a new repository.
- For owner, choose your personal account or an organization to host the repository.
- We recommend creating a public repository—private repositories will use Actions minutes.
- After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README.
Welcome to "Publish packages"! 👋
First, take a moment to examine the image below. It shows the relationship between continuous integration, continuous delivery and continuous deployment.
Continuous integration (CI) is a practice where developers integrate tested code into a shared branch several times per day. Continuous delivery (CD) is the next phase of continuous integration (CI), where we deploy our changes to the world.
Docker is an engine that allows you to run containers. Containers are packages of software that can run reliably in different environments. Containers include everything needed to run the application. Containers are lightweight in comparison to virtual machines. A Dockerfile is a text document that contains all the commands and instructions necessary to build a Docker Image. A Docker image is an executable package comprised of code, dependencies, libraries, a runtime, environment variables, and configuration files. A Docker container is a runtime instance of a Docker Image.
We'll start by creating the workflow file to publish a Docker image to GitHub Packages.
- Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab.
- Navigate to the Code tab.
- From the main branch dropdown, click on the cd branch.
- Navigate to the
.github/workflows/
folder, then select Add file and click on Create new file. - In the Name your file... field, enter
publish.yml
. - Add the following to the
publish.yml
file:name: Publish to Docker on: push: branches: - main permissions: packages: write jobs: publish: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 # Add your test steps here if needed... - name: Docker meta id: meta uses: docker/metadata-action@v4 with: images: ghcr.io/YOURNAME/publish-packages/game tags: type=sha - name: Login to GHCR uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build container uses: docker/build-push-action@v4 with: context: . push: true tags: ${{ steps.meta.outputs.tags }}
- Replace
YOURNAME
with your username. - Commit your changes.
- (optional) Create a pull request to view all the changes you'll make throughout this course. Click the Pull Requests tab, click New pull request, set
base: main
andcompare:cd
. - Wait about 20 seconds then refresh this page for the next step.
You created a publishing workflow! 🎉
We will add a Dockerfile
to the cd
branch. The Dockerfile
contains a set of instructions that get stored in a Docker Image
. If you'd like, you can learn more about Dockerfiles.
- In the
cd
branch, createDockerfile
at the project root and include:FROM nginx:1.17 COPY . /usr/share/nginx/html
- Commit your changes.
- Wait about 20 seconds then refresh this page for the next step.
Let's get publishing! ❤️
You can now merge your changes!
- Merge your changes from
cd
intomain
. If you created the pull request in step 1, just open that PR and click on Merge pull request. If you did not create the pull request earlier, you can do it now by following the instructions in step 1. - (optional) Delete the branch
my-pages
. - Wait about 20 seconds then refresh this page for the next step.
Now things are running! ✨
Whoa, now things are running! This may take a few minutes. This might take a tiny amount of time, so grab your popcorn 🍿 and wait for the build to finish before moving on.
To pull the Docker image, we need to log into Docker first.
Before we can use this Docker image, you will need to generate a personal access token that contains the following permissions:
- repo (all)
- write:packages
- read:packages
We will use this token to log in to Docker, and authenticate with the package.
- Open your terminal (Bash or Git Bash recommended)
- Use the following command to log in:
docker login ghcr.io -u USERNAME -p TOKEN
- Replace
USERNAME
with your GitHub username - Replace
TOKEN
with the Personal Access Token you just created - Press Enter
If everything went well, 🤞 you should see Login Succeeded
in your terminal.
- Copy and paste the
pull
command from the package instructions into your terminal. It should look something like this: - Replace
YOURNAME
with your GitHub username. - Replace
TAG
with the image tag. - Press Enter.
- You should see output indicating that the pull was successful, like
Status: Downloaded newer image for ghcr.io/YOURNAME/publish-packages/game:TAG
. - We can't automatically verify this step for you, so please continue on to the next step below!
Nicely done grabbing your Docker image!
Let's trying running it.
- Find your image information by typing
docker image ls
. - Use the following command to run a container from your image:
docker run -d --rm <YOUR_IMAGE_NAME:TAG>
- Replace
YOUR_IMAGE_NAME
with your image name under theREPOSITORY
column. - Replace
TAG
with the image tag under theTAG
column - Press Enter.
- If everything went well, you will see hash value as output on your screen.
- We can't automatically verify this step for you, so please continue on to the next step below!
Congratulations friend, you've completed this course!
Here's a recap of all the tasks you've accomplished in your repository:
- You wrote a workflow that sends a code through a continuous delivery pipeline.
- You built a fully deployable artifact.
- You did so using GitHub Actions and GitHub Packages!
- Publish your own packages from your projects.
- We'd love to hear what you thought of this course in our discussion board.
- Take another GitHub Skills course.
- Read the GitHub Getting Started docs.
- To find projects to contribute to, check out GitHub Explore.
Get help: Post in our discussion board • Review the GitHub status page
© 2022 GitHub • Code of Conduct • MIT License