-
Notifications
You must be signed in to change notification settings - Fork 0
252 lines (214 loc) · 7.36 KB
/
cicd.yaml
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
name: CI/CD
on:
workflow_dispatch:
inputs:
prerelease:
default: true
type: boolean
tag_name:
required: true
type: string
push:
paths: [".cargo/**", "src/**", "tests/**", "Cargo.*", "CHANGELOG*"]
branches: [main]
pull_request:
# https://github.com/orgs/community/discussions/25161#discussioncomment-3246673
paths: [".cargo/**", "src/**", "tests/**", "Cargo.*", "CHANGELOG*"]
branches: [main]
defaults:
run:
shell: bash
jobs:
release_please:
name: Release please
if: ${{ !contains(github.event_name, 'workflow_dispatch') }}
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
outputs:
created: ${{ steps.release.outputs.release_created }}
tag_name: ${{ steps.release.outputs.tag_name }}
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: rust
prerelease: true
ci:
name: Patrol
needs: [release_please]
if: ${{ !failure() && !cancelled() && !needs.release_please.outputs.created }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Prexec
run: |
########## script ##########
case "${{ matrix.os }}" in
*'windows'*)
echo "OPENSSL_SRC_PERL=C:/Strawberry/perl/bin/perl" | tee -a "$GITHUB_ENV"
;;
esac
cargo update
- name: Run tests
run: cargo test --all-features --workspace
- name: Formatting check
run: cargo fmt --all --check
- name: Clippy check
run: cargo clippy --all-targets --all-features --workspace -- -D warnings
build:
name: Build ${{ matrix.target }}
needs: [ci]
if: ${{ !failure() && !cancelled() }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- target: aarch64-apple-darwin
os: macos-latest
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
use-cross: true
- target: x86_64-apple-darwin
os: macos-latest
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
- target: x86_64-pc-windows-msvc
os: windows-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
- name: Install cross
if: matrix.use-cross
uses: taiki-e/install-action@v2
with:
tool: cross
- name: Prexec
run: |
########## script ##########
# check ci status
case "${{ needs.ci.result }}" in
'success')
echo "success: ci passed"
;;
'skipped')
echo "warn: skipped ci triggered by release_please.created"
;;
*)
echo "error: ??need cancel??"
exit 1
;;
esac
case "${{ matrix.target }}" in
*'-musl')
echo "+ install musl-tools"
sudo apt-get update -qq >/dev/null && sudo apt-get install -qqy musl-tools >/dev/null
;;
*'-windows-'*)
echo "OPENSSL_SRC_PERL=C:/Strawberry/perl/bin/perl" | tee -a "$GITHUB_ENV"
;;
esac
build_cmd="cargo"
[[ "${{ matrix.use-cross }}" == "true" ]] && build_cmd="cross"
echo "BUILD_CMD=${build_cmd}" | tee -a "$GITHUB_ENV"
cargo_metadata="$(cargo metadata --no-deps --format-version 1)"
echo "CRATE_NAME=$(echo $cargo_metadata | jq -r ".packages[0].name")" | tee -a "$GITHUB_ENV"
echo "CRATE_VERSION=$(echo $cargo_metadata | jq -r ".packages[0].version")" | tee -a "$GITHUB_ENV"
cargo update
- name: Build ${{ env.CRATE_NAME }} ${{ env.CRATE_VERSION }}
run: |
$BUILD_CMD build --locked --release --target="${{ matrix.target }}"
- name: Create archive
id: create_archive
run: |
########## script ##########
dist="$(pwd)/__dist__"
mkdir "$dist" && echo "$_" && echo
########## binary ##########
cp "$(find "./target/${{ matrix.target }}/release/" -maxdepth 1 -type f -perm 755)" "${dist}/"
########## misc ##########
cp "README"* "LICENSE"* "CHANGELOG"* "${dist}/" || echo
########## list ##########
echo "List long format:"
ls -lh "${dist}/" && echo
echo "List dynamic dependencies:"
ldd_cmd="ldd"
[[ "$RUNNER_OS" == "macOS" ]] && ldd_cmd="otool -L"
$ldd_cmd "${dist}/${CRATE_NAME}" || :
echo
########## archive ##########
archive_prefix="${CRATE_NAME}-v${CRATE_VERSION}-${{ matrix.target }}"
case "$RUNNER_OS" in
Windows)
archive_suffix=".zip"
archive_cmd="7z a -r"
archive_args="${dist}/"*
;;
*)
archive_suffix=".tar.gz"
archive_cmd="tar -zcf"
archive_args="-C ${dist}/ ."
;;
esac
archive="${archive_prefix}${archive_suffix}"
$archive_cmd "$archive" $archive_args
echo "path=${archive}" | tee -a "$GITHUB_OUTPUT"
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
path: ${{ steps.create_archive.outputs.path }}
manually_release:
name: Manually release
needs: [build]
if: ${{ !failure() && !cancelled() && contains(github.event_name, 'workflow_dispatch') }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@v3
- name: Cleanup a ${{ inputs.tag_name }} release
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
########## script ##########
endpoint="repos/${{ github.repository }}/git/refs/tags/${{ inputs.tag_name }}"
gh api "$endpoint" > /dev/null && {
gh api "$endpoint" -X DELETE && echo "DELETE tag ${{ inputs.tag_name }}"
release_id="$(gh api "repos/${{ github.repository }}/releases/tags/${{ inputs.tag_name }}" --jq ".id" 2>/dev/null)"
[[ -n "$release_id" ]] && {
gh api "repos/${{ github.repository }}/releases/${release_id}" -X DELETE && echo "DELETE releases $release_id"
}
}
:
- uses: softprops/action-gh-release@v1
with:
generate_release_notes: true
prerelease: ${{ inputs.prerelease }}
tag_name: ${{ inputs.tag_name }}
files: |
artifact/*
automatic_release:
name: Automatic release
needs: [release_please, build]
if: ${{ !failure() && !cancelled() && needs.release_please.outputs.created }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@v3
- uses: softprops/action-gh-release@v1
with:
prerelease: false
tag_name: ${{ needs.release_please.outputs.tag_name }}
files: |
artifact/*