Skip to content

Commit 0f935a7

Browse files
authored
Create docker-publish.yml with semver and matrix
1 parent 98eb848 commit 0f935a7

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

.github/workflows/docker-publish.yml

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Docker
2+
3+
# This workflow uses actions that are not certified by GitHub.
4+
# They are provided by a third-party and are governed by
5+
# separate terms of service, privacy policy, and support
6+
# documentation.
7+
8+
on:
9+
push:
10+
branches: [ "main" ]
11+
# Publish semver tags as releases.
12+
tags: [ 'v*.*.*' ]
13+
pull_request:
14+
branches: [ "main" ]
15+
16+
env:
17+
# Use docker.io for Docker Hub if empty
18+
REGISTRY: ghcr.io
19+
# github.repository as <account>/<repo>
20+
IMAGE_NAME: ${{ github.repository }}
21+
UTIL_IMAGE_NAME: ${{ github.repository }}
22+
23+
24+
jobs:
25+
build:
26+
27+
runs-on: ubuntu-latest
28+
strategy:
29+
matrix:
30+
include:
31+
- dockerfile: ./Dockerfile
32+
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
33+
- dockerfile: ./Dockerfile-util
34+
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-util
35+
permissions:
36+
contents: read
37+
packages: write
38+
# This is used to complete the identity challenge
39+
# with sigstore/fulcio when running outside of PRs.
40+
id-token: write
41+
42+
steps:
43+
- name: Checkout repository
44+
uses: actions/checkout@v4
45+
46+
# Install the cosign tool except on PR
47+
# https://github.com/sigstore/cosign-installer
48+
- name: Install cosign
49+
if: github.event_name != 'pull_request'
50+
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0
51+
with:
52+
cosign-release: 'v2.2.4'
53+
54+
# Set up BuildKit Docker container builder to be able to build
55+
# multi-platform images and export cache
56+
# https://github.com/docker/setup-buildx-action
57+
- name: Set up Docker Buildx
58+
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
59+
60+
# Login against a Docker registry except on PR
61+
# https://github.com/docker/login-action
62+
- name: Log into registry ${{ env.REGISTRY }}
63+
if: github.event_name != 'pull_request'
64+
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
65+
with:
66+
registry: ${{ env.REGISTRY }}
67+
username: ${{ github.actor }}
68+
password: ${{ secrets.GITHUB_TOKEN }}
69+
70+
# Extract metadata (tags, labels) for Docker
71+
# https://github.com/docker/metadata-action
72+
- name: Extract Docker metadata
73+
id: meta
74+
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
75+
with:
76+
images: ${{ matrix.image }}
77+
tags: |
78+
type=ref,event=branch
79+
type=ref,event=pr
80+
type=semver,pattern={{version}}
81+
type=semver,pattern={{major}}.{{minor}}
82+
83+
# Build and push Docker image with Buildx (don't push on PR)
84+
# https://github.com/docker/build-push-action
85+
- name: Build and push Docker image
86+
id: build-and-push
87+
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
88+
with:
89+
context: .
90+
file: ${{ matrix.dockerfile }}
91+
push: ${{ github.event_name != 'pull_request' }}
92+
tags: ${{ steps.meta.outputs.tags }}
93+
labels: ${{ steps.meta.outputs.labels }}
94+
cache-from: type=gha
95+
cache-to: type=gha,mode=max
96+
97+
# Sign the resulting Docker image digest except on PRs.
98+
# This will only write to the public Rekor transparency log when the Docker
99+
# repository is public to avoid leaking data. If you would like to publish
100+
# transparency data even for private images, pass --force to cosign below.
101+
# https://github.com/sigstore/cosign
102+
- name: Sign the published Docker image
103+
if: ${{ github.event_name != 'pull_request' }}
104+
env:
105+
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
106+
TAGS: ${{ steps.meta.outputs.tags }}
107+
DIGEST: ${{ steps.build-and-push.outputs.digest }}
108+
# This step uses the identity token to provision an ephemeral certificate
109+
# against the sigstore community Fulcio instance.
110+
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

0 commit comments

Comments
 (0)