GitHub Actions to automatically build Docker and push to registry

Configuration to have GitHub automatically build and push Docker containers

GitHub Actions to automatically build Docker and push to registry
Page content

Introduction

GitHub provies “Actions” which are automatic actions that are ran on gitHub for you. This can be used to compile your code, compile your docker image, or do any arbitrary set of commands. This typically is done in software development to automatically push software between development and test. This is typically done to insure all the necessary code, configurations, etc necessary are checked into the source repository. Additionally this insures all the neccessary steps are done uniformly no matter which developer checks in the code.

Use Case

When code is commited automatically build a continer, and push the container to a Docker Registry.

GitHub Action Code

In the source repository create a file “.github/workflows/push-github-to-registry.yaml”

  • replace NAMEOFYOURIMAGE with the name of the image you are pushing.
name: push-github-to-registry
run-name: ${{ github.actor }} is publishing Registry
on: [push]
jobs:
  publish-to-registry:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: |
          export TAG=latest
          export IMAGE=NAMEOFYOURIMAGE
          echo  ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin ${{ secrets.DOCKER_SERVER }}
          docker build -t ${{ secrets.DOCKER_SERVER }}/${{ secrets.DOCKER_USERNAME }}/"${IMAGE}":"${TAG}" .
          docker push ${{ secrets.DOCKER_SERVER }}/${{ secrets.DOCKER_USERNAME }}/"${IMAGE}":"${TAG}"          

GitHub Secret Token

Create Actions Secrets in the git repository.

  1. DOCKER_USERNAME is the username for your repository.
  2. DOCKER_PASSWORD is the password for the repository
  3. DOCKER_SERVER is the repository. i.e. docker.io/yourname

secret

GitHub Actions Demonstration

Simply check something into the source repository, and click “Actions” and you can watch the actions run and inspect the output of the code when it runs.

demo