refactor dockerhub workflow #4
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
name: Publish Docker image | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
on: # yamllint disable-line rule:truthy | |
push: | |
branches: | |
- 'main' | |
tags: | |
- 'v*' | |
paths-ignore: | |
- '**.md' | |
pull_request: | |
paths-ignore: | |
- '**.md' | |
env: | |
REGISTRY_IMAGE: tykling/dns_exporter | |
jobs: | |
prepare: | |
runs-on: ubuntu-22.04 | |
outputs: | |
matrix: ${{ steps.platforms.outputs.matrix }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 | |
- name: Create matrix | |
id: platforms | |
run: | | |
echo "matrix=$(docker buildx bake image-all --print | jq -cr '.target."image-all".platforms')" >>${GITHUB_OUTPUT} | |
- name: Show matrix | |
run: | | |
echo ${{ steps.platforms.outputs.matrix }} | |
build: | |
runs-on: ubuntu-22.04 | |
needs: | |
- prepare | |
strategy: | |
fail-fast: false | |
matrix: | |
platform: ${{ fromJson(needs.prepare.outputs.matrix) }} | |
steps: | |
- name: Prepare | |
run: | | |
platform=${{ matrix.platform }} | |
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
- name: Check out the repo | |
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 | |
with: | |
images: | | |
${{ env.REGISTRY_IMAGE }} | |
tags: | | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=semver,pattern={{major}} | |
type=ref,event=branch | |
type=ref,event=pr | |
type=sha | |
labels: | | |
org.opencontainers.image.title=dns_exporter | |
org.opencontainers.image.description=A Blackbox-style Prometheus exporter with a focus on DNS monitoring | |
org.opencontainers.image.vendor=tykling | |
- name: Rename meta bake definition file | |
run: | | |
mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta.json" | |
- name: Upload meta bake definition | |
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 | |
with: | |
name: bake-meta | |
path: /tmp/bake-meta.json | |
if-no-files-found: error | |
retention-days: 1 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 | |
with: | |
buildkitd-flags: "--debug" | |
- name: Log in to Docker Hub | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build | |
id: bake | |
uses: docker/bake-action@849707117b03d39aba7924c50a10376a69e88d7d | |
with: | |
files: | | |
./docker-bake.hcl | |
/tmp/bake-meta.json | |
targets: image | |
set: | | |
*.tags= | |
*.platform=${{ matrix.platform }} | |
*.cache-from=type=gha,scope=build-${{ env.PLATFORM_PAIR }} | |
*.cache-to=type=gha,scope=build-${{ env.PLATFORM_PAIR }} | |
*.output=type=image,"name=${{ env.REGISTRY_IMAGE }}",push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} | |
- name: Export digest | |
run: | | |
mkdir -p /tmp/digests | |
digest="${{ fromJSON(steps.bake.outputs.metadata).image['containerimage.digest'] }}" | |
touch "/tmp/digests/${digest#sha256:}" | |
- name: Upload digest | |
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 | |
with: | |
name: digests | |
path: /tmp/digests/* | |
if-no-files-found: error | |
retention-days: 1 | |
merge: | |
runs-on: ubuntu-22.04 | |
if: github.event_name != 'pull_request' | |
needs: | |
- build | |
steps: | |
- name: Download meta bake definition | |
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 | |
with: | |
name: bake-meta | |
path: /tmp | |
- name: Download digests | |
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 | |
with: | |
path: /tmp/digests | |
pattern: digests-* | |
merge-multiple: true | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 | |
- name: Login to DockerHub | |
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Create manifest list and push | |
working-directory: /tmp/digests | |
run: | | |
docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("${{ env.REGISTRY_IMAGE }}")) | "-t " + .) | join(" ")' /tmp/bake-meta.json) \ | |
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) | |
- name: Inspect image | |
run: | | |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:$(jq -r '.target."docker-metadata-action".args.DOCKER_META_VERSION' /tmp/bake-meta.json) | |
... |