-
Notifications
You must be signed in to change notification settings - Fork 3
212 lines (194 loc) · 8.86 KB
/
docker.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: Docker
on:
push:
branches:
- 'main'
- 'feature/**'
tags:
- 'v*.*.*'
schedule:
- cron: '0 06 * * *'
permissions:
contents: read
jobs:
docker:
name: Docker
runs-on: ubuntu-latest
permissions:
id-token: write # needed for OIDC Token signing with cosign
packages: write # needed for pushing packages
strategy:
matrix:
image: ["olareg"]
type: ["scratch", "alpine"]
env:
ALPINE_NAME: "alpine:3"
ALPINE_DIGEST: "sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd" # 3.20.0
steps:
- name: Check out code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- name: Prepare
id: prep
run: |
mkdir -p "output/${{matrix.image}}"
EXT=""
if [ "${{ matrix.type }}" != "scratch" ]; then
EXT="-${{ matrix.type }}"
fi
HUB_IMAGE=olareg/${{ matrix.image }}
GHCR_IMAGE=ghcr.io/olareg/${{ matrix.image }}
VERSION="(devel)"
if [ "${{ github.event_name }}" = "schedule" ]; then
VERSION=edge
elif [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION="${GITHUB_REF#refs/tags/}"
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION="${GITHUB_REF#refs/heads/}"
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
VERSION=edge
fi
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION="pr-${{ github.event.number }}"
fi
VERSION="$(echo "${VERSION}" | sed -r 's#/+#-#g')"
TAGS="${GHCR_IMAGE}:${VERSION}${EXT}"
# TAGS="${TAGS},${HUB_IMAGE}:${VERSION}${EXT}"
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
MINOR="${VERSION%.*}"
MAJOR="${MINOR%.*}"
# TAGS="${TAGS},${HUB_IMAGE}:${MINOR}${EXT},${HUB_IMAGE}:${MAJOR}${EXT}"
TAGS="${TAGS},${GHCR_IMAGE}:${MINOR}${EXT},${GHCR_IMAGE}:${MAJOR}${EXT}"
if [ "${{ matrix.type }}" == "scratch" ]; then
# TAGS="${TAGS},${HUB_IMAGE}:latest"
TAGS="${TAGS},${GHCR_IMAGE}:latest"
else
# TAGS="${TAGS},${HUB_IMAGE}:${{ matrix.type }}"
TAGS="${TAGS},${GHCR_IMAGE}:${{ matrix.type }}"
fi
fi
VCS_SEC="$(git log -1 --format=%ct)"
VCS_DATE="$(date -d "@${VCS_SEC}" +%Y-%m-%dT%H:%M:%SZ --utc)"
REPO_URL="${{github.server_url}}/${{github.repository}}.git"
echo "version=${VERSION}" >>$GITHUB_OUTPUT
echo "image_hub=${HUB_IMAGE}" >>$GITHUB_OUTPUT
echo "image_ghcr=${GHCR_IMAGE}" >>$GITHUB_OUTPUT
echo "tags=${TAGS}" >>$GITHUB_OUTPUT
echo "vcs_sec=${VCS_SEC}" >>$GITHUB_OUTPUT
echo "created=${VCS_DATE}" >>$GITHUB_OUTPUT
echo "repo_url=${REPO_URL}" >>$GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0
# - name: Login to DockerHub
# if: github.repository_owner == 'olareg'
# uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GHCR
if: github.repository_owner == 'olareg'
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # v5.3.0
id: build
with:
context: .
file: ./build/Dockerfile.${{ matrix.image }}.buildkit
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x
target: release-${{ matrix.type }}
outputs: type=oci,dest=output/${{matrix.image}}-${{matrix.type}}.tar
build-args: |
SOURCE_DATE_EPOCH=${{ steps.prep.outputs.vcs_sec }}
BUILD_DATE=${{ steps.prep.outputs.created }}
VCS_REF=${{ github.sha }}
VCS_VERSION=${{ steps.prep.outputs.version }}
- name: Install cosign
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 # v3.5.0
with:
cosign-release: "v2.2.4"
- name: Install syft
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
uses: anchore/sbom-action/download-syft@e8d2a6937ecead383dfe75190d104edd1f9c5751 # v0.16.0
id: syft
with:
syft-version: "v1.5.0"
# Use regctl to modify olareg images to improve reproducibility
- name: Install regctl
uses: regclient/actions/regctl-installer@2dac4eff5925ed07edbfe12d2d11af6304df29a6 # main
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
- name: Mutate
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
id: mutate
run: |
vcs_date="${{ steps.prep.outputs.created }}"
base_name=""
mkdir -p "output/${{matrix.image}}"
if [ "${{matrix.type}}" = "alpine" ]; then
base_name="${ALPINE_NAME}"
base_digest="${ALPINE_DIGEST}"
fi
# mutate the image locally
local_tag="ocidir://output/${{matrix.image}}:${{matrix.type}}"
echo "Loading ${local_tag} from output/${{matrix.image}}-${{matrix.type}}.tar"
regctl image import "${local_tag}" "output/${{matrix.image}}-${{matrix.type}}.tar"
echo "Modifying image for reproducibility"
regctl image mod "${local_tag}" --replace \
--to-oci-referrers --label-to-annotation --annotation-promote
if [ -n "$base_name" ] && [ -n "$base_digest" ]; then
regctl image mod "${local_tag}" --replace \
--annotation "[*]org.opencontainers.image.base.name=${base_name}" \
--annotation "[*]org.opencontainers.image.base.digest=${base_digest}" \
--reproducible \
--time "set=${vcs_date},base-ref=${base_name}@${base_digest}"
else
regctl image mod "${local_tag}" --replace \
--reproducible \
--time "set=${vcs_date}"
fi
echo "digest=$(regctl image digest ${local_tag})" >>$GITHUB_OUTPUT
- name: Attach SBOMs
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
id: sbom
run: |
now_date="$(date +%Y-%m-%dT%H:%M:%SZ --utc)"
for digest in $(regctl manifest get ocidir://output/${{matrix.image}}:${{matrix.type}} \
--format '{{range .Manifests}}{{printf "%s\n" .Digest}}{{end}}'); do
echo "Attaching SBOMs for ${{matrix.image}}@${digest}"
regctl image copy ocidir://output/${{matrix.image}}@${digest} ocidir://output/${{matrix.image}}-sbom -v warn >/dev/null
${{steps.syft.outputs.cmd}} scan -q "oci-dir:output/${{matrix.image}}-sbom" \
--source-name "docker:docker.io/olareg/${{matrix.image}}@${digest}" -o cyclonedx-json \
| regctl artifact put --subject "ocidir://output/${{matrix.image}}@${digest}" \
--artifact-type application/vnd.cyclonedx+json \
-m application/vnd.cyclonedx+json \
--annotation "org.opencontainers.image.created=${now_date}" \
--annotation "org.opencontainers.image.description=CycloneDX JSON SBOM"
${{steps.syft.outputs.cmd}} scan -q "oci-dir:output/${{matrix.image}}-sbom" \
--source-name "docker:docker.io/olareg/${{matrix.image}}@${digest}" -o spdx-json \
| regctl artifact put --subject "ocidir://output/${{matrix.image}}@${digest}" \
--artifact-type application/spdx+json \
-m application/spdx+json \
--annotation "org.opencontainers.image.created=${now_date}" \
--annotation "org.opencontainers.image.description=SPDX JSON SBOM"
rm -r output/${{matrix.image}}-sbom
done
- name: Push and Sign
if: github.event_name != 'pull_request' && github.repository_owner == 'olareg'
id: push
run: |
# loop over the tags
image_hub="${{ steps.prep.outputs.image_hub }}"
for tag in $(echo ${{ steps.prep.outputs.tags }} | tr , ' '); do
digest="$(regctl image digest "ocidir://output/${{matrix.image}}:${{matrix.type}}")"
if [ "${digest}" = "$(regctl image digest "${tag}" 2>/dev/null || true)" ]; then
# image already pushed, don't add referrers to reproducible builds
echo "Skipping ${tag}"
else
echo "Pushing ${tag}"
regctl image copy --referrers "ocidir://output/${{matrix.image}}:${{matrix.type}}@${digest}" "${tag}"
cosign sign -y -r "${tag}@${digest}"
fi
done