Refactor 'images' workflow, include Ubuntu image to push

Until now, the 'images' workflow was separated into two different jobs,
one for just building the images in e.g. pull requests and the other
one for building and pushing the images e.g. after a merge to the 'main'
branch, which resulted in code repetitions. Also, both jobs used
different approaches, one (build) using a 'matrix strategy' based on
the file name of the Dockerfile, the other one (build and push) having a
seperate build and push step for each Dockerfile.

With this change, both jobs have been unified into a single "build and
optionally push" job to remove the repetitions, which now also shares
the same approach - a matrix strategy based on the file names of the
Dockerfiles.

The package naming now follows a clear schema based on the file name of
the Dockerfile. 'Dockerfile' will result in a 'helmfile' package,
'Dockerfile.ubuntu' will result in a 'helmfile-ubuntu' package and so
on. In order to keep the 'helmfile-debian-stable-slim' image package
name, the 'Dockerfile.debian' had to be renamed to
'Dockerfile.debian-stable-slim' accordingly.

Furthermore, the evaluation of the condition whether a push is intended
(or not) has been moved directly to the 'push' flag of the
'docker/build-push-action'.

Signed-off-by: Patrick Hobusch <patrick.hobusch@gmail.com>
This commit is contained in:
Patrick Hobusch 2022-07-28 17:30:23 +02:00
parent 86bfb5689a
commit bbf790247b
4 changed files with 52 additions and 81 deletions

View File

@ -1,38 +0,0 @@
name: "Setup Docker"
outputs:
sha_short:
description: "The short SHA used for image builds"
value: ${{ steps.vars.outputs.sha_short }}
tag:
description: "The tag if run against a tag, otherwise 'canary'"
value: ${{ steps.vars.outputs.tag }}
runs:
using: "composite"
steps:
- name: Get Short SHA & Tag
id: vars
run: |
echo ::set-output name=sha_short::${GITHUB_SHA::7}
TAG=${GITHUB_REF##*/}
if [ "main" == "${TAG}" ]; then
TAG=canary
fi
echo ::set-output name=tag::${TAG:-canary}
shell: bash
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
# - name: Login to DockerHub
# if: ${{ github.ref == 'main' && github.event.pull_request.merged == true }}
# uses: docker/login-action@v1
# with:
# username: ${{ inputs.username }}
# password: ${{ inputs.password }}

View File

@ -0,0 +1,35 @@
name: "Setup Image Variables"
inputs:
dockerfile:
required: true
outputs:
tag:
description: "The tag if run against a tag, otherwise 'canary'"
value: ${{ steps.vars.outputs.tag }}
suffix:
description: "The suffix for the image package name (if any)"
value: ${{ steps.vars.outputs.suffix }}
runs:
using: "composite"
steps:
- name: Get reference and suffix
id: vars
shell: bash
env:
DOCKERFILE: ${{ inputs.dockerfile }}
run: |
TAG=${GITHUB_REF##*/}
if [[ $GITHUB_REF == refs/heads/main ]]; then
TAG=canary
elif [[ $GITHUB_REF == refs/pull/*/merge ]]; then
TAG=pullrequest # this 'tag' is just used for caching
fi
echo ::set-output name=tag::${TAG:-canary}
SUFFIX=${DOCKERFILE##Dockerfile}
if [[ "${SUFFIX}" == "."* ]]; then
SUFFIX=${SUFFIX//./-} # convert dots into dashes
fi
echo ::set-output name=suffix::${SUFFIX}

View File

@ -22,18 +22,18 @@ on:
- "Makefile"
jobs:
build-test:
if: startsWith(github.ref, 'refs/pull/')
build:
name: Build
runs-on: ubuntu-latest
permissions:
contents: read
name: Build
packages: write
strategy:
matrix:
include:
- dockerfile: Dockerfile
- dockerfile: Dockerfile.debian
- dockerfile: Dockerfile.ubuntu
dockerfile:
- Dockerfile
- Dockerfile.debian-stable-slim
- Dockerfile.ubuntu
steps:
- name: Checkout
uses: actions/checkout@v3
@ -43,29 +43,14 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build
uses: docker/build-push-action@v3
with:
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
version: latest
build:
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
name: Build and Publish
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKER_USER }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Docker Environment
- name: Set up Image Variables
id: vars
uses: ./.github/actions/setup-docker-environment
uses: ./.github/actions/setup-image-vars
with:
dockerfile: ${{ matrix.dockerfile }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
@ -74,24 +59,13 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push
uses: docker/build-push-action@v2
- name: Build / Push
uses: docker/build-push-action@v3
with:
file: Dockerfile
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
push: true
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
tags: |
ghcr.io/${{ github.repository }}:${{ steps.vars.outputs.tag }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and Push (debian stable-slim)
uses: docker/build-push-action@v2
with:
file: Dockerfile.debian
platforms: linux/amd64,linux/arm64
push: true
tags: |
ghcr.io/${{ github.repository }}-debian-stable-slim:${{ steps.vars.outputs.tag }}
ghcr.io/${{ github.repository }}${{ steps.vars.outputs.suffix }}:${{ steps.vars.outputs.tag }}
cache-from: type=gha
cache-to: type=gha,mode=max