Push to Docker Hub using GitHub Actions


Knock-knock! Who’s there? Jenkins. Jenkins who? jk 🤣

Links

Create & clone repo

Create a GitHub repository.

Clone your repository and cd into it.

git clone git@github.com:itsmetommy/github-actions.git && cd github-actions

Create secrets

Settings → Secrets

Click New repository secret.

Create two secrets.

Create DOCKER_USERNAME.

Create DOCKER_PASSWORD.

View Actions secrets.

Create Dockerfile

Create public-html directory and index.html.

mkdir public-html; echo "Hello, GitHub Actions\!" > public-html/index.html

Create Dockerfile.

vi Dockerfile
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/
EXPOSE 80

Create workflow file

Create a .github/workflows directory.

mkdir -p .github/workflows && cd .github/workflows

The below workflow checks out the GitHub repository, uses the login-action to log in to the registry, uses the build-push-action action to build a Docker image based on your repository’s Dockerfile, then pushes the image to Docker Hub and applies the tag(s) to the image.

vi build.yml
name: Publish DockerHub image

on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
  pull_request:
    branches:
      - 'main'

jobs:
  build-container:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: itsmetommy/hello-github-actions

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Push to GitHub

git add .; git commit -m "Added dockerhub workflow"; git push origin main

Check GitHub Actions

Check DockerHub

You will notice that the tag is main, which matches the branch we pushed to.

Docker metadata-action

We can use the Docker metadata action to handle tags and labels based on GitHub actions events and Git metadata.

Update build.yml.

vi build.yml
name: Publish DockerHub image

on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
  pull_request:
    branches:
      - 'main'

jobs:
  build-container:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: itsmetommy/hello-github-actions
          tags: |
              type=ref,event=branch
              type=sha
              type=sha,prefix={{branch}}-
              type=sha,format=long
              type=sha,format=long,prefix={{branch}}-
              type=raw,value=latest

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Quick tag breakdown.

type=ref,event=branch                   # main
type=sha                                # sha-b88ec75
type=sha,prefix={{branch}}-             # main-b88ec75
type=sha,format=long                    # sha-b88ec75550cb6d268789285e91f9d82ee8017018
type=sha,format=long,prefix={{branch}}- # main-b88ec75550cb6d268789285e91f9d82ee8017018
type=raw,value=latest                   # latest

Push to GitHub

git add .; git commit -m "Updated workflow file"; git push origin main

Check GitHub Actions

Check DockerHub

You should see all your tagged docker images within DockerHub.