Byparr/.github/workflows/docker-publish.yml

122 lines
4.1 KiB
YAML
Raw Normal View History

2024-07-25 02:08:26 +02:00
name: Docker
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
on:
schedule:
2024-07-25 21:07:10 +00:00
- cron: "25 0 * * *"
2024-07-25 02:08:26 +02:00
push:
2024-07-25 21:07:10 +00:00
branches: ["main"]
2024-07-25 02:08:26 +02:00
# Publish semver tags as releases.
2024-07-25 21:07:10 +00:00
tags: ["v*.*.*"]
2024-07-25 02:08:26 +02:00
pull_request:
2024-07-25 21:07:10 +00:00
branches: ["main"]
2024-11-26 18:18:02 +00:00
workflow_dispatch:
2024-07-25 02:08:26 +02:00
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
2024-12-11 00:40:11 +00:00
strategy:
matrix:
include:
2024-12-11 00:46:53 +00:00
- arch: amd64
- arch: arm64
2024-12-11 00:40:11 +00:00
2024-12-11 00:46:53 +00:00
runs-on: ubuntu-latest
2024-07-25 02:08:26 +02:00
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
2024-11-30 12:43:56 +00:00
uses: sigstore/cosign-installer@v3
2024-07-25 02:08:26 +02:00
# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
2024-11-30 12:43:56 +00:00
uses: docker/setup-buildx-action@v3
2024-07-25 02:08:26 +02:00
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
2024-11-30 12:43:56 +00:00
uses: docker/login-action@v3
2024-07-25 02:08:26 +02:00
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
2024-11-30 12:43:56 +00:00
uses: docker/metadata-action@v5
2024-07-25 02:08:26 +02:00
with:
2024-07-25 21:07:10 +00:00
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,enable=${{ github.event_name != 'pull_request' && github.ref_name == 'main' }}, value=latest
2024-07-25 02:08:26 +02:00
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
2024-12-10 21:54:29 +00:00
- name: Test
id: test
2024-12-11 00:46:53 +00:00
if: ${{matrix.arch == 'amd64'}}
2024-12-10 21:54:29 +00:00
uses: docker/build-push-action@v6
with:
context: .
2024-12-11 00:40:11 +00:00
platforms: linux/${{ matrix.arch }}
2024-12-10 21:54:29 +00:00
cache-from: type=gha
cache-to: type=gha,mode=max
target: test
2024-07-25 02:08:26 +02:00
# 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
id: build-and-push
2024-11-30 12:43:56 +00:00
uses: docker/build-push-action@v6
2024-07-25 02:08:26 +02:00
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2024-12-11 00:40:11 +00:00
platforms: linux/${{ matrix.arch }}
2024-07-25 02:08:26 +02:00
cache-from: type=gha
cache-to: type=gha,mode=max
2024-11-30 12:43:56 +00:00
build-args: GITHUB_BUILD=true,VERSION=${{ github.ref_name }}
2024-07-25 02:08:26 +02:00
# 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' }}
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 }}
# 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}