From 9fc488308663471de46c3fe6c5bbe66da566d0b0 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Mon, 4 Dec 2023 13:25:46 +0000 Subject: [PATCH] Update Docker reusable workflow --- .github/workflows/docker-build-push.yml | 64 +++++++++++++++---------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/.github/workflows/docker-build-push.yml b/.github/workflows/docker-build-push.yml index aee99a5..8bbfa13 100644 --- a/.github/workflows/docker-build-push.yml +++ b/.github/workflows/docker-build-push.yml @@ -3,7 +3,7 @@ name: Build and Push to Docker Hub on: workflow_call: inputs: - download_artifact: + download-artifact: description: > 'The name of a build artifact containing the files required to build the image(s). The download should contain a single .tar archive named build.tar.' @@ -15,26 +15,26 @@ on: default: '["linux/amd64"]' required: false type: string - repository_name: + repository-name: description: 'The repository to push images to (e.g. namespace/repo-name).' default: ${{ github.repository }} required: false type: string - tag_latest: + tag-latest: description: 'Whether to tag the builds with the latest tag or not.' - default: false + default: 'false' required: false - type: boolean - tag_raw: + type: string + tag-raw: description: 'The raw tag name to tag the builds with, if any.' default: '' required: false type: string secrets: - dockerhub_username: + dockerhub-username: description: 'The username used to authenticate with Docker Hub.' required: true - dockerhub_token: + dockerhub-token: description: 'The personal access token used to authenticate with Docker Hub.' required: true @@ -51,11 +51,13 @@ jobs: id: meta uses: docker/metadata-action@v5 with: - images: ${{ inputs.repository_name }} + images: ${{ inputs.repository-name }} + flavor: | + latest=false tags: | type=semver,pattern={{version}} - type=raw,value=latest,enable=${{ inputs.tag_latest }} - type=raw,value=${{ inputs.tag_raw }},enable=${{ inputs.tag_raw != '' }} + type=raw,value=latest,enable=${{ inputs.tag-latest == 'true' }} + type=raw,value=${{ inputs.tag-raw }},enable=${{ inputs.tag-raw != '' }} - name: 'Set up QEMU' uses: docker/setup-qemu-action@v3 @@ -66,21 +68,28 @@ jobs: - name: 'Login to Docker Hub' uses: docker/login-action@v3 with: - username: ${{ secrets.dockerhub_username }} - password: ${{ secrets.dockerhub_token }} + username: ${{ secrets.dockerhub-username }} + password: ${{ secrets.dockerhub-token }} + + - name: 'Checkout Dockerfile' + uses: actions/checkout@v4 + with: + sparse-checkout: | + Dockerfile + sparse-checkout-cone-mode: false - name: 'Download Files Required for Build' - if: ${{ inputs.download_artifact != '' }} + if: ${{ inputs.download-artifact != '' }} uses: actions/download-artifact@v3 with: - name: ${{ inputs.download_artifact }} + name: ${{ inputs.download-artifact }} path: '/tmp/' - name: 'Extract Files Required for Build' - if: ${{ inputs.download_artifact != '' }} + if: ${{ inputs.download-artifact != '' }} run: | - mkdir /tmp/src - tar -xvf /tmp/build.tar -C /tmp/src + mkdir ./src + tar -xvf /tmp/build.tar -C ./src - name: 'Build and Push by Digest' id: build @@ -88,8 +97,9 @@ jobs: with: cache-from: type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }} cache-to: type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }} + context: . labels: ${{ steps.meta.outputs.labels }} - outputs: type=image,name=${{ inputs.repository_name }},push-by-digest=true,name-canonical=true,push=true + outputs: type=image,name=${{ inputs.repository-name }},push-by-digest=true,name-canonical=true,push=true platforms: ${{ matrix.platform }} - name: 'Export Digest' @@ -124,24 +134,26 @@ jobs: id: meta uses: docker/metadata-action@v5 with: - images: ${{ inputs.repository_name }} + images: ${{ inputs.repository-name }} + flavor: | + latest=false tags: | type=semver,pattern={{version}} - type=raw,value=latest,enable=${{ inputs.tag_latest }} - type=raw,value=${{ inputs.tag_raw }},enable=${{ inputs.tag_raw != '' }} + type=raw,value=latest,enable=${{ inputs.tag-latest == 'true' }} + type=raw,value=${{ inputs.tag-raw }},enable=${{ inputs.tag-raw != '' }} - name: 'Login to Docker Hub' uses: docker/login-action@v3 with: - username: ${{ secrets.dockerhub_username }} - password: ${{ secrets.dockerhub_token }} + username: ${{ secrets.dockerhub-username }} + password: ${{ secrets.dockerhub-token }} - name: 'Create Manifest List and Push' run: | docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ - $(printf '${{ inputs.repository_name }}@sha256:%s ' *) + $(printf '${{ inputs.repository-name }}@sha256:%s ' *) working-directory: /tmp/digests - name: 'Inspect Image' run: | - docker buildx imagetools inspect ${{ inputs.repository_name }}:${{ steps.meta.outputs.version }} \ No newline at end of file + docker buildx imagetools inspect ${{ inputs.repository-name }}:${{ steps.meta.outputs.version }} \ No newline at end of file