-
Notifications
You must be signed in to change notification settings - Fork 9
183 lines (159 loc) · 5.81 KB
/
release.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
name: Release
on:
push:
branches: [ "main" ]
paths:
- 'Cargo.toml'
workflow_dispatch:
permissions:
contents: write
jobs:
prepare-release:
name: Prepare release
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-package-info.outputs.version }}
should_release: ${{ steps.check-version.outputs.exists == 'false' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get package info from Cargo.toml
id: get-package-info
run: |
version=$(grep -m1 '^version = ' Cargo.toml | cut -d '"' -f2)
package_name=$(grep -m1 '^name = ' Cargo.toml | cut -d '"' -f2)
echo "version=${version}" >> $GITHUB_OUTPUT
echo "package_name=${package_name}" >> $GITHUB_OUTPUT
- name: Check if version exists on crates.io
id: check-version
run: |
version="${{ steps.get-package-info.outputs.version }}"
package_name="${{ steps.get-package-info.outputs.package_name }}"
if cargo search "${package_name}" | grep -q "^${package_name} = \"${version}\""; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Create release
if: steps.check-version.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
version="${{ steps.get-package-info.outputs.version }}"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git tag -a "v${version}" -m "Release v${version}"
git push origin "v${version}"
gh release create "v${version}" \
--title "v${version}" \
--generate-notes \
--draft # Make it draft until binaries are uploaded
build-and-upload:
needs: prepare-release
if: needs.prepare-release.outputs.should_release == 'true'
name: ${{ matrix.target }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
use-cross: true
- os: ubuntu-latest
target: aarch64-unknown-linux-musl
use-cross: true
- os: macos-13
target: x86_64-apple-darwin
use-cross: false
- os: macos-latest
target: aarch64-apple-darwin
use-cross: false
- os: windows-latest
target: x86_64-pc-windows-msvc
use-cross: false
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Handle Rust dependencies caching
uses: Swatinem/rust-cache@v2
with:
key: v1-${{ matrix.target }}
- name: Build binary
uses: clechasseur/rs-cargo@v2
with:
command: build
args: --release --target ${{ matrix.target }}
use-cross: ${{ matrix.use-cross }}
- name: Get binary name from Cargo.toml
shell: bash
run: |
BINARY_NAME=$(grep -m1 '^name' Cargo.toml | cut -d'"' -f2 | cut -d"'" -f2)
echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
- name: Create release archive
shell: bash
run: |
cd target/${{ matrix.target }}/release
VERSION="${{ needs.prepare-release.outputs.version }}"
if [ "${{ runner.os }}" = "Windows" ]; then
ARCHIVE="${{ env.BINARY_NAME }}-v${VERSION}-${{ matrix.target }}.zip"
mv "${{ env.BINARY_NAME }}.exe" "${{ env.BINARY_NAME }}-${{ matrix.target }}.exe"
7z a "$ARCHIVE" "${{ env.BINARY_NAME }}-${{ matrix.target }}.exe"
else
ARCHIVE="${{ env.BINARY_NAME }}-v${VERSION}-${{ matrix.target }}.tar.gz"
mv "${{ env.BINARY_NAME }}" "${{ env.BINARY_NAME }}-${{ matrix.target }}"
tar -czvf "$ARCHIVE" "${{ env.BINARY_NAME }}-${{ matrix.target }}"
fi
# Generate checksums
openssl dgst -r -sha256 -out "$ARCHIVE.sha256" "$ARCHIVE"
openssl dgst -r -sha512 -out "$ARCHIVE.sha512" "$ARCHIVE"
echo "ASSET=$ARCHIVE" >> $GITHUB_ENV
- name: Verify binary
shell: bash
run: |
cd target/${{ matrix.target }}/release
case "${{ matrix.target }}" in
*windows*)
7z x -y "$ASSET"
./${{ env.BINARY_NAME }}-${{ matrix.target }}.exe --version ;;
aarch64*)
echo "Can't test an ARM binary on a AMD64 runner" ;;
*)
tar -xvzf "$ASSET"
./${{ env.BINARY_NAME }}-${{ matrix.target }} --version ;;
esac
- name: Upload to release
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ needs.prepare-release.outputs.version }}
files: |
target/${{ matrix.target }}/release/${{ env.ASSET }}
target/${{ matrix.target }}/release/${{ env.ASSET }}.sha256
target/${{ matrix.target }}/release/${{ env.ASSET }}.sha512
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish:
needs: [prepare-release, build-and-upload]
if: needs.prepare-release.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Rust cache
uses: Swatinem/rust-cache@v2
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish
- name: Publish release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release edit "v${{ needs.prepare-release.outputs.version }}" --draft=false