Repository navigation
277 lines (243 loc) 路 9.49 KB
/
Copy pathdocker-build.yml
File metadata and controls
277 lines (243 loc) 路 9.49 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
name: Docker Build and Push
run-name: >-
${{ github.workflow }}${{ github.event_name == 'workflow_dispatch' && format(' [{0}] {1}:{2}', inputs.run_key, inputs.branch, inputs.choice) || '' }}
permissions:
contents: write
packages: write
on:
workflow_dispatch:
inputs:
branch:
type: choice
description: Target branch to build/push for
required: true
default: develop
options:
- develop
- master
choice:
type: choice
description: Trigger build and optionally bump version
required: true
default: none
options:
- none
- major
- minor
- patch
run_key:
type: string
description: Correlation id (optional)
required: false
default: ""
push:
branches:
- develop
jobs:
docker-build:
runs-on: ubuntu-latest
if: >-
(
github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/develop'
) && (
github.event_name != 'push' || github.actor != 'github-actions[bot]'
) && (
github.event_name != 'push' ||
github.event.head_commit.message == null ||
!startsWith(github.event.head_commit.message, 'ci: bump version v')
)
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.branch || github.ref }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Create and use multi-platform builder
run: |
docker buildx create --name multiarch --use --bootstrap
docker buildx inspect multiarch
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-musl,aarch64-unknown-linux-musl,wasm32-unknown-unknown
- name: Detect runner image/libc
id: runner_env
shell: bash
run: |
set -euo pipefail
echo "image_os=${ImageOS:-unknown}" >> "$GITHUB_OUTPUT"
echo "image_version=${ImageVersion:-unknown}" >> "$GITHUB_OUTPUT"
if command -v ldd >/dev/null 2>&1; then
glibc="$(ldd --version 2>/dev/null | head -n1 | awk '{print $NF}')"
else
glibc="unknown"
fi
echo "glibc=${glibc:-unknown}" >> "$GITHUB_OUTPUT"
- name: Cache cargo tools
id: cache-tools
uses: actions/cache@v4
with:
path: |
~/.cargo/bin
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-${{ steps.runner_env.outputs.image_os }}-${{ steps.runner_env.outputs.image_version }}-${{ steps.runner_env.outputs.glibc }}-cargo-tools-v2
restore-keys: |
${{ runner.os }}-${{ steps.runner_env.outputs.image_os }}-${{ steps.runner_env.outputs.image_version }}-${{ steps.runner_env.outputs.glibc }}-cargo-tools-
- name: Install cross
run: |
if ! command -v cross &> /dev/null; then
cargo install cross --git https://github.com/cross-rs/cross
fi
- name: Install trunk
run: |
if ! command -v trunk &> /dev/null; then
RUSTFLAGS="" cargo install trunk --locked
fi
- name: Install wasm-bindgen-cli
run: |
if ! command -v wasm-bindgen &> /dev/null; then
cargo install wasm-bindgen-cli
fi
- name: Install cargo-edit
run: |
if ! command -v cargo-set-version &> /dev/null; then
cargo install cargo-edit --locked
fi
- name: Cache cargo registry and dependencies
id: cache-cargo-deps
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index
~/.cargo/registry/cache
~/.cargo/git/db
key: ${{ runner.os }}-${{ steps.runner_env.outputs.image_os }}-${{ steps.runner_env.outputs.image_version }}-${{ steps.runner_env.outputs.glibc }}-cargo-deps-v2-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('**/Cargo.toml') }}
restore-keys: |
${{ runner.os }}-${{ steps.runner_env.outputs.image_os }}-${{ steps.runner_env.outputs.image_version }}-${{ steps.runner_env.outputs.glibc }}-cargo-deps-v2-${{ hashFiles('**/Cargo.lock') }}-
${{ runner.os }}-${{ steps.runner_env.outputs.image_os }}-${{ steps.runner_env.outputs.image_version }}-${{ steps.runner_env.outputs.glibc }}-cargo-deps-v2-
${{ runner.os }}-cargo-deps-
- name: Cache Docker buildx
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Cache frontend dist
id: cache-frontend
uses: actions/cache@v4
with:
path: frontend/dist
key: ${{ runner.os }}-frontend-${{ hashFiles('frontend/**/*', 'shared/**/*') }}-${{ hashFiles('frontend/Cargo.toml', 'frontend/Trunk.toml') }}
restore-keys: |
${{ runner.os }}-frontend-
- name: Cache cross-compilation tools
uses: actions/cache@v4
with:
path: |
~/.cross
~/.rustup/toolchains/*/lib/rustlib/x86_64-unknown-linux-musl
~/.rustup/toolchains/*/lib/rustlib/aarch64-unknown-linux-musl
key: ${{ runner.os }}-cross-${{ hashFiles('**/Cargo.lock') }}-v2
restore-keys: |
${{ runner.os }}-cross-
- name: Cache built resources
id: cache-resources
uses: actions/cache@v4
with:
path: |
resources/*.ts
key: ${{ runner.os }}-resources-${{ hashFiles('resources/*.jpg') }}-v1
restore-keys: |
${{ runner.os }}-resources-
- name: Install ffmpeg (only if resources not cached)
if: steps.cache-resources.outputs.cache-hit != 'true'
run: |
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Bump patch version (develop)
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
run: ./bin/inc_version.sh
- name: Set version (manual)
if: github.event_name == 'workflow_dispatch'
shell: bash
run: |
CHOICE='${{ github.event.inputs.choice }}'
case "${CHOICE:-none}" in
none) echo "Skipping version bump" ;;
major) ./bin/inc_version.sh m ;;
minor) ./bin/inc_version.sh p ;;
patch) ./bin/inc_version.sh ;;
*) echo "馃Ж Error: Unsupported option"; exit 1 ;;
esac
- name: Commit version bump (local)
id: version_commit
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
shell: bash
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
FILES=(Cargo.lock backend/Cargo.lock backend/Cargo.toml frontend/Cargo.toml shared/Cargo.toml)
for f in "${FILES[@]}"; do
if [ -f "$f" ]; then
git add "$f"
fi
done
if git diff --cached --quiet; then
echo "did_commit=false" >> "$GITHUB_OUTPUT"
exit 0
fi
VERSION="$(grep '^version' backend/Cargo.toml | head -n1 | cut -d'"' -f2)"
if [ -n "${VERSION}" ]; then
git commit -m "ci: bump version v${VERSION}"
else
git commit -m "ci: bump version"
fi
echo "did_commit=true" >> "$GITHUB_OUTPUT"
- name: Extract branch name
shell: bash
id: extract_branch
run: |
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
echo "branch=${{ inputs.branch }}" >> "$GITHUB_OUTPUT"
else
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
fi
- name: Make build script executable
run: chmod +x ./bin/build_docker.sh
- name: Build and push Docker images
env:
GITHUB_IO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO_OWNER: ${{ github.repository_owner }}
FRONTEND_CACHE_HIT: ${{ steps.cache-frontend.outputs.cache-hit }}
CARGO_DEPS_CACHE_HIT: ${{ steps.cache-cargo-deps.outputs.cache-hit }}
BUILDX_CACHE_FROM: type=local,src=/tmp/.buildx-cache
BUILDX_CACHE_TO: type=local,dest=/tmp/.buildx-cache-new,mode=max
run: ./bin/build_docker.sh ${{ steps.extract_branch.outputs.branch }}
- name: Push version bump
if: success() && steps.version_commit.outputs.did_commit == 'true'
shell: bash
run: |
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
BRANCH='${{ inputs.branch }}'
else
BRANCH="${GITHUB_REF#refs/heads/}"
fi
if [[ "${BRANCH}" != "develop" && "${BRANCH}" != "master" ]]; then
echo "Skipping push: unsupported branch '${BRANCH}'"
exit 0
fi
git push origin "HEAD:${BRANCH}"
- name: Move buildx cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache || true