diff --git a/.github/scripts/docker-tags.sh b/.github/scripts/docker-tags.sh index 8c2a4e5..d0b557b 100755 --- a/.github/scripts/docker-tags.sh +++ b/.github/scripts/docker-tags.sh @@ -2,43 +2,50 @@ # Generates docker images tags for the docker/build-push-action@v2 action depending on the branch/tag. -declare -a IMAGE_TAGS +declare -a registryArr +registryArr+=("docker.io") # Docker Hub +registryArr+=("ghcr.io") # GitHub Container Registry + +declare -a imageTagArr # feature/* => sha-xxxxxxx # Note: disabled #if [[ "${GITHUB_REF}" =~ "refs/heads/feature/" ]]; then # GIT_SHA7=$(echo ${GITHUB_SHA} | cut -c1-7) # Short SHA (7 characters) -# IMAGE_TAGS+=("${IMAGE}:sha-${GIT_SHA7}-${VERSION}") -# IMAGE_TAGS+=("ghcr.io/${IMAGE}:sha-${GIT_SHA7}-${VERSION}") +# imageTagArr+=("${IMAGE}:sha-${GIT_SHA7}-${VERSION}") +# imageTagArr+=("ghcr.io/${IMAGE}:sha-${GIT_SHA7}-${VERSION}") #fi -# develop => edge +# develop => version-edge if [[ "${GITHUB_REF}" == "refs/heads/develop" ]]; then - IMAGE_TAGS+=("${IMAGE}:edge-${VERSION}") - IMAGE_TAGS+=("ghcr.io/${IMAGE}:edge-${VERSION}") + imageTagArr+=("${IMAGE}:${VERSION}-edge") fi -# master => stable +# master => version if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then - IMAGE_TAGS+=("${IMAGE}:stable-${VERSION}") - IMAGE_TAGS+=("ghcr.io/${IMAGE}:stable-${VERSION}") + imageTagArr+=("${IMAGE}:${VERSION}") fi # tags/v1.0.0 => 1.0 if [[ "${GITHUB_REF}" =~ "refs/tags/" ]]; then # Extract version parts from release tag - IFS='.' read -a ver_arr <<< "${GITHUB_REF#refs/tags/}" - VERSION_MAJOR=${ver_arr[0]#v*} # 2.7.0 => "2" - VERSION_MINOR=${ver_arr[1]} # "2.7.0" => "7" - IMAGE_TAGS+=("${IMAGE}:stable-${VERSION}") - IMAGE_TAGS+=("${IMAGE}:${VERSION_MAJOR}-${VERSION}") - IMAGE_TAGS+=("${IMAGE}:${VERSION_MAJOR}.${VERSION_MINOR}-${VERSION}") - IMAGE_TAGS+=("ghcr.io/${IMAGE}:stable-${VERSION}") - IMAGE_TAGS+=("ghcr.io/${IMAGE}:${VERSION_MAJOR}-${VERSION}") - IMAGE_TAGS+=("ghcr.io/${IMAGE}:${VERSION_MAJOR}.${VERSION_MINOR}-${VERSION}") + IFS='.' read -a release_arr <<< "${GITHUB_REF#refs/tags/}" + releaseMajor=${release_arr[0]#v*} # 2.7.0 => "2" + releaseMinor=${release_arr[1]} # "2.7.0" => "7" + imageTagArr+=("${IMAGE}:${VERSION}") + imageTagArr+=("${IMAGE}:${VERSION}-${releaseMajor}") + imageTagArr+=("${IMAGE}:${VERSION}-${releaseMajor}.${releaseMinor}") fi -# Output a comma concatenated list of image tags -IMAGE_TAGS_STR=$(IFS=,; echo "${IMAGE_TAGS[*]}") -echo "${IMAGE_TAGS_STR}" -echo "::set-output name=tags::${IMAGE_TAGS_STR}" +# Build an array of registry/image:tag values +declare -a repoImageTagArr +for registry in ${registryArr[@]}; do + for imageTag in ${imageTagArr[@]}; do + repoImageTagArr+=("${registry}/${imageTag}") + done +done + +# Print with new lines for output in build logs +(IFS=$'\n'; echo "${repoImageTagArr[*]}") +# Using newlines in outputs variables does not seem to work, so we'll use comas +(IFS=$','; echo "::set-output name=tags::${repoImageTagArr[*]}") diff --git a/.github/workflows/docker-build-push.yaml b/.github/workflows/docker-build-push.yaml index 3844ecc..a2d3d4c 100644 --- a/.github/workflows/docker-build-push.yaml +++ b/.github/workflows/docker-build-push.yaml @@ -93,20 +93,40 @@ jobs: id: docker_tags run: make tags - - # Build for local use + registry cache (ghcr.io) - name: Build and cache image (local) - id: docker_build_local + # Build for local use + name: Build image (amd64) + id: docker_build_amd64 uses: docker/build-push-action@v2 + env: + IMAGE_CACHE: ghcr.io/${{ env.IMAGE }}:build-${{ env.VERSION }} with: context: . file: ./Dockerfile build-args: | UPSTREAM_IMAGE=${{ env.UPSTREAM_IMAGE }} VERSION=${{ env.VERSION }} - #platforms: linux/amd64,linux/arm64 # Note: cannot use multi-platform with local image caching ("load: true") + platforms: linux/amd64 # Note: cannot use multi-platform with local image caching ("load: true") tags: ${{ env.IMAGE }}:build-${{ env.VERSION }} load: true # cache image locally for use by other steps - cache-from: type=registry,ref=ghcr.io/${{ env.DOCKER_IMAGE }}:build-${{ matrix.version }} + cache-from: type=registry,ref=${{ env.IMAGE_CACHE }} + cache-to: type=inline # Write the cache metadata into the image configuration + - + # Build for local use + name: Build image (arm64) + id: docker_build_arm64 + uses: docker/build-push-action@v2 + env: + IMAGE_CACHE: ghcr.io/${{ env.IMAGE }}:build-${{ env.VERSION }} + with: + context: . + file: ./Dockerfile + build-args: | + UPSTREAM_IMAGE=${{ env.UPSTREAM_IMAGE }} + VERSION=${{ env.VERSION }} + platforms: linux/arm64 # Note: cannot use multi-platform with local image caching ("load: true") + tags: ${{ env.IMAGE }}:build-${{ env.VERSION }} + load: true # cache image locally for use by other steps + cache-from: type=registry,ref=${{ env.IMAGE_CACHE }} cache-to: type=inline # Write the cache metadata into the image configuration - # Print image info @@ -117,10 +137,11 @@ jobs: docker image inspect "${{ env.IMAGE }}:build-${{ env.VERSION }}" - # Cache image layers in the registry - # This will pick-up the build cache from the local build step - name: Build and push image cache (ghcr.io) - id: docker_build_cache + name: Push image cache (ghcr.io) + id: docker_push_cache uses: docker/build-push-action@v2 + env: + IMAGE_CACHE: ghcr.io/${{ env.IMAGE }}:build-${{ env.VERSION }} with: context: . file: ./Dockerfile @@ -128,7 +149,7 @@ jobs: UPSTREAM_IMAGE=${{ env.UPSTREAM_IMAGE }} VERSION=${{ env.VERSION }} platforms: linux/amd64,linux/arm64 - tags: ghcr.io/${{ env.IMAGE }}:build-${{ env.VERSION }} # Build cache tag in ghcr.io + tags: ${{ env.IMAGE_CACHE }} # Build cache tag in ghcr.io push: ${{ github.event_name != 'pull_request' }} # Don't push for PRs cache-to: type=inline # Write the cache metadata into the image configuration - @@ -138,8 +159,8 @@ jobs: - # Push final image to the registry # This will pick-up the build cache from the local build step - name: Build and push image - id: docker_build_push + name: Push image + id: docker_push_image # Don't run if the list of tags is empty # Note: using tags from docker_tags (custom) if: ${{ steps.docker_tags.outputs.tags != '' }}