Repository navigation
192 lines (181 loc) · 8.01 KB
/
Copy pathbuild.yml
File metadata and controls
192 lines (181 loc) · 8.01 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
name: build
# Releases are automatic: change `version` in Cargo.toml on main and this workflow tests, builds,
# tags v<version> and publishes the release. Other pushes to main and pull requests only run the
# tests. Pushing a v* tag by hand still releases that tag, and a run from the Actions tab builds
# test binaries without releasing.
on:
push:
branches: [main]
tags: ["v*"]
pull_request:
workflow_dispatch:
# "contents: write" lets the release job create the tag and the GitHub release.
permissions:
contents: write
# Two pushes in a row never build the same ref at once, so a version is released only once.
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: false
jobs:
plan:
name: decide whether to release
runs-on: ubuntu-latest
outputs:
release: ${{ steps.plan.outputs.release }}
build: ${{ steps.plan.outputs.build }}
tag: ${{ steps.plan.outputs.tag }}
steps:
- uses: actions/checkout@v4
- id: plan
run: |
version=$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -n 1)
[ -n "$version" ] || { echo "no version in Cargo.toml"; exit 1; }
tag="v$version"
release=false
if [ "$GITHUB_REF_TYPE" = tag ]; then
# a hand-pushed tag must match Cargo.toml, or the binaries would report another version
[ "$GITHUB_REF_NAME" = "$tag" ] || { echo "tag $GITHUB_REF_NAME does not match Cargo.toml ($tag)"; exit 1; }
release=true
elif [ "$GITHUB_EVENT_NAME" = push ] && [ "$GITHUB_REF_NAME" = main ]; then
if git ls-remote --exit-code --tags origin "refs/tags/$tag" >/dev/null; then
echo "$tag already exists: tests only"
else
echo "Cargo.toml is at $version and $tag doesn't exist yet: releasing"
release=true
fi
fi
build=$release
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then build=true; fi
echo "release=$release" >> "$GITHUB_OUTPUT"
echo "build=$build" >> "$GITHUB_OUTPUT"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
echo "version $version: release=$release, build=$build" >> "$GITHUB_STEP_SUMMARY"
test:
name: test (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- run: cargo test --locked
windows:
name: build (windows x86_64)
needs: [plan, test]
if: needs.plan.outputs.build == 'true'
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- run: cargo build --release --locked
# Code signing through SignPath (free for open source, certificate by SignPath Foundation).
# It switches on once the repository variable SIGNPATH_ORGANIZATION_ID and the secret
# SIGNPATH_API_TOKEN exist; until then releases are built unsigned. See docs/signing.md.
- name: upload the unsigned exe for signing
id: unsigned
if: vars.SIGNPATH_ORGANIZATION_ID != ''
uses: actions/upload-artifact@v4
with:
name: windows-unsigned
path: target/release/margin.exe
- name: sign with SignPath
if: vars.SIGNPATH_ORGANIZATION_ID != ''
uses: signpath/github-action-submit-signing-request@v3
with:
api-token: ${{ secrets.SIGNPATH_API_TOKEN }}
organization-id: ${{ vars.SIGNPATH_ORGANIZATION_ID }}
project-slug: ${{ vars.SIGNPATH_PROJECT_SLUG || 'margin' }}
# releases go through release signing (an approver confirms each request in SignPath);
# manual runs from the Actions tab use test signing
signing-policy-slug: ${{ needs.plan.outputs.release == 'true' && 'release-signing' || 'test-signing' }}
github-artifact-id: ${{ steps.unsigned.outputs.artifact-id }}
wait-for-completion: true
wait-for-completion-timeout-in-seconds: 3600
output-artifact-directory: signed
- name: package
shell: pwsh
run: |
New-Item -ItemType Directory dist | Out-Null
$exe = if (Test-Path signed\margin.exe) { 'signed\margin.exe' } else { 'target\release\margin.exe' }
Copy-Item $exe dist\margin.exe
$sig = Get-AuthenticodeSignature dist\margin.exe
Write-Host "signature: $($sig.Status) $($sig.SignerCertificate.Subject)"
if ('${{ vars.SIGNPATH_ORGANIZATION_ID }}' -and $sig.Status -eq 'NotSigned') { throw 'signing is configured but margin.exe came back unsigned' }
$hash = (Get-FileHash dist\margin.exe -Algorithm SHA256).Hash.ToLower()
[IO.File]::WriteAllText("dist/margin.exe.sha256", "$hash margin.exe`n")
- uses: actions/upload-artifact@v4
with:
name: windows
path: dist/*
linux:
name: build (linux ${{ matrix.arch }})
needs: [plan, test]
if: needs.plan.outputs.build == 'true'
runs-on: ${{ matrix.runner }}
# the arm64 runner is newer; if it is unavailable the x86_64 release still goes out
continue-on-error: ${{ matrix.arch == 'aarch64' }}
strategy:
fail-fast: false
matrix:
include:
- { runner: ubuntu-latest, arch: x86_64 }
- { runner: ubuntu-24.04-arm, arch: aarch64 }
steps:
- uses: actions/checkout@v4
- name: build a static binary (musl), so it runs on any distribution
run: |
rustup target add ${{ matrix.arch }}-unknown-linux-musl
cargo build --release --locked --target ${{ matrix.arch }}-unknown-linux-musl
- name: package
run: |
mkdir dist
cp target/${{ matrix.arch }}-unknown-linux-musl/release/margin dist/margin-linux-${{ matrix.arch }}
cd dist && sha256sum margin-linux-${{ matrix.arch }} > margin-linux-${{ matrix.arch }}.sha256
- uses: actions/upload-artifact@v4
with:
name: linux-${{ matrix.arch }}
path: dist/*
release:
name: publish the release
needs: [plan, windows, linux]
# the linux job counts as a success even when only arm64 failed (continue-on-error above)
if: ${{ !cancelled() && needs.plan.outputs.release == 'true' && needs.windows.result == 'success' && needs.linux.result == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: create the tag and release if needed, then upload the binaries
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.plan.outputs.tag }}
run: |
ls -l dist
# On a push to main this creates the tag at the commit that was tested and built.
gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1 \
|| gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "Margin $TAG" --generate-notes
gh release upload "$TAG" dist/* --repo "$GITHUB_REPOSITORY" --clobber
# Updates the winget package (winget install arxhr007.Margin) to the new release. It needs the
# secret WINGET_TOKEN: a classic personal access token with the public_repo scope, from an account
# with a fork of microsoft/winget-pkgs. Without it the step is skipped. The first version was
# submitted by hand; this only updates an existing package.
winget:
name: update the winget package
needs: [plan, release]
if: ${{ !cancelled() && needs.release.result == 'success' }}
runs-on: ubuntu-latest
steps:
- name: check for the winget token
id: token
env:
WINGET_TOKEN: ${{ secrets.WINGET_TOKEN }}
run: |
if [ -n "$WINGET_TOKEN" ]; then echo "present=true" >> "$GITHUB_OUTPUT"; else echo "no WINGET_TOKEN secret: skipping"; fi
- uses: vedantmgoyal9/winget-releaser@v2
if: steps.token.outputs.present == 'true'
with:
identifier: arxhr007.Margin
installers-regex: '^margin\.exe$'
release-tag: ${{ needs.plan.outputs.tag }}
token: ${{ secrets.WINGET_TOKEN }}