-
Notifications
You must be signed in to change notification settings - Fork 2
252 lines (246 loc) · 9.4 KB
/
rust.yml
File metadata and controls
252 lines (246 loc) · 9.4 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
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
name: Rust
on:
push:
branches: [master]
env:
CARGO_TERM_COLOR: always
jobs:
setup:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.read.outputs.version }}
tag: ${{ steps.read.outputs.tag }}
package_name: ${{ steps.read.outputs.package_name }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install rust-toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: rustfmt, clippy
- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install toml-cli
run: cargo install toml-cli
- name: Cache toml-cli
uses: actions/cache@v3
with:
path: ~/.cargo/bin/toml
key: toml-cli-${{ runner.os }}
- name: Read cargo metadata
id: read
run: |
VERSION=$(toml get Cargo.toml package.version --raw)
PACKAGE_NAME=$(toml get Cargo.toml package.name --raw)
echo "📦 Detected package: $PACKAGE_NAME v$VERSION"
if [ -z "$VERSION" ] || [ -z "$PACKAGE_NAME" ]; then
echo "❌ Failed to read package info from Cargo.toml"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
check:
needs: setup
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: rustfmt
- name: Format check
run: cargo fmt -- --check
tests:
needs: setup
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Prepare environment
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Run tests
run: cargo test --all-features -- --nocapture
clippy:
needs: setup
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Load clippy
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy
- name: Run clippy
run: cargo clippy --all-features
build:
needs: setup
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup build
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Build release
run: cargo check --release --all-features
publish:
needs: [setup, check, tests, clippy, build]
if: needs.setup.outputs.tag != ''
runs-on: ubuntu-latest
outputs:
published: ${{ steps.publish.outputs.published }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Restore toml-cli
uses: actions/cache@v3
with:
path: ~/.cargo/bin/toml
key: toml-cli-${{ runner.os }}
- name: Publish to crates.io
id: publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
set -e
echo "published=false" >> $GITHUB_OUTPUT
echo "${{ secrets.CARGO_REGISTRY_TOKEN }}" | cargo login
PACKAGE_NAME=$(toml get Cargo.toml package.name --raw)
VERSION=${{ needs.setup.outputs.version }}
if cargo publish --allow-dirty; then
echo "published=true" >> $GITHUB_OUTPUT
echo "🎉🎉🎉 PUBLISH SUCCESSFUL 🎉🎉🎉"
echo "✅ Successfully published $PACKAGE_NAME v$VERSION to crates.io"
echo "📦 Crates.io: [https://crates.io/crates/$PACKAGE_NAME/$VERSION](https://crates.io/crates/$PACKAGE_NAME/$VERSION)"
echo "📚 Docs.rs: [https://docs.rs/$PACKAGE_NAME/$VERSION](https://docs.rs/$PACKAGE_NAME/$VERSION)"
else
echo "❌ Publish failed"
fi
release:
needs: [setup, check, tests, clippy, build]
permissions:
contents: write
packages: write
if: needs.setup.outputs.tag != ''
runs-on: ubuntu-latest
outputs:
released: ${{ steps.release.outputs.released }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get package name
id: package_info
run: |
echo "package_name=${{ needs.setup.outputs.package_name }}" >> $GITHUB_OUTPUT
- name: Check tag status
id: check_tag
run: |
if git tag -l | grep -q "^${{ needs.setup.outputs.tag }}$"; then
echo "tag_exists=true" >> $GITHUB_OUTPUT
echo "🏷️ Tag ${{ needs.setup.outputs.tag }} exists locally"
else
echo "tag_exists=false" >> $GITHUB_OUTPUT
echo "🏷️ Tag ${{ needs.setup.outputs.tag }} does not exist locally"
fi
if git ls-remote --tags origin | grep -q "refs/tags/${{ needs.setup.outputs.tag }}$"; then
echo "remote_tag_exists=true" >> $GITHUB_OUTPUT
echo "🌐 Tag ${{ needs.setup.outputs.tag }} exists on remote"
else
echo "remote_tag_exists=false" >> $GITHUB_OUTPUT
echo "🌐 Tag ${{ needs.setup.outputs.tag }} does not exist on remote"
fi
- name: Check release status
id: check_release
run: |
if gh release view "${{ needs.setup.outputs.tag }}" > /dev/null 2>&1; then
echo "release_exists=true" >> $GITHUB_OUTPUT
echo "📦 Release ${{ needs.setup.outputs.tag }} already exists"
else
echo "release_exists=false" >> $GITHUB_OUTPUT
echo "📦 Release ${{ needs.setup.outputs.tag }} does not exist"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create or update release
id: release
run: |
set -e
echo "released=false" >> $GITHUB_OUTPUT
PACKAGE_NAME="${{ steps.package_info.outputs.package_name }}"
VERSION="${{ needs.setup.outputs.version }}"
TAG="${{ needs.setup.outputs.tag }}"
echo "📦 Building source archives..."
git archive --format=zip --prefix="${PACKAGE_NAME}-${VERSION}/" HEAD > "${PACKAGE_NAME}-${VERSION}.zip"
git archive --format=tar.gz --prefix="${PACKAGE_NAME}-${VERSION}/" HEAD > "${PACKAGE_NAME}-${VERSION}.tar.gz"
if [ "${{ steps.check_release.outputs.release_exists }}" = "true" ]; then
echo "🔄 Updating existing release: $TAG"
gh release view "$TAG" --json assets --jq '.assets[].name' | while read asset; do
if [ -n "$asset" ]; then
echo "🗑️ Deleting asset: $asset"
gh release delete-asset "$TAG" "$asset" --yes || true
fi
done
if gh release edit "$TAG" \
--title "$TAG (Updated $(date '+%Y-%m-%d %H:%M:%S'))" \
--notes "Release $TAG - Updated at $(date '+%Y-%m-%d %H:%M:%S UTC')
## Changes
- Version: $VERSION
- Package: $PACKAGE_NAME
## Links
📦 [Crate on crates.io](https://crates.io/crates/$PACKAGE_NAME/$VERSION)
📚 [Documentation on docs.rs](https://docs.rs/$PACKAGE_NAME/$VERSION)
📋 [Commit History](https://github.com/${{ github.repository }}/commits/$TAG)" && \
gh release upload "$TAG" "${PACKAGE_NAME}-${VERSION}.zip" "${PACKAGE_NAME}-${VERSION}.tar.gz" --clobber; then
echo "released=true" >> $GITHUB_OUTPUT
echo "✅ Updated release $TAG"
echo "🔖 Tag: $TAG"
echo "🚀 Release: [GitHub Release](${{ github.server_url }}/${{ github.repository }}/releases/tag/$TAG)"
else
echo "❌ Failed to update release"
fi
else
if [ "${{ steps.check_tag.outputs.remote_tag_exists }}" = "false" ]; then
echo "🏷️ Creating and pushing tag: $TAG"
git tag "$TAG"
git push origin "$TAG"
fi
echo "🆕 Creating new release: $TAG"
if gh release create "$TAG" \
--title "$TAG (Created $(date '+%Y-%m-%d %H:%M:%S'))" \
--notes "Release $TAG - Created at $(date '+%Y-%m-%d %H:%M:%S UTC')
## Changes
- Version: $VERSION
- Package: $PACKAGE_NAME
## Links
📦 [Crate on crates.io](https://crates.io/crates/$PACKAGE_NAME/$VERSION)
📚 [Documentation on docs.rs](https://docs.rs/$PACKAGE_NAME/$VERSION)
📋 [Commit History](https://github.com/${{ github.repository }}/commits/$TAG)" \
--latest && \
gh release upload "$TAG" "${PACKAGE_NAME}-${VERSION}.zip" "${PACKAGE_NAME}-${VERSION}.tar.gz"; then
echo "released=true" >> $GITHUB_OUTPUT
echo "✅ Created release $TAG"
echo "🔖 Tag: $TAG"
echo "🚀 Release: [GitHub Release](${{ github.server_url }}/${{ github.repository }}/releases/tag/$TAG)"
else
echo "❌ Failed to create release"
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}