From 9eac07755b2c5fa80d35667b612354f8562a9951 Mon Sep 17 00:00:00 2001 From: Philippe Vaucher Date: Fri, 16 Aug 2024 08:45:13 +0200 Subject: [PATCH] Build & push images to both registries at once --- .github/actions/build/action.yml | 28 +++++++++------------------- bin/images | 10 ++++------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/.github/actions/build/action.yml b/.github/actions/build/action.yml index f5af3213..4fc2cc0d 100644 --- a/.github/actions/build/action.yml +++ b/.github/actions/build/action.yml @@ -9,6 +9,12 @@ inputs: runs: using: composite steps: + - name: Login to docker.io + uses: docker/login-action@master + with: + username: ${{ fromJSON(inputs.secrets).DOCKERHUB_USERNAME }} + password: ${{ fromJSON(inputs.secrets).DOCKERHUB_TOKEN }} + - name: Login to ghcr.io uses: docker/login-action@master with: @@ -26,29 +32,13 @@ runs: shell: bash run: | echo "tags=$(bin/images --json | jq -c '.images[] | select(.version == "${{ inputs.version }}" and .context == "${{ inputs.context }}") | .tags_with_repository')" >> $GITHUB_OUTPUT - echo "ghcr_tags=$(bin/images --json | jq -c '.images[] | select(.version == "${{ inputs.version }}" and .context == "${{ inputs.context }}") | .ghcr_tags_with_repository')" >> $GITHUB_OUTPUT - - name: Build & push image to ghcr.io + - name: Build & push images uses: docker/build-push-action@master with: pull: true push: ${{ github.ref == 'refs/heads/master' }} - tags: ${{ join(fromJSON(steps.data.outputs.ghcr_tags), ', ') }} - context: images/${{ inputs.version }}/${{ inputs.context }} - cache-from: type=registry,ref=${{ fromJSON(steps.data.outputs.ghcr_tags)[0] }}-cache - cache-to: type=registry,ref=${{ fromJSON(steps.data.outputs.ghcr_tags)[0] }}-cache,mode=max - - - name: Login to Docker Hub - uses: docker/login-action@master - with: - username: ${{ fromJSON(inputs.secrets).DOCKERHUB_USERNAME }} - password: ${{ fromJSON(inputs.secrets).DOCKERHUB_TOKEN }} - - - name: Push image to Docker Hub - uses: docker/build-push-action@master - with: - pull: false - push: ${{ github.ref == 'refs/heads/master' }} - cache-from: ${{ fromJSON(steps.data.outputs.ghcr_tags)[0] }}-cache tags: ${{ join(fromJSON(steps.data.outputs.tags), ', ') }} context: images/${{ inputs.version }}/${{ inputs.context }} + cache-from: type=registry,ref=${{ fromJSON(steps.data.outputs.tags)[0] }}-cache + cache-to: type=registry,ref=${{ fromJSON(steps.data.outputs.tags)[0] }}-cache,mode=max diff --git a/bin/images b/bin/images index e26ed5be..063f3fa7 100755 --- a/bin/images +++ b/bin/images @@ -19,6 +19,7 @@ class Image attr_reader :version attr_reader :context + REGISTRIES = %w[ghcr.io docker.io] REPOSITORY = 'silex/emacs' class << self @@ -101,7 +102,6 @@ class Image directory: directory, tags: tags, tags_with_repository: tags_with_repository, - ghcr_tags_with_repository: ghcr_tags_with_repository, depends: depends, depends_with_repository: depends_with_repository, children: children @@ -119,11 +119,9 @@ class Image end def tags_with_repository - tags.map{ |s| format('%s:%s', REPOSITORY, s) } - end - - def ghcr_tags_with_repository - tags_with_repository.map{ |s| format('ghcr.io/%s', s) } + REGISTRIES.map do |registry| + tags.map{ |s| format('%s/%s:%s', registry, REPOSITORY, s) } + end.flatten end def depends