Docker Publish Action

A composite GitHub Action that builds and pushes Docker images tagged with a branch name and git tags.

Description

Builds Docker images using make docker-build and pushes them with make docker-push. Images are tagged with the caller-specified branch name and any git tags pointing at HEAD. Golang-style version tags (e.g., v1.2.3) have the leading v stripped automatically.

Usage

- uses: ./.github/actions/docker-publish-action
  with:
    docker-registry: "docker.io"
    docker-repository: "myorg"
    image-name: "myapp"
    branch-tag: ${{ github.ref_name }}
    docker-username: ${{ secrets.DOCKER_USERNAME }}
    docker-password: ${{ secrets.DOCKER_PASSWORD }}

Inputs

InputRequiredDefaultDescription
docker-registryNodocker.ioDocker registry URL
docker-repositoryYesDocker repository prefix
image-nameYesName of the Docker image
branch-tagYesBranch name to use as a Docker image tag
extra-environment-varsNo""Extra environment variables for make commands
docker-usernameYesDocker registry username
docker-passwordYesDocker registry password

Example Workflow

name: Publish Docker Image

on:
  push:
    branches: [main]
    tags: ["v*"]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: ./.github/actions/docker-publish-action
        with:
          docker-registry: "docker.io"
          docker-repository: "voltha"
          image-name: "voltha-northbound"
          branch-tag: ${{ github.ref_name }}
          docker-username: ${{ secrets.DOCKER_USERNAME }}
          docker-password: ${{ secrets.DOCKER_PASSWORD }}

How It Works

  1. Logs in to the configured Docker registry
  2. Discovers git tags pointing at the current HEAD commit
  3. Runs make docker-build with DOCKER_TAG set to the branch-tag input
  4. Runs make docker-build for each git tag (stripping the v prefix)
  5. Runs make docker-push for the branch tag and each git tag
  6. Logs out of the Docker registry

Requirements

  • The repository must have a Makefile with docker-build and docker-push targets
  • These targets must respect DOCKER_TAG, DOCKER_REGISTRY, DOCKER_REPOSITORY, and IMAGE_NAME environment variables
  • The checkout step should use fetch-depth: 0 to ensure git tags are available