Build and push docker images to ghcr.io with GitHub Actions

󰃭 2022-12-28

When you host your project code on GitHub and want to release it as a docker image for deployment or just publish it, the way to go are GitHub actions. Actions are basically hooks that can start CI/DC workflows on repository events.

GitHub actions can be used to build and push images to GitHub’s Container Registry which are reachable under https://ghcr.io which is part of the package registry. The package registry is not only for docker images, it can also host quite a few other kinds of packages. In this case we’ll focus on docker images.

Prerequisites:

  • GitHub Repository
  • Basic Knowledge about GitHub actions syntax
  • Dockerfile

The GitHub Workflow

I created a workflow in my repository under .github/workflow/cd.md and added the following:

name: Continuous Delivery
on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*.*.*'

jobs:
  build:
    name: Buid and push Docker image to GitHub Container registry
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v3

      - name: Docker Setup Buildx
        uses: docker/setup-buildx-action@v2.2.1

      - name: Docker Login
        uses: docker/login-action@v2.1.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker images
        uses: docker/build-push-action@v3.2.0
        env:
          REGISTRY: ghcr.io
          IMAGE_NAME: ${{ github.repository }}
        with:
          context: .
          file: ./Dockerfile
          target: final
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest

Let’s go through the important parts:

Permissions: Actions have access to the repo while running. We should always make sure by setting the permissions, that actions have the minimum access they require. See here: permissions for the github_token

Step 1–2: Checkout the code and Setup docker

Step 3: Login to GitHub Container Registry: This is where the interesting part starts. github.actor is the user that triggers the workflow. For password use secrets.GITHUB_TOKEN which is a temporary token which is automatically generated for this workflow. See here: publishing images to github-packages.

Step 4: Build and push Docker images: If the registry that you want to push to belongs to an organization then you will need to add permissions to create packages. If it lives under your own handle you don’t need to configure anything more since you are the owner already and the secrets.GITHUB_TOKEN has all the permissions granted.

The action will consume the Dockerfile and build the image up to the target build step that you can define. In docker the repository where the image will be hosted is also part of the tag. Setting the image name to the repository name will create an image with the following tag: ghcr.io/OWNER/IMAGE_NAME:latest

Read more here: pushing container images

Happy shipping \o/

Enter your instance's address


More posts like this

Mastering Multi-Process Containers: Running PHP Applications with s6-overlay

󰃭 2025-03-26 | #ci #devops #docker #s6-overlay

The Dockerized Development Setup

Containerization has completely changed how we build and deploy PHP applications. With Docker, you can make sure that your production environment behaves just like your local setup, which means fewer surprises when you go live.

In this post, we’re diving into running Symfony in a container that runs multiple processes using s6-overlay. We’ll explain why having more than one process in a container can be important, how this idea is different from Docker’s usual “one process per container” rule, and how s6-overlay makes it easier to run everything together.

Continue reading 


Manage s6-overlay setup with s6-cli

󰃭 2024-12-06 | #ci #devops #docker #s6-overlay

I developed a small cli in golang to ease creating, validating and documenting services that s6 supervises.

Usage

You do not need to install anything, just execute the binary via docker

docker run -it --rm hakindazz/s6-cli help
COMMANDS:
   create, c   create a service
   remove, rm  remove a service
   lint, l     lint directories and files
   mermaid, m  document s6 service dependencies in mermaid syntax
   help, h     Shows a list of commands or help for one command

Create a service with s6-cli

docker run -it --rm -v ./:/etc/s6-overlay hakindazz/s6-cli create oneshot init-dependencies

Here is the file / directory structure it creates:

Continue reading 