Skip to content

Commit 33f29c1

Browse files
authored
CI(Docker): Docker workflow improvements (#5128)
1 parent 2f7de93 commit 33f29c1

File tree

1 file changed

+98
-11
lines changed

1 file changed

+98
-11
lines changed

.github/workflows/docker.yml

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ on:
1616
branches:
1717
- main
1818
- releasebranch_*
19-
- '!releasebranch_7_*'
19+
- "!releasebranch_7_*"
2020
# tags: ['*.*.*']
2121
paths-ignore: [doc/**]
22+
pull_request:
23+
paths:
24+
- .github/workflows/docker.yml
25+
- Dockerfile
26+
- docker/**
27+
- "!docker/**.md"
28+
workflow_dispatch:
2229
release:
2330
types: [published]
2431

@@ -35,10 +42,17 @@ jobs:
3542
# For a release, e.g. 8.3.0, created tags are:
3643
# 8.3.0-alpine, 8.3.0-debian, 8.3.0-ubuntu and latest (with ubuntu)
3744
docker-os-matrix:
38-
name: build and push ${{ matrix.os }} for ${{ github.ref }}
39-
if: github.repository_owner == 'OSGeo'
45+
name: ${{ matrix.os }} for ${{ github.ref }}
4046
runs-on: ubuntu-latest
41-
47+
concurrency:
48+
group: >-
49+
${{ github.workflow }}-${{ matrix.os }}-${{ github.event_name }}-
50+
${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
51+
# Cancel in progress in pull requests.
52+
# Otherwise, limit to one in progress and one queued for each type.
53+
# Only the latest queued job per event type will be kept, older will be cancelled.
54+
# The already running job will be completed.
55+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
4256
strategy:
4357
matrix:
4458
os:
@@ -49,32 +63,78 @@ jobs:
4963
fail-fast: false
5064

5165
permissions:
66+
attestations: write
5267
contents: read
68+
id-token: write
5369
packages: write
5470

5571
steps:
5672
- name: Checkout
5773
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
5874
with:
5975
fetch-depth: 0
76+
- name: Get the latest tag and release branches
77+
id: tag-branch
78+
run: |
79+
# Make sure tags are fetched
80+
git fetch --tags
81+
# Get sorted list of tags, keep the first that has a semver pattern (not RCs)
82+
latest_tag="$(git tag --sort=-v:refname \
83+
| grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' \
84+
| head -n 1)"
85+
latest_rel_branch="$(git branch --all --list 'origin/*' \
86+
--contains "${latest_tag}" --format "%(refname:lstrip=3)")"
87+
echo "latest_tag=${latest_tag}" >> "${GITHUB_OUTPUT}"
88+
echo "latest_tag is: ${latest_tag}"
89+
echo "latest_rel_branch=${latest_rel_branch}" >> "${GITHUB_OUTPUT}"
90+
echo "latest_rel_branch is: ${latest_rel_branch}"
91+
- name: Get enable values for meta step
92+
id: enable
93+
run: |
94+
latest="${{
95+
(github.ref || format('{0}{1}', 'refs/tags/', github.event.release.tag_name))
96+
== format('refs/tags/{0}', steps.tag-branch.outputs.latest_tag)
97+
&& matrix.os == 'ubuntu' }}"
98+
current="${{
99+
( contains(fromJSON('["tag", "release"]'), github.event_name)
100+
&& (github.ref || format('{0}{1}', 'refs/tags/', github.event.release.tag_name))
101+
== format('refs/tags/{0}', steps.tag-branch.outputs.latest_tag)
102+
)
103+
|| github.ref == format('refs/heads/{0}', steps.tag-branch.outputs.latest_rel_branch)
104+
}}"
105+
echo "latest=${latest}" >> "${GITHUB_OUTPUT}"
106+
echo "latest is $latest"
107+
echo "current=${current}" >> "${GITHUB_OUTPUT}"
108+
echo "current is $current"
60109
- name: Docker meta
61110
id: meta
62111
uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5.6.1
63112
with:
64-
images: osgeo/grass-gis
113+
images: |
114+
name=docker.io/osgeo/grass-gis,enable=${{ github.repository_owner == 'OSGeo'
115+
&& github.event_name != 'pull_request' }}
116+
name=ghcr.io/${{ github.repository }}
65117
tags: |
66118
type=ref,event=tag
67119
type=ref,event=branch
68-
type=raw,value=current,enable=${{ github.ref == format('refs/heads/{0}', 'releasebranch_8_3') }}
69-
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/8.3') && matrix.os == 'ubuntu' }},suffix=
120+
type=ref,event=pr
121+
type=raw,value=current,enable=${{ steps.enable.outputs.current }}
122+
type=raw,value=latest,enable=${{ steps.enable.outputs.latest }},suffix=
70123
flavor: |
71124
latest=false
72125
suffix=-${{ matrix.os }}
73126
- name: Set up QEMU
74127
uses: docker/setup-qemu-action@4574d27a4764455b42196d70a065bc6853246a25 # v3.4.0
75128
- name: Set up Docker Buildx
76129
uses: docker/setup-buildx-action@f7ce87c1d6bead3e36075b2ce75da1f6cc28aaca # v3.9.0
77-
- name: Login to DockerHub
130+
- name: Login to GitHub Container Registry
131+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
132+
with:
133+
registry: ghcr.io
134+
username: ${{ github.actor }}
135+
password: ${{ secrets.GITHUB_TOKEN }}
136+
- name: Login to Docker Hub
137+
if: ${{ github.repository_owner == 'OSGeo' && github.event_name != 'pull_request' }}
78138
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
79139
with:
80140
username: ${{ secrets.DOCKERHUB_USERNAME }}
@@ -83,12 +143,39 @@ jobs:
83143
id: docker_build
84144
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0
85145
with:
86-
push: true
87-
pull: true
146+
push: ${{ github.event_name != 'pull_request' }}
88147
context: .
89148
tags: ${{ steps.meta.outputs.tags }}
90149
file: docker/${{ matrix.os }}/Dockerfile
91-
cache-from: type=gha,scope=${{ matrix.os }}
150+
annotations: ${{ steps.meta.outputs.annotations }}
151+
provenance: mode=max
152+
sbom: true
153+
# Don't use cache for releases.
154+
no-cache: ${{ contains(fromJSON('["tag", "release"]'), github.event_name) && true }}
155+
# Don't use gha cache for releases. Cache is not used if `cache-from:` is empty
156+
cache-from: >-
157+
${{ !contains(fromJSON('["tag", "release"]'), github.event_name)
158+
&& format('type=gha,scope={0}', matrix.os) || '' }}
92159
cache-to: type=gha,mode=max,scope=${{ matrix.os }}
93160
- name: Image digest
94161
run: echo ${{ steps.docker_build.outputs.digest }}
162+
- name: Attest docker.io image
163+
uses: actions/attest-build-provenance@520d128f165991a6c774bcb264f323e3d70747f4 # v2.2.0
164+
# If there isn't a digest, an annotation cannot be added
165+
if: >-
166+
${{ github.repository_owner == 'OSGeo' && github.event_name != 'pull_request'
167+
&& steps.docker_build.outputs.digest }}
168+
id: attest
169+
with:
170+
subject-name: docker.io/osgeo/grass-gis
171+
subject-digest: ${{ steps.docker_build.outputs.digest }}
172+
push-to-registry: ${{ github.repository_owner == 'OSGeo' && github.event_name != 'pull_request' }}
173+
- name: Attest ghcr.io image
174+
uses: actions/attest-build-provenance@520d128f165991a6c774bcb264f323e3d70747f4 # v2.2.0
175+
# If there isn't a digest, an annotation cannot be added
176+
if: ${{ steps.docker_build.outputs.digest }}
177+
id: attest-ghcr
178+
with:
179+
subject-name: ghcr.io/${{ github.repository }}
180+
subject-digest: ${{ steps.docker_build.outputs.digest }}
181+
push-to-registry: ${{ github.event_name != 'pull_request' }}

0 commit comments

Comments
 (0)