-
Notifications
You must be signed in to change notification settings - Fork 2
224 lines (197 loc) · 8.46 KB
/
build-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
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
217
218
219
220
221
222
223
224
name: Build and Push Docker Image
on:
push:
branches:
- trunk
workflow_dispatch:
permissions:
contents: read
jobs:
prepare:
name: Prepare list of images to build
runs-on: ubuntu-latest
outputs:
images: ${{ steps.set-matrix.outputs.images }}
steps:
- name: Check out the source code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29
- name: Set matrix
id: set-matrix
run: echo images="$(jq '."x-build"' images/src/*/.devcontainer.json | jq --slurp -c .)" >> "${GITHUB_OUTPUT}"
publish-images:
needs: prepare
name: 'Publish ${{ matrix.image.name }}'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.prepare.outputs.images) }}
steps:
- name: Check out the repo
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
with:
fetch-depth: 0
- name: Check changed files
id: changes
run: |
if [ "${{ github.event_name }}" = "push" ]; then
if [ "${{ github.event.forced }}" = "true" ]; then
echo needs_build=true >> "${GITHUB_OUTPUT}"
else
base="${{ github.event.before }}"
head="${{ github.event.after }}"
image="images/src/${{ matrix.image.image-name }}"
changes="$(git diff --name-only "${base}" "${head}" -- "${image}" | grep -Fv "${image}/README.md" || true)"
if [ -n "${changes}" ]; then
echo needs_build=true >> "${GITHUB_OUTPUT}"
else
echo needs_build=false >> "${GITHUB_OUTPUT}"
fi
fi
else
echo needs_build=true >> "${GITHUB_OUTPUT}"
fi
- name: Expose GitHub Runtime
uses: Automattic/vip-actions/expose-github-runtime@e1faabf165941008de4c0c1381df153e49d8ad2c # v0.6.0
- name: Set up Docker
uses: crazy-max/ghaction-setup-docker@5bddaa4323ffd60efb2b5045b75b9637c12d4e50 # v3.2.0
with:
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}
- name: Set up QEMU
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0
if: steps.changes.outputs.needs_build == 'true'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0
if: steps.changes.outputs.needs_build == 'true'
- name: Log in to GitHub Docker Registry
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
if: steps.changes.outputs.needs_build == 'true'
with:
registry: https://ghcr.io
username: ${{ github.actor }}}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Lowercase repository name
run: echo "REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')" >> "${GITHUB_ENV}"
- name: Set versions
if: steps.changes.outputs.needs_build == 'true'
id: set-versions
run: |
echo major="$(echo "${{ matrix.image.image-version }}" | cut -d. -f1)" >> "${GITHUB_OUTPUT}"
echo minor="$(echo "${{ matrix.image.image-version }}" | cut -d. -f2)" >> "${GITHUB_OUTPUT}"
echo patch="$(echo "${{ matrix.image.image-version }}" | cut -d. -f3)" >> "${GITHUB_OUTPUT}"
- name: Check if image already exists
if: steps.changes.outputs.needs_build == 'true'
id: exists
run: |
if docker buildx imagetools inspect "ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}:${{ steps.set-versions.outputs.major }}.${{ steps.set-versions.outputs.minor }}.${{ steps.set-versions.outputs.patch }}" > /dev/null 2>&1; then
echo exists=true >> "${GITHUB_OUTPUT}"
else
echo exists=false >> "${GITHUB_OUTPUT}"
fi
- name: Install @devcontainers/cli
run: npm install -g @devcontainers/cli
if: ${{ steps.changes.outputs.needs_build == 'true' && steps.exists.outputs.exists != 'true' }}
- name: Build image
run: |
devcontainer build \
--workspace-folder images/src/${{ matrix.image.image-name }} \
--platform linux/amd64,linux/arm64 \
--image-name=ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}:${{ steps.set-versions.outputs.major }}.${{ steps.set-versions.outputs.minor }}.${{ steps.set-versions.outputs.patch }} \
--image-name=ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}:${{ steps.set-versions.outputs.major }}.${{ steps.set-versions.outputs.minor }} \
--image-name=ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}:${{ steps.set-versions.outputs.major }} \
--image-name=ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}:latest \
--output type=docker \
--cache-from type=gha \
--cache-to type=gha,mode=max
if: ${{ steps.changes.outputs.needs_build == 'true' && steps.exists.outputs.exists != 'true' }}
- name: Publish image
run: docker push --all-tags ghcr.io/${{ env.REPO }}/${{ matrix.image.image-name }}
if: ${{ steps.changes.outputs.needs_build == 'true' && steps.exists.outputs.exists != 'true' }}
publish-features:
name: Publish features
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check out the repo
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
with:
fetch-depth: 0
- name: Check changed files
id: changes
run: |
if [ "${{ github.event_name }}" = "push" ]; then
base="${{ github.event.before }}"
head="${{ github.event.after }}"
path="features/src"
changes="$(git diff --name-only "${base}" "${head}" -- "${path}" | grep -Fv "/README.md" || true)"
if [ -n "${changes}" ]; then
echo needs_build=true >> "${GITHUB_OUTPUT}"
else
echo needs_build=false >> "${GITHUB_OUTPUT}"
fi
else
echo needs_build=true >> "${GITHUB_OUTPUT}"
fi
- name: Log in to GitHub Docker Registry
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
with:
registry: https://ghcr.io
username: ${{ github.actor }}}
password: ${{ secrets.GITHUB_TOKEN }}
if: steps.changes.outputs.needs_build == 'true'
- name: Install @devcontainers/cli
run: npm install -g @devcontainers/cli
if: steps.changes.outputs.needs_build == 'true'
- name: Publish features
run: devcontainer features publish features/src --namespace "${{ github.repository }}"
if: steps.changes.outputs.needs_build == 'true'
publish-templates:
name: Publish templates
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check out the repo
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
with:
fetch-depth: 0
- name: Check changed files
id: changes
run: |
if [ "${{ github.event_name }}" = "push" ]; then
base="${{ github.event.before }}"
head="${{ github.event.after }}"
path="templates/src"
changes="$(git diff --name-only "${base}" "${head}" -- "${path}" | grep -Fv "/README.md" || true)"
if [ -n "${changes}" ]; then
echo needs_build=true >> "${GITHUB_OUTPUT}"
else
echo needs_build=false >> "${GITHUB_OUTPUT}"
fi
else
echo needs_build=true >> "${GITHUB_OUTPUT}"
fi
- name: Log in to GitHub Docker Registry
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
with:
registry: https://ghcr.io
username: ${{ github.actor }}}
password: ${{ secrets.GITHUB_TOKEN }}
if: steps.changes.outputs.needs_build == 'true'
- name: Install @devcontainers/cli
run: npm install -g @devcontainers/cli
if: steps.changes.outputs.needs_build == 'true'
- name: Publish templates
run: devcontainer templates publish templates/src --namespace "${{ github.repository }}"
if: steps.changes.outputs.needs_build == 'true'