-
Notifications
You must be signed in to change notification settings - Fork 9
188 lines (163 loc) · 7.19 KB
/
container-push.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
name: Container Push
on:
workflow_call:
inputs:
name:
description: 'A short name for the container image. Will be used to create the image path and set the title.'
required: true
type: string
tag:
description: 'Container image tag to push. Normally it will be the GITHUB_REF_NAME env variable'
required: true
type: string
latest:
description: 'Denotes if the `latest` tag should be generated or not'
required: false
type: boolean
default: false
registry_org:
description: 'The registry organization to push to'
default: '${{ github.repository_owner }}'
required: false
type: string
dockerfile_path:
description: 'The relative path to the Dockerfile to build.'
default: 'Dockerfile'
required: false
type: string
build_context:
description: 'The path to the context to build the container from.'
default: '.'
required: false
type: string
licenses:
description: 'The licenses under which the container is distributed.'
default: 'Apache-2.0'
required: false
type: string
vendor:
description: 'The vendor of the container.'
default: 'Equinix, Inc.'
required: false
type: string
platforms:
description: 'The platforms to build the container for.'
default: 'linux/amd64'
required: false
type: string
env:
COSIGN_EXPERIMENTAL: 1
jobs:
container:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
outputs:
image-digest: ${{ steps.container_info.outputs.image-digest }}
image-tags: ${{ steps.container_info.outputs.image-tags }}
steps:
- name: Checkout
uses: actions/[email protected]
- name: Login to ghcr.io
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get input with Github Environment Variable reference
id: get_inputs
run: |
echo "name=${{ inputs.name }}" >> $GITHUB_OUTPUT
echo "tag=${{ inputs.tag }}" >> $GITHUB_OUTPUT
echo "latest=${{ inputs.latest }}" >> $GITHUB_OUTPUT
echo "registry_org=${{ inputs.registry_org }}" >> $GITHUB_OUTPUT
echo "dockerfile_path=${{ inputs.dockerfile_path }}" >> $GITHUB_OUTPUT
echo "build_context=${{ inputs.build_context }}" >> $GITHUB_OUTPUT
echo "licenses=${{ inputs.licenses }}" >> $GITHUB_OUTPUT
echo "vendor=${{ inputs.vendor }}" >> $GITHUB_OUTPUT
echo "platforms=${{ inputs.platforms }}" >> $GITHUB_OUTPUT
echo "github_server_url=${GITHUB_SERVER_URL}" >> $GITHUB_OUTPUT
echo "github_repository=${GITHUB_REPOSITORY}" >> $GITHUB_OUTPUT
- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ steps.get_inputs.outputs.registry_org }}/${{ steps.get_inputs.outputs.name }}
tags: |
type=raw,value=${{ steps.get_inputs.outputs.tag}}
type=sha,format=long
type=raw,value=latest,enable=${{ steps.get_inputs.outputs.latest }}
labels: |
org.opencontainers.image.source=${{ steps.get_inputs.outputs.github_server_url }}/${{ steps.get_inputs.outputs.github_repository }}
org.opencontainers.image.title=${{ steps.get_inputs.outputs.name }}
org.opencontainers.image.version=${{ steps.get_inputs.outputs.tag }}
org.opencontainers.image.licenses=${{ steps.get_inputs.outputs.licenses }}
org.opencontainers.image.vendor=${{ steps.get_inputs.outputs.vendor }}
- name: Build container images and push
id: docker_build
uses: docker/build-push-action@v5
with:
context: ${{ steps.get_inputs.outputs.build_context }}
file: ${{ steps.get_inputs.outputs.dockerfile_path }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: true
platforms: ${{ steps.get_inputs.outputs.platforms }}
- name: Get container info
id: container_info
run: |
image_tags="${{ steps.get_inputs.outputs.tag }},sha-$(git rev-parse HEAD)"
echo "image-digest=${{ steps.docker_build.outputs.digest }}" >> $GITHUB_OUTPUT
echo "image-tags=${image_tags}" >> $GITHUB_OUTPUT
sign:
runs-on: ubuntu-latest
needs: [container]
permissions:
packages: write
id-token: write
env:
IMAGE_DIGEST: ${{ needs.container.outputs.image-digest }}
steps:
- name: Install cosign
uses: sigstore/[email protected]
- name: Login to ghcr.io
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Sign image
run: |
cosign sign -y "ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST}"
echo "::notice title=Verify signature::COSIGN_EXPERIMENTAL=1 cosign verify ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST} | jq '.[0]'"
echo "::notice title=Inspect signature bundle::COSIGN_EXPERIMENTAL=1 cosign verify ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST} | jq '.[0].optional.Bundle.Payload.body |= @base64d | .[0].optional.Bundle.Payload.body | fromjson'"
echo "::notice title=Inspect certificate::COSIGN_EXPERIMENTAL=1 cosign verify ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST} | jq -r '.[0].optional.Bundle.Payload.body |= @base64d | .[0].optional.Bundle.Payload.body | fromjson | .spec.signature.publicKey.content |= @base64d | .spec.signature.publicKey.content' | openssl x509 -text"
sbom:
runs-on: ubuntu-latest
needs: [container]
permissions:
packages: write
id-token: write
env:
IMAGE_DIGEST: ${{ needs.container.outputs.image-digest }}
steps:
- name: Install cosign
uses: sigstore/[email protected]
- name: Install Syft
uses: anchore/sbom-action/[email protected]
- name: Login to ghcr.io
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Attach SBOM to image
run: |
syft "ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST}" -o spdx-json=sbom-spdx.json
cosign attest -y --predicate sbom-spdx.json --type spdx "ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST}"
echo "::notice title=Verify SBOM attestation::COSIGN_EXPERIMENTAL=1 cosign verify-attestation ghcr.io/${{ inputs.registry_org }}/${{ inputs.name }}@${IMAGE_DIGEST} | jq '.payload |= @base64d | .payload | fromjson | select(.predicateType == \"https://spdx.dev/Document\") | .predicate.Data | fromjson'"