Skip to content
/ publish-packages Public template
generated from skills/template-template

Use GitHub Actions to publish your project to a Docker image.

License

Notifications You must be signed in to change notification settings

skills/publish-packages

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Actions: Publish to GitHub Packages

This course will walk you through using GitHub Actions to publish your project to a Docker image.

⛳ Start

start-course

When you select Start course or Use this template, you will be prompted to create a new repository. We recommend creating a public repository, as private repositories will use Actions minutes.
After you make your own repository, wait about 20 seconds and refresh. I will go to the next step.

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.

📝 Step 1: Create the workflow file

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, dependancies, 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.

⌨️ Activity: Create the workflow file

  1. Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab.
  2. Open the pull request I made for you from the cd branch.
  3. Add a new file at .github/workflows/publish.yml.
  4. 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@v2
          # 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
  5. Replace YOURNAME with your username.
  6. Commit your changes.
  7. Wait about 20 seconds then refresh this page for the next step.

🚢 Step 2: Add a Dockerfile

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.

⌨️ Activity: Add a Dockerfile

  1. In the cd branch, create Dockerfile at the project root and include:
    FROM nginx:1.17
    COPY . /usr/share/nginx/html
  2. Commit your changes.
  3. Wait about 20 seconds then refresh this page for the next step.

🐿️ Step 3: Merge your pull request

Let's get publishing! ❤️

You can now merge your pull request!

⌨️ Activity: Merge your pull request

  1. Click Merge pull request.
  2. Delete the branch cd (optional).
  3. Wait about 20 seconds then refresh this page for the next step.

🚣 Step 4: Pull your image

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

screenshot personal access token creation page with boxes for repo (all), write:packages, and read:packages checked

We will use this token to log in to Docker, and authenticate with the package.

  1. Open your terminal (Bash or Git Bash recommended)
  2. Use the following command to log in:
    docker login docker.pkg.github.com -u USERNAME -p TOKEN
    
  3. Replace USERNAME with your GitHub username
  4. Replace TOKEN with the Personal Access Token you just created
  5. Press Enter

If everything went well, 🤞 you should see Login Succeeded in your terminal.

⌨️ Activity: Pull your image

  1. Copy and paste the pull command from the package instructions into your terminal. It should look something like this:
    • docker pull docker.pkg.github.com/YOURNAME/js-build/tic-tac-toe:f29 screenshot of the pull command on the GitHub package page
  2. Press Enter.
  3. You should see output indicating that the pull was successful, like Status: Downloaded newer image for docker. screenshot of successful Docker image output
  4. We can't automatically verify this step for you, so please continue on to the next step below!

🏃 Step 5: Run your image

Nicely done grabbing your Docker image! ☺️

Let's trying running it.

⌨️ Activity: Run your image

  1. Find your image information by typing Docker image ls. screenshot of output from Docker image ls command: lists docker images, REPOSITORY TAG and docker URL
  2. 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>
    
  3. Replace YOUR_IMAGE_NAME with your image name under the REPOSITORY column.
  4. Replace TAG with the image tag under the TAG column example of running the docker command listed above
  5. Press Enter.
  6. If everything went well, you will see hash value as output on your screen.
  7. We can't automatically verify this step for you, so please continue on to the next step below!

🏁 Finish

Congratulations friend, you've completed this course!

celebrate

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!

What's next?


Get help: Post in our community forumReview the GitHub status page

© 2022 GitHub • Code of ConductCC-BY-4.0 License