/publish-packages-check

skills/publish-packages#20

Primary LanguageHTMLMIT LicenseMIT

Publish to GitHub Packages

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

Welcome

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.

How to start this course

  1. Right-click Start course and open the link in a new tab.
    start-course
  2. 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. Create a new repository
  3. 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.

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

⌨️ 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. Navigate to the Code tab.
  3. From the main branch dropdown, click on the cd branch.
  4. Navigate to the .github/workflows/ folder, then select Add file and click on Create new file.
  5. In the Name your file... field, enter publish.yml.
  6. 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 }}
  7. Replace YOURNAME with your username.
  8. Commit your changes.
  9. (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 and compare:cd.
  10. 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 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.

⌨️ 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 changes

Let's get publishing! ❤️

You can now merge your changes!

⌨️ Activity: Merge your changes

  1. Merge your changes from cd into main. 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.
  2. (optional) Delete the branch cd.
  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 ghcr.io -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 ghcr.io/YOURNAME/publish-packages/game:TAG screenshot of the pull command on the GitHub package page
    • Tip: To reach this page, click the Code tab at the top of your repository. Then, find the navigation bar below the repository description, and click the Packages heading link
  2. Replace YOURNAME with your GitHub username.
  3. Replace TAG with the image tag.
  4. Press Enter.
  5. You should see output indicating that the pull was successful, like Status: Downloaded newer image for ghcr.io/YOURNAME/publish-packages/game:TAG. screenshot of successful Docker image output
  6. 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 --rm <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 discussion boardReview the GitHub status page

© 2022 GitHub • Code of ConductMIT License