From 6a248a94d2e1bafbf931b1548d03507b32843742 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Thu, 2 May 2024 13:22:30 -0400 Subject: [PATCH] Docker: improve tagging, distinguish `main` and `latest` * Use `latest` for the latest release * Use `main` (or the branch) for the latest dev builds against a branch * Tag releases with their version * Only push when building against `main` branch * Only build amd64 images on PRs for speedier builds * Separate main/PR caches --- .github/workflows/docker.yaml | 44 ++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 8873d060..affa8eb8 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -29,7 +29,7 @@ env: DOCKER_BUILDKIT: 1 jobs: - buildx: + docker-buildx: runs-on: ubuntu-latest steps: - name: Checkout @@ -41,26 +41,48 @@ jobs: uses: docker/setup-buildx-action@v3 - name: Log into Dockerhub registry run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u $DOCKERHUB_ACCOUNT --password-stdin - - name: Build + - name: Build and push Docker image + env: + RELEASE: "${{ startsWith(github.ref, 'refs/tags/') && 'ON' || 'OFF' }}" run: | + set -ex + DOCKERHUB_IMAGE_ID=$DOCKERHUB_ACCOUNT/$IMAGE_NAME # Change all uppercase to lowercase DOCKERHUB_IMAGE_ID=$(echo $DOCKERHUB_IMAGE_ID | tr '[A-Z]' '[a-z]') + image_tags=() + # Strip git ref prefix from version - VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + VERSION_TAG=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') # Strip "v" prefix from tag name - [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION_TAG=$(echo $VERSION_TAG | sed -e 's/^v//') - # Use Docker `latest` tag convention - [ "$VERSION" == "main" ] && VERSION=latest + image_tags+=("--tag" "$DOCKERHUB_IMAGE_ID:$VERSION_TAG") + + if [ "$RELEASE" == "ON" ]; then + # tag as latest on releases + image_tags+=("--tag" "$DOCKERHUB_IMAGE_ID:latest") + fi + + # Only build amd64 on PRs, build all platforms on main. The arm builds + # take far too long. + image_platforms="--platform linux/amd64" + push_image="" + cache_tag="pr-cache" + if [ "${{ github.ref }}" == "refs/head/main" ]; then + # Only push on main + push_image="--push" + image_platforms="--platform linux/arm/v7,linux/arm64/v8,linux/amd64" + cache_tag="main-cache" + fi docker buildx build \ - --push \ - --platform linux/arm/v7,linux/arm64/v8,linux/amd64 \ - --cache-from=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:buildcache \ - --cache-to=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:buildcache,mode=max \ - --tag $DOCKERHUB_IMAGE_ID:$VERSION \ + ${push_image} \ + ${image_platforms} \ + --cache-from=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:$cache_tag \ + --cache-to=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:$cache_tag,mode=max \ + "${image_tags[@]}" \ .