-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (169 loc) · 6.97 KB
/
rust.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
name: Build, test, upload artifact and release crate
on:
push:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
jobs:
conditions:
outputs:
release: ${{ steps.set-release-flag.outputs.release }}
release-id: ${{ steps.get-release-id.outputs.release_id }}
runs-on: thevickypedia-default
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get Package Name
run: |
name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
echo "Package Name: $name"
echo "pkg_name=$name" >> $GITHUB_ENV
echo "asset_name=asset_$name" >> $GITHUB_ENV
shell: bash
- name: Set Release Flag
id: set-release-flag
run: |
current_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
latest_version=$(curl -s https://crates.io/api/v1/crates/${{ env.pkg_name }} | jq -r '.versions[0].num')
echo "Current Package Version: ${current_version}"
echo "Latest Package Version: ${latest_version}"
if [ "$latest_version" != "$current_version" ]; then
echo "Version has changed. Setting release flag to true."
echo "release=true" >> $GITHUB_ENV
echo "release=true" >> $GITHUB_OUTPUT
else
echo "Version has not changed. Setting release flag to false."
echo "release=false" >> $GITHUB_ENV
echo "release=false" >> $GITHUB_OUTPUT
fi
echo "pkg_version=$current_version" >> $GITHUB_ENV
shell: bash
- name: Create New Release
id: get-release-id
if: env.release == 'true'
run: |
release_tag="v${{ env.pkg_version }}"
cargo_prerelease=("alpha" "beta" "rc")
prerelease=false
for cargo_pre in "${cargo_prerelease[@]}"; do
if [[ $pkg_version == *"$cargo_pre"* ]]; then
prerelease=true
break
fi
done
commit_msg="Release compiled executable for $release_tag"
release_data="{\"tag_name\":\"$release_tag\",\"name\":\"$release_tag\",\"body\":\"$commit_msg\",\"draft\":false,\"prerelease\":$prerelease}"
response=$(curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \
-d "$release_data" \
"https://api.github.com/repos/${{ github.repository }}/releases")
release_id=$(echo $response | jq -r .id)
echo "Release ID: $release_id"
echo "release_id=$release_id" >> "$GITHUB_OUTPUT"
shell: bash
build-and-upload:
needs: conditions
strategy:
matrix:
platform:
# Naming Convention: {package_name}-{operating_system}-{architecture}.{archive_format}
- release_for: darwin-amd64
bin: none-shall-pass
name: none-shall-pass-darwin-amd64.tar.gz
pkg-name: none-shall-pass-darwin-amd64
- release_for: darwin-arm64
bin: none-shall-pass
name: none-shall-pass-darwin-arm64.tar.gz
pkg-name: none-shall-pass-darwin-arm64
- release_for: linux-amd64
bin: none-shall-pass
name: none-shall-pass-linux-amd64.tar.gz
pkg-name: none-shall-pass-linux-amd64
- release_for: windows-amd64
bin: none-shall-pass.exe
name: none-shall-pass-windows-amd64.zip
pkg-name: none-shall-pass-windows-amd64
name: Upload asset for ${{ matrix.platform.release_for }}
runs-on: ${{ matrix.platform.release_for }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update Rust
# print it with style
run: |
printf '*%.0s' {1..60} && printf "\n"
echo "Existing rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n\n"
rustup update && printf "\n"
printf '*%.0s' {1..60} && printf "\n"
echo "Updated rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n"
shell: bash
- name: Build
run: |
if [ "${{ needs.conditions.outputs.release }}" == "true" ]; then
cargo build --release
else
cargo build --verbose
fi
shell: bash
- name: Run tests
run: |
if [ "${{ needs.conditions.outputs.release }}" == "true" ]; then
cargo test
else
cargo test --verbose
fi
shell: bash
- name: Release ID Propagation
if: needs.conditions.outputs.release == 'true'
run: |
if [ -n "${{ needs.conditions.outputs.release-id }}" ]; then
echo "Release ID propagated: ${{ needs.conditions.outputs.release-id }}"
else
echo "Release ID propagation failed. Exiting.."
exit 1
fi
echo "start_time=$(date +%s)" >> "$GITHUB_ENV"
shell: bash
- name: Copy Asset (Windows)
if: needs.conditions.outputs.release == 'true' && matrix.platform.release_for == 'windows*'
run: |
mkdir -p ${{ matrix.platform.pkg-name }}
cp target/release/${{ matrix.platform.bin }} ${{ matrix.platform.pkg-name }}/${{ matrix.platform.bin }}
Compress-Archive -DestinationPath ${{ matrix.platform.name }} -Path ${{ matrix.platform.pkg-name }}
shell: pwsh
- name: Copy Asset (macOS/Ubuntu)
if: needs.conditions.outputs.release == 'true' && matrix.platform.release_for != 'windows*'
run: |
mkdir -p ${{ matrix.platform.pkg-name }}
cp target/release/${{ matrix.platform.bin }} ${{ matrix.platform.pkg-name }}/${{ matrix.platform.bin }}
tar -zcvf ${{ matrix.platform.name }} ${{ matrix.platform.pkg-name }}
shell: bash
- name: Upload Asset to Release
if: needs.conditions.outputs.release == 'true'
run: |
curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"${{ matrix.platform.name }}" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ needs.conditions.outputs.release-id }}/assets?name=${{ matrix.platform.name }}"
shell: bash
- name: Runtime Analyzer
if: needs.conditions.outputs.release == 'true'
run: |
start=${{ env.start_time }}
end=$(date +%s)
time_taken=$((end-start))
echo "Time taken for ${{ matrix.platform.release_for }}: $time_taken s"
shell: bash
release-crate:
runs-on: thevickypedia-default
needs: [ conditions, build-and-upload ]
steps:
- name: Release Crate
if: needs.conditions.outputs.release == 'true'
run: |
cargo login ${{ secrets.CRATES_TOKEN }}
cargo publish --allow-dirty # Set allow-dirty since building will create a /target folder that will be uncommitted in git
shell: bash