From bbf790247b1f2990c0797c1998024c803d770064 Mon Sep 17 00:00:00 2001 From: Patrick Hobusch Date: Thu, 28 Jul 2022 17:30:23 +0200 Subject: [PATCH] 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 --- .../setup-docker-environment/action.yaml | 38 ------------ .github/actions/setup-image-vars/action.yaml | 35 +++++++++++ .github/workflows/images.yaml | 60 ++++++------------- ...le.debian => Dockerfile.debian-stable-slim | 0 4 files changed, 52 insertions(+), 81 deletions(-) delete mode 100644 .github/actions/setup-docker-environment/action.yaml create mode 100644 .github/actions/setup-image-vars/action.yaml rename Dockerfile.debian => Dockerfile.debian-stable-slim (100%) diff --git a/.github/actions/setup-docker-environment/action.yaml b/.github/actions/setup-docker-environment/action.yaml deleted file mode 100644 index 35218844..00000000 --- a/.github/actions/setup-docker-environment/action.yaml +++ /dev/null @@ -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 }} diff --git a/.github/actions/setup-image-vars/action.yaml b/.github/actions/setup-image-vars/action.yaml new file mode 100644 index 00000000..c9cdd975 --- /dev/null +++ b/.github/actions/setup-image-vars/action.yaml @@ -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} diff --git a/.github/workflows/images.yaml b/.github/workflows/images.yaml index cd8da2d1..37eb9b2e 100644 --- a/.github/workflows/images.yaml +++ b/.github/workflows/images.yaml @@ -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 diff --git a/Dockerfile.debian b/Dockerfile.debian-stable-slim similarity index 100% rename from Dockerfile.debian rename to Dockerfile.debian-stable-slim