Secrets > Actions Second," /> Secrets > Actions Second," />

Docker – Build Image on GITHub And Push To Private Registry

Hosting your own registry is one thing, but deploying a “runner” or similar (including all of the associated infrastructure) may be overkill for your project. Luckily, GITHub (Mirco$oft) has you covered.

All you need is:

  • A project with Dockerfile
  • Hosted on GITHub in a repo
  • A Docker registry (private or DockerHUB account is fine)

First, In your GITHub repo. Click Settings > Secrets > Actions

Second, Click “New Repository Secret” and add two key/value secrets. Preferably something like:

DOCKER_USER
DOCKER_PASS

Third, Click actions and then “New Workflow”

Fourth, Choose “set up a workflow yourself” and use my template below:

name: Continuous Integration

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Dockerreg.tld
        uses: docker/login-action@v2
        with:
          registry: dockerred.tld
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASS }}

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: |
            dockerreg.tld/projectname:latest
            dockerreg.tld/projectname:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Remember to replace “dockerreg.tld” with your Docker registry URL and “projectname” with your own preferred Docker Image name.

Once you save, a “Build and Push” should kick off.

It will also Build and Push after every commit and merge to “main”

The Docker Image will be available under tag “latest” and the GITHub SHA sum as another tag.

Continuously Integrate me 👾

Leave a Reply

Your email address will not be published. Required fields are marked *