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.
- Open the pull request I made for you from the
cd
branch. - Add a new file at
.github/workflows/publish.yml
. - Add the following to the
publish.yml
file:name: Publish to Docker on: push: branches: - main jobs: publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # Add your test steps here if needed... - name: Build container uses: docker/build-push-action@v1 with: username: YOURNAME password: ${{ secrets.GITHUB_TOKEN }} registry: docker.pkg.github.com repository: YOURNAME/publish-packages/game tag_with_sha: true
- Replace
YOURNAME
with your username. - Commit your changes.
- Wait about 20 seconds then refresh this page for the next step.
You created a publishing workflow! 🎉
We will add a Dockerfile
in this pull request. 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 pull request!
- Click Merge pull request.
- Delete the branch
cd
(optional). - 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 docker.pkg.github.com -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: - Press Enter.
- You should see output indicating that the pull was successful, like
Status: Downloaded newer image for docker
. - 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 -it --rm -p 8080:80 --name ttt <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 • CC-BY-4.0 License