-
Notifications
You must be signed in to change notification settings - Fork 4
598 lines (494 loc) · 20.9 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
# SPDX-FileContributor: Sebastian Thomschke (Vegard IT GmbH)
# SPDX-License-Identifier: Apache-2.0
# SPDX-ArtifactOfProjectHomePage: https://github.com/vegardit/copycat
#
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
name: Build
on:
push:
branches-ignore: # build all branches except:
- 'dependabot/**' # prevent GHA triggered twice (once for commit to the branch and once for opening/syncing the PR)
tags-ignore: # don't build tags
- '**'
paths-ignore:
- '**/*.adoc'
- '**/*.md'
- '.editorconfig'
- '.git*'
- '.github/*.yml'
pull_request:
paths-ignore:
- '**/*.adoc'
- '**/*.md'
- '.editorconfig'
- '.git*'
- '.github/*.yml'
workflow_dispatch:
# https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
inputs:
additional_maven_args:
description: 'Additional Maven Args'
required: false
default: ''
defaults:
run:
shell: bash
env:
# TODO don't upgrade as newer versions break Windows builds https://github.com/oracle/graal/issues/4340
GRAALVM_VERSION: 21.3.3.1 # https://github.com/graalvm/graalvm-ce-builds/releases
JAVA_VERSION: 17
RELEASE_NAME: "snapshot"
jobs:
###########################################################
maven-build:
###########################################################
runs-on: ubuntu-latest
steps:
- name: Show environment variables
run: env | sort
- name: Git Checkout
uses: actions/checkout@v4 # https://github.com/actions/checkout
- name: "Install: JDK ${{ env.JAVA_VERSION }}"
uses: actions/setup-java@v4 # https://github.com/actions/setup-java
with:
distribution: temurin
java-version: ${{ env.JAVA_VERSION }}
- name: "Cache: Restore"
id: cache-restore
if: ${{ !env.ACT }} # https://github.com/nektos/act#skipping-steps
uses: actions/cache/restore@v3
with:
path: |
~/.m2/bin
~/.m2/repository
!~/.m2/repository/com/vegardit/copycat
key: ${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
- name: Test with Maven
if: ${{ github.ref_name != 'main' || env.ACT }}
env:
GITHUB_USER: ${{ github.actor }}
GITHUB_API_KEY: ${{ github.token }}
MAY_CREATE_RELEASE: false
run: |
bash .ci/build.sh ${{ github.event.inputs.additional_maven_args }}
- name: Build with Maven
if: ${{ github.ref_name == 'main' && !env.ACT }}
env:
GITHUB_USER: ${{ github.actor }}
GITHUB_API_KEY: ${{ secrets.GH_API_TOKEN }}
MAY_CREATE_RELEASE: true
run: |
set -eu
# https://github.community/t/github-actions-bot-email-address/17204
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
bash .ci/build.sh ${{ github.event.inputs.additional_maven_args }}
- name: Test minimized fat jar
run: |
set -ex
mv target/copycat-*-fat-minimized.jar target/copycat-fat.jar
java -jar target/copycat-fat.jar --help
java -jar target/copycat-fat.jar -V
java -jar target/copycat-fat.jar sync target/classes target/classes.copy -v
- name: "Share: maven-build-artifacts"
uses: actions/upload-artifact@v4
with:
name: maven-build-artifacts
path: |
target/copycat-fat.jar
target/picocli-reflections.json
target/bash/bashcompletion.sh
##################################################
# Cache Update
# See https://github.com/actions/cache/issues/342
##################################################
- name: "Cache: Delete Previous"
if: ${{ steps.cache-restore.outputs.cache-hit && !env.ACT }}
env:
GH_TOKEN: ${{ github.token }}
run: |
gh extension install actions/gh-actions-cache
# "|| true" is to avoid "Error: Resource not accessible by integration" from failing the job
gh actions-cache delete ${{ steps.cache-restore.outputs.cache-primary-key }} --confirm || true
- name: "Cache: Update"
uses: actions/cache/save@v3
if: ${{ always() && !cancelled() && !env.ACT }} # save cache even fails
with:
path: |
~/.m2/bin
~/.m2/repository
!~/.m2/repository/com/vegardit/maven
!~/.m2/repository/*SNAPSHOT*
key: ${{ steps.cache-restore.outputs.cache-primary-key }}
###########################################################
native-image:
###########################################################
runs-on: ${{ matrix.os }}
needs: [maven-build]
strategy:
fail-fast: false
matrix:
include:
- arch: "linux-amd64"
os: "ubuntu-latest"
graal_install_root: "/opt/graalvm"
- arch: "linux-arm64"
os: "ubuntu-latest"
graal_install_root: "/opt/graalvm"
- arch: "darwin-amd64"
os: "macos-latest"
graal_install_root: "graalvm"
- arch: "windows-amd64"
os: "windows-latest"
graal_install_root: "graalvm"
env:
# https://www.graalvm.org/21.3/reference-manual/native-image/Options/
# -H:NativeLinkerOption=-no-pie -> do not to generate Position Independent Executables (PIE)
NATIVE_IMAGE_ARGS:
-H:NativeLinkerOption=-no-pie
-H:ReflectionConfigurationFiles=picocli-reflections.json
-H:Log=registerResource:3
-H:+ReportExceptionStackTraces
-H:+RemoveUnusedSymbols
-H:ExcludeResources=com/sun/.*.properties
--exclude-config copycat-fat.jar META-INF/native-image/net.sf.jstuff/.*.json
--exclude-config copycat-fat.jar META-INF/native-image/jansi/.*.json
--allow-incomplete-classpath
--no-fallback
--no-server
--verbose
-Dfile.encoding=UTF-8
--class-path native-image-metadata
--class-path copycat-fat.jar
com.vegardit.copycat.CopyCatMain
copycat-${{ matrix.arch }}
steps:
- name: "Cache: GraalVM binaries"
uses: actions/cache@v3
with:
path: |
${{ matrix.graal_install_root }}
key: ${{ matrix.arch }}-graalvm-${{ env.GRAALVM_VERSION }}-java-${{ env.JAVA_VERSION }}
- name: "Create arm64 container"
if: matrix.arch == 'linux-arm64'
run: |
sudo apt-get update -y
sudo apt-get install --no-install-recommends -y qemu-user-static
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker container create --name arm64 --tty \
-v /opt:/opt:rw \
-v $PWD:/workspace:rw \
arm64v8/ubuntu:22.04
docker container start arm64
docker exec --tty arm64 apt-get update -y
docker exec --tty arm64 apt-get install -y curl sudo
- name: "Install: GraalVM"
run: |
set -eu
case "${{ matrix.arch }}" in
linux-amd64)
sudo apt-get install --no-install-recommends -y gcc g++ libz-dev
GRAAL_VM_HOME=${{ matrix.graal_install_root }}
if [ ! -e $GRAAL_VM_HOME/bin/native-image ]; then
rm -rf $GRAAL_VM_HOME
mkdir -p $GRAAL_VM_HOME
curl -fL "https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${{ env.GRAALVM_VERSION }}/graalvm-ce-java${{ env.JAVA_VERSION}}-linux-amd64-${{ env.GRAALVM_VERSION }}.tar.gz" \
| tar zxv -C $GRAAL_VM_HOME --strip-components=1 \
--exclude=*/bin/jvisualvm \
--exclude=*/lib/src.zip \
--exclude=*/lib/visualvm
$GRAAL_VM_HOME/bin/gu install native-image
fi
$GRAAL_VM_HOME/bin/native-image --version
echo "JAVA_HOME=$GRAAL_VM_HOME" >> $GITHUB_ENV
echo "$GRAAL_VM_HOME/bin" >> $GITHUB_PATH
;;
linux-arm64)
cat <<EOF > install_graalvm.sh
set -eu
sudo apt-get install --no-install-recommends -y gcc g++ libz-dev
GRAAL_VM_HOME=${{ matrix.graal_install_root }}
if [ ! -e \$GRAAL_VM_HOME/bin/native-image ]; then
rm -rf \$GRAAL_VM_HOME
mkdir -p \$GRAAL_VM_HOME
curl -fL "https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${{ env.GRAALVM_VERSION }}/graalvm-ce-java${{ env.JAVA_VERSION }}-linux-aarch64-${{ env.GRAALVM_VERSION }}.tar.gz" \
| tar zxv -C \$GRAAL_VM_HOME --strip-components=1 \
--exclude=*/bin/jvisualvm \
--exclude=*/lib/src.zip \
--exclude=*/lib/visualvm
\$GRAAL_VM_HOME/bin/gu install native-image
fi
\$GRAAL_VM_HOME/bin/native-image --version
EOF
docker exec --tty -w /workspace arm64 /bin/bash ./install_graalvm.sh
;;
darwin-amd64)
GRAAL_VM_HOME=${{ matrix.graal_install_root }}/Contents/Home
if [ ! -e $GRAAL_VM_HOME/bin/native-image ]; then
rm -rf ${{ matrix.graal_install_root }}
mkdir ${{ matrix.graal_install_root }}
curl -fL "https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${{ env.GRAALVM_VERSION }}/graalvm-ce-java${{ env.JAVA_VERSION }}-darwin-amd64-${{ env.GRAALVM_VERSION }}.tar.gz" \
| tar zxv -C graalvm --strip-components=1 \
--exclude=*/bin/jvisualvm \
--exclude=*/lib/src.zip \
--exclude=*/lib/visualvm
$GRAAL_VM_HOME/bin/gu install native-image
fi
$GRAAL_VM_HOME/bin/native-image --version
echo "JAVA_HOME=$GRAAL_VM_HOME" >> $GITHUB_ENV
echo "$GRAAL_VM_HOME/bin" >> $GITHUB_PATH
;;
windows-amd64)
GRAAL_VM_HOME=${{ matrix.graal_install_root }}
if [ ! -e $GRAAL_VM_HOME/bin/native-image ]; then
rm -rf $GRAAL_VM_HOME
echo "Downloading GraalVM..."
curl -fL -o graalvm.zip https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${{ env.GRAALVM_VERSION }}/graalvm-ce-java${{ env.JAVA_VERSION }}-windows-amd64-${{ env.GRAALVM_VERSION }}.zip
echo "Extracting GraalVM..."
7z x graalvm.zip
mv graalvm-ce-* $GRAAL_VM_HOME
$GRAAL_VM_HOME/bin/gu.cmd install native-image
fi
$GRAAL_VM_HOME/bin/native-image.cmd --version
echo "JAVA_HOME=$(cygpath -wa $GRAAL_VM_HOME)" >> $GITHUB_ENV
echo "$(cygpath -wa $GRAAL_VM_HOME/bin)" >> $GITHUB_PATH
;;
esac
- name: "Install: binutils (strip)"
run: |
case "${{ matrix.arch }}" in
linux-amd64)
sudo apt-get --no-install-recommends install -y binutils
;;
linux-arm64)
docker exec --tty arm64 \
sudo apt-get --no-install-recommends install -y binutils
;;
esac
- name: "Install: UPX"
run: |
case "${{ matrix.arch }}" in
linux-amd64)
mkdir /opt/upx
upx_download_url=$(curl -fsSL -H "Authorization: token ${{ github.token }}" https://api.github.com/repos/upx/upx/releases/latest | grep browser_download_url | grep amd64_linux.tar.xz | cut "-d\"" -f4)
echo "Downloading [$upx_download_url]..."
curl -fL $upx_download_url | tar Jxv -C /opt/upx --strip-components=1
;;
linux-arm64)
mkdir /opt/upx
upx_download_url=$(curl -fsSL -H "Authorization: token ${{ github.token }}" https://api.github.com/repos/upx/upx/releases/latest | grep browser_download_url | grep arm64_linux.tar.xz | cut "-d\"" -f4)
echo "Downloading [$upx_download_url]..."
curl -fL $upx_download_url | tar Jxv -C /opt/upx --strip-components=1
;;
darwin-amd64)
brew install upx
;;
windows-amd64)
# first choco install in a build takes 3+ minutes
# choco install -y upx
upx_download_url=$(curl -fsSL -H "Authorization: token ${{ github.token }}" https://api.github.com/repos/upx/upx/releases/latest | grep browser_download_url | grep win64.zip | cut "-d\"" -f4)
echo "Downloading [$upx_download_url]..."
curl -fL -o /tmp/upx.zip $upx_download_url
echo "Extracting upx.zip..."
mkdir /tmp/upx
7z e /tmp/upx.zip -o/tmp/upx *.exe -r
echo "$(cygpath -wa /tmp/upx)" >> $GITHUB_PATH
;;
esac
- name: "Get: maven-build-artifacts"
uses: actions/download-artifact@v4
with:
name: maven-build-artifacts
- name: Enable Developer Command Prompt
if: matrix.arch == 'windows-amd64'
# makes cl.exe available on PATH
# https://github.com/marketplace/actions/enable-developer-command-prompt
uses: ilammy/msvc-dev-cmd@v1
- name: "Collect metadata"
run: |
set -eu
# https://www.graalvm.org/22.3/reference-manual/native-image/metadata/AutomaticMetadataCollection/#tracing-agent
cat <<'EOF' > collect_metadata.sh
set -eu
rm -rf native-image-metadata
function traceCall() {
echo "Tracing copycat ${@}..."
java -agentlib:native-image-agent=config-merge-dir=native-image-metadata/META-INF/native-image/ -jar copycat-fat.jar ${@} >/dev/null
}
rm -rf workdir
mkdir -p workdir/source/dir1
touch workdir/source/dir1/file1
traceCall sync workdir/source workdir/target
traceCall --help
traceCall --help
traceCall --version
traceCall sync --help
traceCall watch --help
EOF
case "${{ matrix.arch }}" in
linux-arm64)
docker exec --tty -w /workspace arm64 /bin/bash -c "
export JAVA_HOME=${{ matrix.graal_install_root }}
export PATH=${{ matrix.graal_install_root }}/bin:\$PATH
bash collect_metadata.sh
"
;;
*)
bash collect_metadata.sh
;;
esac
- name: "Build executable"
run: |
case "${{ matrix.arch }}" in
linux-amd64)
/usr/bin/gcc -v
native-image \
-H:+StaticExecutableWithDynamicLibC `# https://www.graalvm.org/21.3/reference-manual/native-image/StaticImages/#build-mostly-static-native-image` \
--report-unsupported-elements-at-runtime `# avoid: Unsupported type sun.awt.X11.XBaseWindow is reachable` \
${{ env.NATIVE_IMAGE_ARGS }}
;;
linux-arm64)
docker exec --tty -w /workspace arm64 /bin/bash -c "
export JAVA_HOME=${{ matrix.graal_install_root }}
export PATH=${{ matrix.graal_install_root }}/bin:\$PATH
/usr/bin/gcc -v
native-image \
-H:+StaticExecutableWithDynamicLibC `# https://www.graalvm.org/21.3/reference-manual/native-image/StaticImages/#build-mostly-static-native-image` \
--report-unsupported-elements-at-runtime `# avoid: Unsupported type sun.awt.X11.XBaseWindow is reachable` \
-Djdk.lang.Process.launchMechanism=vfork `# https://github.com/oracle/graal/issues/4143`\
-H:-CheckToolchain `#https://github.com/oracle/graal/issues/4143`\
${{ env.NATIVE_IMAGE_ARGS }}
"
;;
darwin-amd64)
# not using "--static" because of error: DARWIN does not support building static executable images.
native-image ${{ env.NATIVE_IMAGE_ARGS }}
;;
windows-amd64)
# https://www.graalvm.org/21.3/reference-manual/native-image/StaticImages/
native-image.cmd --static ${{ env.NATIVE_IMAGE_ARGS }}
;;
esac
- name: "Test executable"
run: |
case "${{ matrix.arch }}" in
linux-arm64)
docker exec --tty -w /workspace arm64 /bin/bash -c "
chmod u+x copycat-${{ matrix.arch }} &&
./copycat-${{ matrix.arch }} --version &&
./copycat-${{ matrix.arch }} --help &&
./copycat-${{ matrix.arch }} sync --help &&
./copycat-${{ matrix.arch }} watch --help
"
;;
*)
chmod u+x copycat-${{ matrix.arch }}
./copycat-${{ matrix.arch }} --version
./copycat-${{ matrix.arch }} --help
./copycat-${{ matrix.arch }} sync --help
./copycat-${{ matrix.arch }} --help
;;
esac
- name: "Compress executable"
run: |
set -eu
case "${{ matrix.arch }}" in
linux-amd64)
strip --strip-unneeded copycat-${{ matrix.arch }}
/opt/upx/upx -v -9 --no-progress copycat-${{ matrix.arch }}
;;
linux-arm64)
docker exec --tty -w /workspace arm64 /bin/bash -c "
strip --strip-unneeded copycat-${{ matrix.arch }} &&
/opt/upx/upx -v -9 --no-progress copycat-${{ matrix.arch }}
"
;;
darwin-amd64)
# https://www.unix.com/man-page/osx/1/strip/
strip copycat-${{ matrix.arch }}
upx -v -9 --no-progress copycat-${{ matrix.arch }}
;;
windows-amd64)
cp copycat-${{ matrix.arch }}.exe copycat-${{ matrix.arch }}-uncompressed.exe
upx -v -9 --no-progress copycat-${{ matrix.arch }}.exe
;;
esac
- name: "Test compressed executable"
run: |
case "${{ matrix.arch }}" in
linux-arm64)
docker exec --tty -w /workspace arm64 /bin/bash -c "
chmod u+x copycat-${{ matrix.arch }} &&
./copycat-${{ matrix.arch }} --version &&
./copycat-${{ matrix.arch }} --help &&
./copycat-${{ matrix.arch }} sync --help &&
./copycat-${{ matrix.arch }} watch --help
"
;;
*)
chmod u+x copycat-${{ matrix.arch }}
./copycat-${{ matrix.arch }} --version
./copycat-${{ matrix.arch }} --help
./copycat-${{ matrix.arch }} sync --help
./copycat-${{ matrix.arch }} --help
;;
esac
- name: "Share: native binary"
uses: actions/upload-artifact@v4
if: matrix.arch != 'windows-amd64'
with:
name: binaries-${{ matrix.arch }}
path: copycat-${{ matrix.arch }}
- name: "Share: native binary"
uses: actions/upload-artifact@v4
if: matrix.arch == 'windows-amd64'
with:
name: binaries-${{ matrix.arch }}
path: |
copycat-${{ matrix.arch }}.exe
copycat-${{ matrix.arch }}-uncompressed.exe
###########################################################
publish-release:
###########################################################
runs-on: ubuntu-latest
needs:
- native-image
if: ${{ github.ref_name == 'main' && !github.event.act }}
steps:
- name: "SCM Checkout"
# only required by "hub release create" to prevent "fatal: Not a git repository"
uses: actions/checkout@v4 #https://github.com/actions/checkout
- name: "Get: all build artifacts"
uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: "Delete previous '${{ env.RELEASE_NAME }}' release"
if: env.RELEASE_NAME == 'snapshot'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# https://cli.github.com/manual/gh_release_delete
run: |
GH_DEBUG=1 gh release delete "$RELEASE_NAME" --yes --cleanup-tag || true
- name: "Create '${{ env.RELEASE_NAME }}' Release"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# https://cli.github.com/manual/gh_release_create
run: |
GH_DEBUG=1 gh release create "$RELEASE_NAME" \
--latest \
--notes "${{ github.event.head_commit.message }}" \
--target "${{ github.sha }}" \
artifacts/copycat-fat.jar#copycat-${{ env.RELEASE_NAME }}-fat.jar \
artifacts/bash/bashcompletion.sh#copycat-${{ env.RELEASE_NAME }}-bashcompletion.sh \
artifacts/copycat-linux-amd64#copycat-${{ env.RELEASE_NAME }}-linux-amd64 \
artifacts/copycat-linux-arm64#copycat-${{ env.RELEASE_NAME }}-linux-arm64 \
artifacts/copycat-darwin-amd64#copycat-${{ env.RELEASE_NAME }}-darwin-amd64 \
artifacts/copycat-windows-amd64.exe#copycat-${{ env.RELEASE_NAME }}-windows-amd64.exe \
artifacts/copycat-windows-amd64-uncompressed.exe#copycat-${{ env.RELEASE_NAME }}-windows-amd64-uncompressed.exe
- name: "Delete intermediate build artifacts"
uses: geekyeggo/delete-artifact@v4 # https://github.com/GeekyEggo/delete-artifact/
with:
name: "*"
failOnError: false