Skip to content

Commit

Permalink
Merge pull request #19 from cromefire/github-multiplatform-fix
Browse files Browse the repository at this point in the history
Fix build for multiplatform docker
  • Loading branch information
cromefire authored Oct 22, 2023
2 parents e8d3f3f + fe3df5c commit a850d5b
Showing 1 changed file with 110 additions and 32 deletions.
142 changes: 110 additions & 32 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ jobs:
with:
platforms: ${{ matrix.platform.qemu }}

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/[email protected]
with:
cosign-release: 'v2.2.0'

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
Expand All @@ -72,18 +64,18 @@ jobs:
- name: Go package cache
uses: actions/cache@v3
with:
path: go-package-cache
key: ${{ matrix.platform.go }}-docker-go-package-${{ hashFiles('go.sum') }}
restore-keys: |
${{ matrix.platform.go }}-docker-go-package-
path: go-package-cache
key: ${{ matrix.platform.go }}-docker-go-package-${{ hashFiles('go.sum') }}
restore-keys: |
${{ matrix.platform.go }}-docker-go-package-
- name: Go build cache
uses: actions/cache@v3
with:
path: go-build-cache
key: ${{ matrix.platform.go }}-docker-go-build-${{ hashFiles('go.sum') }}
restore-keys: |
${{ matrix.platform.go }}-docker-go-build-
path: go-build-cache
key: ${{ matrix.platform.go }}-docker-go-build-${{ hashFiles('go.sum') }}
restore-keys: |
${{ matrix.platform.go }}-docker-go-build-
- name: Inject go package cache into docker
uses: reproducible-containers/[email protected]
Expand Down Expand Up @@ -114,42 +106,128 @@ jobs:
uses: docker/[email protected]
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=edge,branch=main
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,enable=false

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
- name: Build and push Docker image in PR
id: build-and-push-pr
uses: docker/[email protected]
if: github.event_name == 'pull_request'
with:
context: .
file: ${{ matrix.platform.dockerfile && matrix.platform.dockerfile || 'Dockerfile' }}
build-args: GOARCH=${{ matrix.platform.go }}
platforms: ${{ matrix.platform.docker }}
push: false
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Build and push Docker image with Buildx (push if not on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image outside Branch
id: build-and-push
uses: docker/[email protected]
if: github.event_name != 'pull_request'
with:
context: .
file: ${{ matrix.platform.dockerfile && matrix.platform.dockerfile || 'Dockerfile' }}
build-args: GOARCH=${{ matrix.platform.go }}
platforms: ${{ matrix.platform.docker }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
push: true
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true

- name: Export digest
if: github.event_name != 'pull_request'
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
create-manifests:
runs-on: ubuntu-latest
needs: [ build ]
if: github.event_name != 'pull_request'

steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
uses: sigstore/[email protected]
with:
cosign-release: 'v2.2.0'


# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/[email protected]
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=edge,branch=main
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,enable=false
- name: Create manifest list and push
env:
DOCKER_METADATA_OUTPUT_JSON: ${{ toJSON(steps.meta.outputs) }}
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
working-directory: /tmp/digests
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
RAW_TAGS: ${{ join(steps.meta.outputs.tags, ' ') }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
run: |
TAGS=""
for RAW_TAG in RAW_TAGS; do
TAGS="${TAGS}$(printf '${RAW_TAG}@sha256:%s ' *)"
done
echo "${TAGS}" | xargs -I {} cosign sign --yes {}
- name: Inspect image
run: |
for RAW_TAG in RAW_TAGS; do
docker buildx imagetools inspect ${RAW_TAG}
done

0 comments on commit a850d5b

Please sign in to comment.