-
Notifications
You must be signed in to change notification settings - Fork 44
369 lines (312 loc) · 11.3 KB
/
Copy pathbuild.yml
File metadata and controls
369 lines (312 loc) · 11.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
name: Build and release
on:
pull_request:
branches: [ "main" ]
push:
branches: [ "main" ]
permissions:
contents: write
jobs:
check-version:
name: Check version
runs-on: ubuntu-24.04
outputs:
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.version.outputs.tag }}
release_exists: ${{ steps.release_check.outputs.release_exists }}
should_release: ${{ steps.decide.outputs.should_release }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Read version
id: version
shell: bash
run: |
read_version() {
awk -F' *= *' '$1 == "version" { gsub(/"/, "", $2); print $2; exit }' "$1"
}
mailcrab_version=$(read_version mailcrab/Cargo.toml)
backend_version=$(read_version backend/Cargo.toml)
frontend_version=$(read_version frontend/Cargo.toml)
if [[ "$mailcrab_version" != "$backend_version" || "$mailcrab_version" != "$frontend_version" ]]; then
echo "Cargo package versions must stay in sync" >&2
echo "mailcrab/Cargo.toml: $mailcrab_version" >&2
echo "backend/Cargo.toml: $backend_version" >&2
echo "frontend/Cargo.toml: $frontend_version" >&2
exit 1
fi
echo "version=$mailcrab_version" >> "$GITHUB_OUTPUT"
echo "tag=v$mailcrab_version" >> "$GITHUB_OUTPUT"
- name: Check release existence
id: release_check
uses: actions/github-script@v7
with:
script: |
const tag = '${{ steps.version.outputs.tag }}';
try {
await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag,
});
core.setOutput('release_exists', 'true');
} catch (error) {
if (error.status === 404) {
core.setOutput('release_exists', 'false');
} else {
throw error;
}
}
- name: Check tag state
id: tag_check
shell: bash
env:
TAG_NAME: ${{ steps.version.outputs.tag }}
run: |
if git rev-parse -q --verify "refs/tags/$TAG_NAME" >/dev/null; then
tag_commit=$(git rev-list -n 1 "$TAG_NAME")
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
if [[ "$tag_commit" == "$GITHUB_SHA" ]]; then
echo "tag_matches_head=true" >> "$GITHUB_OUTPUT"
else
echo "tag_matches_head=false" >> "$GITHUB_OUTPUT"
fi
else
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
echo "tag_matches_head=false" >> "$GITHUB_OUTPUT"
fi
- name: Decide release
id: decide
shell: bash
env:
RELEASE_EXISTS: ${{ steps.release_check.outputs.release_exists }}
TAG_EXISTS: ${{ steps.tag_check.outputs.tag_exists }}
TAG_MATCHES_HEAD: ${{ steps.tag_check.outputs.tag_matches_head }}
run: |
should_release=false
if [[ "$GITHUB_EVENT_NAME" == "push" && "$GITHUB_REF" == "refs/heads/main" ]]; then
if [[ "$RELEASE_EXISTS" == "true" ]]; then
should_release=false
elif [[ "$TAG_EXISTS" == "false" || "$TAG_MATCHES_HEAD" == "true" ]]; then
should_release=true
else
echo "${{ steps.version.outputs.tag }} exists on a different commit and has no release" >&2
exit 1
fi
fi
echo "should_release=$should_release" >> "$GITHUB_OUTPUT"
frontend:
name: Build frontend assets
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- run: rustup toolchain install stable --profile minimal
- run: rustup target add wasm32-unknown-unknown
- uses: jetli/trunk-action@v0.5.0
with:
version: 'latest'
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
frontend/target/
key: frontend-${{ hashFiles('frontend/Cargo.toml') }}
restore-keys: frontend-
- name: Build frontend
run: trunk build
working-directory: frontend
- uses: actions/upload-artifact@v4
with:
name: frontend-build
path: frontend/dist
build:
name: Binaries for ${{ matrix.name }}
needs:
- check-version
- frontend
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- name: linux-x86-64-gnu
target: x86_64-unknown-linux-gnu
os: ubuntu-24.04
artifact: mailcrab-linux-x86-64-gnu
- name: linux-armv7-gnu
target: armv7-unknown-linux-gnueabihf
os: ubuntu-24.04
artifact: mailcrab-linux-armv7-gnu
- name: linux-arm64-gnu
target: aarch64-unknown-linux-gnu
os: ubuntu-24.04
artifact: mailcrab-linux-arm64-gnu
- name: linux-x86-64-musl
target: x86_64-unknown-linux-musl
os: ubuntu-24.04
artifact: mailcrab-linux-x86-64-musl
- name: linux-arm64-musl
target: aarch64-unknown-linux-musl
os: ubuntu-24.04
artifact: mailcrab-linux-arm64-musl
- name: osx-arm64
target: aarch64-apple-darwin
os: macos-latest
artifact: mailcrab-osx-arm64
steps:
- uses: actions/checkout@v4
- run: rustup toolchain install stable --profile minimal
- name: Add Rust target
if: ${{ runner.os == 'macOS' }}
run: rustup target add ${{ matrix.target }}
- uses: actions/download-artifact@v4
with:
name: frontend-build
path: frontend/dist
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
backend/target/
key: backend-${{ matrix.name }}-${{ hashFiles('backend/Cargo.toml') }}
restore-keys: backend-${{ matrix.name }}-
- if: ${{ runner.os == 'Linux' }}
run: cargo install cross --git https://github.com/cross-rs/cross || true
- name: Cross build
if: ${{ runner.os == 'Linux' }}
run: cross build --release --locked --target ${{ matrix.target }} --manifest-path backend/Cargo.toml
- name: Cargo build
if: ${{ runner.os == 'macOS' }}
run: cargo build --release --locked --target ${{ matrix.target }} --manifest-path backend/Cargo.toml
- name: Package binary
if: ${{ needs.check-version.outputs.should_release == 'true' }}
shell: bash
run: |
mkdir -p bin
cp backend/target/${{ matrix.target }}/release/mailcrab-backend bin/${{ matrix.artifact }}
- uses: actions/upload-artifact@v4
if: ${{ needs.check-version.outputs.should_release == 'true' }}
with:
name: ${{ matrix.artifact }}
path: bin/${{ matrix.artifact }}
publish-crates:
name: Publish crates.io packages
needs:
- check-version
- build
if: ${{ needs.check-version.outputs.should_release == 'true' }}
runs-on: ubuntu-24.04
environment: production
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- run: rustup toolchain install stable --profile minimal
- name: Authenticate with crates.io
id: crates_io_auth
uses: rust-lang/crates-io-auth-action@v1
- name: Publish crates
env:
CARGO_REGISTRY_TOKEN: ${{ steps.crates_io_auth.outputs.token }}
run: cargo publish --locked --manifest-path mailcrab/Cargo.toml
docker:
name: Docker image
needs:
- check-version
- build
- publish-crates
if: ${{ needs.check-version.outputs.should_release == 'true' }}
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Setup docker buildx
uses: docker/setup-buildx-action@v2
- name: Login to dockerhub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- uses: actions/download-artifact@v4
with:
name: mailcrab-linux-x86-64-musl
path: bin
- uses: actions/download-artifact@v4
with:
name: mailcrab-linux-arm64-musl
path: bin
- name: Prepare docker binaries
shell: bash
run: |
mv bin/mailcrab-linux-x86-64-musl bin/amd64
mv bin/mailcrab-linux-arm64-musl bin/arm64
- name: Build docker image
run: docker buildx build --push --platform=linux/amd64,linux/arm64 . -t marlonb/mailcrab:latest -t marlonb/mailcrab:${{ needs.check-version.outputs.tag }}
release:
name: GitHub release
needs:
- check-version
- build
- publish-crates
- docker
if: ${{ needs.check-version.outputs.should_release == 'true' }}
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@v4
with:
name: mailcrab-linux-x86-64-gnu
path: dist
- uses: actions/download-artifact@v4
with:
name: mailcrab-linux-armv7-gnu
path: dist
- uses: actions/download-artifact@v4
with:
name: mailcrab-linux-arm64-gnu
path: dist
- uses: actions/download-artifact@v4
with:
name: mailcrab-osx-arm64
path: dist
- name: Prepare release files
shell: bash
env:
TAG_NAME: ${{ needs.check-version.outputs.tag }}
run: |
mkdir -p release
for name in \
linux-x86-64-gnu \
linux-armv7-gnu \
linux-arm64-gnu \
osx-arm64
do
src="dist/mailcrab-$name"
dst="release/mailcrab-$name-$TAG_NAME"
cp "$src" "$dst"
sha256sum -b "$dst" > "$dst.sha256"
done
- name: Create release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ needs.check-version.outputs.tag }}
target_commitish: ${{ github.sha }}
draft: true
append_body: true
body: |
Docker releases: https://hub.docker.com/repository/docker/marlonb/mailcrab/tags
Run docker image using: `docker run --rm -p 1080:1080 -p 1025:1025 marlonb/mailcrab:${{ needs.check-version.outputs.tag }}`
files: |
release/mailcrab-linux-x86-64-gnu-${{ needs.check-version.outputs.tag }}
release/mailcrab-linux-x86-64-gnu-${{ needs.check-version.outputs.tag }}.sha256
release/mailcrab-linux-armv7-gnu-${{ needs.check-version.outputs.tag }}
release/mailcrab-linux-armv7-gnu-${{ needs.check-version.outputs.tag }}.sha256
release/mailcrab-linux-arm64-gnu-${{ needs.check-version.outputs.tag }}
release/mailcrab-linux-arm64-gnu-${{ needs.check-version.outputs.tag }}.sha256
release/mailcrab-osx-arm64-${{ needs.check-version.outputs.tag }}
release/mailcrab-osx-arm64-${{ needs.check-version.outputs.tag }}.sha256