-
Notifications
You must be signed in to change notification settings - Fork 0
216 lines (187 loc) · 7.29 KB
/
build.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
213
214
215
216
name: Build and push container images for Specific Ruby Version
on:
workflow_dispatch:
workflow_call:
inputs:
ruby:
description: 'Ruby version'
required: true
type: string
env:
COSIGN_EXPERIMENTAL: 1
REGISTRY_IMAGE: zenjoy/ruby
DEBIAN: bullseye
jobs:
build:
name: Build Ruby ${{ inputs.ruby }} for ${{ matrix.platform }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, ubuntu-22.04-arm-2-core]
include:
- os: ubuntu-latest
platform: amd64
- os: ubuntu-22.04-arm-2-core
platform: arm64
permissions:
id-token: write # keyless Cosign signatures
packages: write # GHCR
contents: write # git tags
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Parse ruby version
id: prepare
run: |
RUBY_VERSION=$(grep -Eo 'RUBY_VERSION ${{ inputs.ruby }}.[0-9]+$' Dockerfile | cut -d ' ' -f2)
MAJOR=$(echo $RUBY_VERSION | cut -d . -f 1)
MINOR=$(echo $RUBY_VERSION | cut -d . -f 2)
PATCH=$(echo $RUBY_VERSION | cut -d . -f 3)
TODAY=$(date +%Y-%m-%d)
echo "major=$MAJOR" | tee -a $GITHUB_OUTPUT
echo "minor=$MINOR" | tee -a $GITHUB_OUTPUT
echo "patch=$PATCH" | tee -a $GITHUB_OUTPUT
echo "version=$RUBY_VERSION" | tee -a $GITHUB_OUTPUT
echo "today=${TODAY}" | tee -a $GITHUB_OUTPUT
- name: Generate build tag from head
id: branch
run: |
export GIT_REF=${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}
echo "branch=$(echo ${GIT_REF,,} | sed 's/[^a-zA-Z0-9]/-/g')" | tee -a $GITHUB_OUTPUT
- name: Generate Docker metadata with ruby version
uses: docker/metadata-action@v5
id: metadata
with:
flavor: |
latest=${{ matrix.ruby == '3.3' }}
images: |
docker.io/${{ env.REGISTRY_IMAGE }}
ghcr.io/${{ env.REGISTRY_IMAGE }}
tags: |
type=semver,pattern={{version}},value=v${{ steps.prepare.outputs.version }}
type=semver,pattern={{major}}.{{minor}},value=v${{ steps.prepare.outputs.version }}
labels: |
org.opencontainers.image.title=ruby
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Login to GitHub Container Repository
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build container image
uses: docker/build-push-action@v6
id: build
with:
labels: ${{ steps.metadata.outputs.labels }}
build-args: |
BUILD_AT=${{ steps.prepare.outputs.today }}
RUBY=${{ inputs.ruby }}
DEBIAN=bullseye
platforms: linux/${{ matrix.platform }}
outputs: |
type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
cache-from: |
type=registry,ref=${{ env.REGISTRY_IMAGE }}:buildcache-${{ matrix.platform }}-${{ inputs.ruby }}
cache-to: |
type=registry,ref=${{ env.REGISTRY_IMAGE }}:buildcache-${{ matrix.platform }}-${{ inputs.ruby }},mode=max
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ inputs.ruby }}-${{ env.DEBIAN }}-${{ matrix.platform }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
name: Generating Manifest for Ruby ${{ inputs.ruby }}
runs-on: ubuntu-latest
needs:
- build
permissions:
id-token: write # keyless Cosign signatures
packages: write # GHCR
contents: write # git tags
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install Cosign
uses: sigstore/[email protected]
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-${{ inputs.ruby }}-${{ env.DEBIAN }}-*
merge-multiple: true
- name: Parse ruby version
id: prepare
run: |
RUBY_VERSION=$(grep -Eo 'RUBY_VERSION ${{ inputs.ruby }}.[0-9]+$' Dockerfile | cut -d ' ' -f2)
MAJOR=$(echo $RUBY_VERSION | cut -d . -f 1)
MINOR=$(echo $RUBY_VERSION | cut -d . -f 2)
PATCH=$(echo $RUBY_VERSION | cut -d . -f 3)
TODAY=$(date +%Y-%m-%d)
echo "major=$MAJOR" | tee -a $GITHUB_OUTPUT
echo "minor=$MINOR" | tee -a $GITHUB_OUTPUT
echo "patch=$PATCH" | tee -a $GITHUB_OUTPUT
echo "version=$RUBY_VERSION" | tee -a $GITHUB_OUTPUT
echo "today=${TODAY}" | tee -a $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Docker metadata with ruby version
uses: docker/metadata-action@v5
id: metadata
with:
flavor: |
latest=${{ inputs.ruby == '3.3' }}
images: |
docker.io/${{ env.REGISTRY_IMAGE }}
ghcr.io/${{ env.REGISTRY_IMAGE }}
tags: |
type=semver,pattern={{version}},value=v${{ steps.prepare.outputs.version }}
type=semver,pattern={{major}}.{{minor}},value=v${{ steps.prepare.outputs.version }}
labels: |
org.opencontainers.image.title=ruby
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Repository
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
id: inspect
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.metadata.outputs.version }}
DIGEST=$(docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.metadata.outputs.version }} | grep Digest | awk '{print $2}')
echo "IMAGE_DIGEST=$DIGEST" >> $GITHUB_OUTPUT
echo "Image digest: $DIGEST"
- name: Sign container images
run: |
cosign sign --yes --recursive "docker.io/$IMAGE_NAME@$IMAGE_DIGEST"
cosign sign --yes --recursive "ghcr.io/$IMAGE_NAME@$IMAGE_DIGEST"
env:
IMAGE_NAME: ${{ env.REGISTRY_IMAGE }}
IMAGE_DIGEST: ${{ steps.inspect.outputs.IMAGE_DIGEST }}