Tech Hub

@ Solution Architecture Works

Automate Your Workflow with GitHub Actions – Part 2 of 2

Publish to GitHub Packages and GitHub Container Registry

Estimated reading: 4 minutes 6 views

In this section, you will learn the basics of using a workflow to publish to GitHub Packages, as well as the steps required to build, authenticate, tag, and push a Docker image to the GitHub Container Registry.

Use a Workflow to Publish to GitHub Packages

GitHub Packages allows you to securely publish and consume packages, store your packages alongside your code, and share them privately with your team or publicly with the open-source community. You can also use GitHub Actions to automate the management of your packages.

Here is an example of a basic workflow that runs whenever a new release is created in a repository. If the tests pass, the package is then published to GitHub Packages.

name: Node.js Package

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 12
      - run: npm ci
      - run: npm test

  publish-gpr:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
      - run: npm ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

Publish to GitHub Packages and GitHub Container Registry

First, the workflow file must be located in the .github/workflows directory. It is common to name a workflow that publishes a new package whenever a release is created something like release-package.yml, so that project collaborators can easily understand its purpose without opening the file.

The previous workflow performs several actions after a new release is created:

  • A job named build runs npm ci (where “ci” stands for Continuous Integration) to install dependencies directly from the package-lock.json file, as well as the project tests.
  • Once the build job succeeds, the job named publish-gpr publishes the package.
  • The workflow publishes the package to the registry URL: https://npm.pkg.github.com/ using an access token for authentication.

Use GitHub Container Registry to Host and Manage Docker Container Images

GitHub Packages supports the use of containers, Kubernetes, and other cloud-native technologies to manage the entire application lifecycle, including production operations, development, publishing, and deployment. GitHub Packages also offers a container registry designed to meet the specific needs of container images.

You can use GitHub Container Registry to easily host and manage Docker images within your GitHub organization or personal user account. This registry allows you to configure precise permissions to manage and access packages.

With the container registry, you can:

  • Store container images in your organization or user account, rather than in a repository.
  • Define fine-grained permissions for container images.
  • Access public container images anonymously.

Once you have built the image, authenticated, and logged in to the GitHub Container Registry service at ghcr.io, you can tag and push the latest version of the image to the registry using the following commands:

echo $GITHUB_TOKEN | docker login ghcr.io -u USERNAME --password-stdin

docker tag IMAGE_ID ghcr.io/OWNER/IMAGE_NAME:latest

docker push ghcr.io/OWNER/IMAGE_NAME:latest

Note

To authenticate via a GitHub Actions workflow, you can use a GITHUB_TOKEN:

  • For the container registry at ghcr.io/OWNER/IMAGE-NAME.
  • For package registries at PACKAGE-REGISTRY.pkg.github.com.
Share this Doc

Publish to GitHub Packages and GitHub Container Registry

Or copy link

CONTENTS