11name : Rust
2+
23on :
34 push :
45 branches : [master]
56 workflow_dispatch :
7+
68env :
79 CARGO_TERM_COLOR : always
10+
811jobs :
912 setup :
1013 runs-on : ubuntu-latest
1114 outputs :
12- version : ${{ steps.version.outputs.version }}
13- tag : ${{ steps.version.outputs.tag }}
14- package_name : ${{ steps.version.outputs.package_name }}
15+ version : ${{ steps.read.outputs.version }}
16+ tag : ${{ steps.read.outputs.tag }}
1517 steps :
1618 - name : checkout
1719 uses : actions/checkout@v4
1820 with :
1921 fetch-depth : 0
20- - name : setup
22+
23+ - name : install
2124 uses : dtolnay/rust-toolchain@stable
2225 with :
2326 toolchain : stable
2427 components : rustfmt, clippy
28+
2529 - name : cache
2630 uses : actions/cache@v3
2731 with :
@@ -30,71 +34,73 @@ jobs:
3034 ~/.cargo/git
3135 target
3236 key : ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
33- - name : install
37+
38+ - name : build
3439 run : cargo install toml-cli
35- - name : cacheBin
40+
41+ - name : save
3642 uses : actions/cache@v3
3743 with :
3844 path : ~/.cargo/bin/toml
3945 key : toml-cli-${{ runner.os }}
40- - name : detect
41- id : detect
46+
47+ - name : read
48+ id : read
4249 run : |
4350 VERSION=$(toml get Cargo.toml package.version --raw)
44- PACKAGE_NAME=$(toml get Cargo.toml package.name --raw)
4551 echo "detected version: $VERSION"
46- echo "detected package name: $PACKAGE_NAME"
4752 if [ -z "$VERSION" ]; then
4853 echo "❌ failed to read version from Cargo.toml"
4954 exit 1
5055 fi
51- if [ -z "$PACKAGE_NAME" ]; then
52- echo "❌ failed to read package name from Cargo.toml"
53- exit 1
54- fi
5556 echo "version=$VERSION" >> $GITHUB_OUTPUT
5657 echo "tag=v$VERSION" >> $GITHUB_OUTPUT
57- echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
5858
59- format :
59+ check :
6060 needs : setup
6161 runs-on : ubuntu-latest
6262 steps :
6363 - name : checkout
6464 uses : actions/checkout@v4
65+
6566 - name : setup
6667 uses : dtolnay/rust-toolchain@stable
6768 with :
6869 toolchain : stable
6970 components : rustfmt
70- - name : check
71+
72+ - name : format
7173 run : cargo fmt -- --check
7274
73- test :
75+ tests :
7476 needs : setup
7577 runs-on : ubuntu-latest
7678 steps :
7779 - name : checkout
7880 uses : actions/checkout@v4
79- - name : setup
81+
82+ - name : prepare
8083 uses : dtolnay/rust-toolchain@stable
8184 with :
8285 toolchain : stable
83- - name : run
86+
87+ - name : test
8488 run : cargo test --all-features -- --nocapture
8589
86- lint :
90+ clippy :
8791 needs : setup
8892 runs-on : ubuntu-latest
8993 steps :
9094 - name : checkout
9195 uses : actions/checkout@v4
92- - name : setup
96+
97+ - name : load
9398 uses : dtolnay/rust-toolchain@stable
9499 with :
95100 toolchain : stable
96101 components : clippy
97- - name : check
102+
103+ - name : lint
98104 run : cargo clippy --all-features -- -A warnings
99105
100106 build :
@@ -103,21 +109,25 @@ jobs:
103109 steps :
104110 - name : checkout
105111 uses : actions/checkout@v4
112+
106113 - name : setup
107114 uses : dtolnay/rust-toolchain@stable
108115 with :
109116 toolchain : stable
117+
110118 - name : check
111119 run : cargo check --release --all-features
112120
113121 publish :
114- needs : [setup, format, test, lint, build]
122+ needs : [setup, check, tests, clippy, build]
123+ if : needs.setup.outputs.tag != ''
115124 runs-on : ubuntu-latest
116125 outputs :
117126 published : ${{ steps.publish.outputs.published }}
118127 steps :
119128 - name : checkout
120129 uses : actions/checkout@v4
130+
121131 - name : publish
122132 id : publish
123133 env :
@@ -128,127 +138,50 @@ jobs:
128138 echo "${{ secrets.CARGO_REGISTRY_TOKEN }}" | cargo login
129139 if cargo publish --allow-dirty; then
130140 echo "published=true" >> $GITHUB_OUTPUT
131- echo "✅ published version ${{ needs.setup.outputs.version }} to crates.io"
132- echo "📦 https://crates.io/crates/${{ needs.setup.outputs.package_name }}"
133- echo "📚 https://docs.rs/${{ needs.setup.outputs.package_name }}/${{ needs.setup.outputs.version }}/"
134141 else
135- echo "❌ publish failed"
142+ echo "❌ publish failed, but continuing. "
136143 fi
137144
138145 release :
139- needs : [setup, format, test, lint , build]
146+ needs : [setup, check, tests, clippy , build]
140147 permissions :
141148 contents : write
142149 packages : write
143150 if : needs.setup.outputs.tag != ''
144151 runs-on : ubuntu-latest
145- outputs :
146- released : ${{ steps.release.outputs.released }}
147152 steps :
148153 - name : checkout
149154 uses : actions/checkout@v4
150155 with :
151156 fetch-depth : 0
152- - name : verify
153- id : verify
154- run : |
155- if git tag -l | grep -q "^${{ needs.setup.outputs.tag }}$"; then
156- echo "tag_exists=true" >> $GITHUB_OUTPUT
157- echo "🏷️ Tag ${{ needs.setup.outputs.tag }} already exists locally"
158- else
159- echo "tag_exists=false" >> $GITHUB_OUTPUT
160- echo "🏷️ Tag ${{ needs.setup.outputs.tag }} does not exist locally"
161- fi
162- if git ls-remote --tags origin | grep -q "refs/tags/${{ needs.setup.outputs.tag }}$"; then
163- echo "remote_tag_exists=true" >> $GITHUB_OUTPUT
164- echo "🌐 Tag ${{ needs.setup.outputs.tag }} already exists on remote"
165- else
166- echo "remote_tag_exists=false" >> $GITHUB_OUTPUT
167- echo "🌐 Tag ${{ needs.setup.outputs.tag }} does not exist on remote"
168- fi
169- - name : inspect
170- id : inspect
171- run : |
172- if gh release view "${{ needs.setup.outputs.tag }}" > /dev/null 2>&1; then
173- echo "release_exists=true" >> $GITHUB_OUTPUT
174- echo "📦 Release ${{ needs.setup.outputs.tag }} already exists"
175- else
176- echo "release_exists=false" >> $GITHUB_OUTPUT
177- echo "📦 Release ${{ needs.setup.outputs.tag }} does not exist"
178- fi
179- env :
180- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
157+
181158 - name : release
182- id : release
183159 run : |
184- set -e
185- echo "released=false" >> $GITHUB_OUTPUT
186- if [ "${{ steps.inspect.outputs.release_exists }}" = "true" ]; then
187- echo "🔄 Updating existing release ${{ needs.setup.outputs.tag }}"
188- gh release view "${{ needs.setup.outputs.tag }}" --json assets --jq '.assets[].name' | while read asset; do
189- if [ -n "$asset" ]; then
190- echo "🗑️ Deleting existing asset: $asset"
191- gh release delete-asset "${{ needs.setup.outputs.tag }}" "$asset" --yes || true
192- fi
193- done
194- echo "📦 Creating source archives"
195- git archive --format=zip --prefix="${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}/" HEAD > "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.zip"
196- git archive --format=tar.gz --prefix="${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}/" HEAD > "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.tar.gz"
197- if gh release edit "${{ needs.setup.outputs.tag }}" \
198- --title "${{ needs.setup.outputs.tag }} (Updated $(date '+%Y-%m-%d %H:%M:%S'))" \
199- --notes "Release ${{ needs.setup.outputs.tag }} - Updated at $(date '+%Y-%m-%d %H:%M:%S UTC')
200- ## Changes
201- - Automated release update from CI/CD pipeline
202- - Version: ${{ needs.setup.outputs.version }}
203- - Package: ${{ needs.setup.outputs.package_name }}
204- ## Links
205- 📦 [Crate on crates.io](https://crates.io/crates/${{ needs.setup.outputs.package_name }}/${{ needs.setup.outputs.version }})
206- 📚 [Documentation on docs.rs](https://docs.rs/${{ needs.setup.outputs.package_name }}/${{ needs.setup.outputs.version }}/)
207- 📋 [Commit History](https://github.com/${{ github.repository }}/commits/${{ needs.setup.outputs.tag }})" && \
208- gh release upload "${{ needs.setup.outputs.tag }}" \
209- "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.zip" \
210- "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.tar.gz" \
211- --clobber; then
212- echo "released=true" >> $GITHUB_OUTPUT
213- echo "✅ Updated existing GitHub release ${{ needs.setup.outputs.tag }}"
214- echo "📦 Uploaded source archives"
215- else
216- echo "❌ Failed to update existing GitHub release"
217- fi
218- else
219- if [ "${{ steps.verify.outputs.remote_tag_exists }}" = "false" ]; then
220- echo "🏷️ Creating new tag ${{ needs.setup.outputs.tag }}"
221- git tag "${{ needs.setup.outputs.tag }}"
222- git push origin "${{ needs.setup.outputs.tag }}"
223- fi
224- echo "🆕 Creating new release ${{ needs.setup.outputs.tag }}"
225- echo "📦 Creating source archives"
226- git archive --format=zip --prefix="${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}/" HEAD > "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.zip"
227- git archive --format=tar.gz --prefix="${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}/" HEAD > "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.tar.gz"
228- if gh release create "${{ needs.setup.outputs.tag }}" \
229- --title "${{ needs.setup.outputs.tag }} (Created $(date '+%Y-%m-%d %H:%M:%S'))" \
230- --notes "Release ${{ needs.setup.outputs.tag }} - Created at $(date '+%Y-%m-%d %H:%M:%S UTC')
231- ## Changes
232- - Version: ${{ needs.setup.outputs.version }}
233- - Package: ${{ needs.setup.outputs.package_name }}
234- ## Links
235- 📦 [Crate on crates.io](https://crates.io/crates/${{ needs.setup.outputs.package_name }}/${{ needs.setup.outputs.version }})
236- 📚 [Documentation on docs.rs](https://docs.rs/${{ needs.setup.outputs.package_name }}/${{ needs.setup.outputs.version }}/)
237- 📋 [Commit History](https://github.com/${{ github.repository }}/commits/${{ needs.setup.outputs.tag }})" \
238- --latest && \
239- gh release upload "${{ needs.setup.outputs.tag }}" \
240- "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.zip" \
241- "${{ needs.setup.outputs.package_name }}-${{ needs.setup.outputs.version }}.tar.gz"; then
242- echo "released=true" >> $GITHUB_OUTPUT
243- echo "✅ Created new GitHub release ${{ needs.setup.outputs.tag }}"
244- echo "📦 Uploaded source archives"
245- else
246- echo "❌ Failed to create new GitHub release"
247- fi
248- fi
249- if [ "${{ steps.release.outputs.released }}" = "true" ]; then
250- echo "🏷️ Tag: ${{ needs.setup.outputs.tag }}"
251- echo "🚀 Release: ${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ needs.setup.outputs.tag }}"
252- fi
160+ gh release create "${{ needs.setup.outputs.tag }}" \
161+ --title "${{ needs.setup.outputs.tag }}" \
162+ --notes "Release ${{ needs.setup.outputs.tag }}" \
163+ --latest
253164 env :
254165 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
166+
167+ notice :
168+ needs : [setup, publish, release]
169+ if : needs.publish.outputs.published == 'true'
170+ runs-on : ubuntu-latest
171+ steps :
172+ - name : checkout
173+ uses : actions/checkout@v4
174+
175+ - name : restore
176+ uses : actions/cache@v3
177+ with :
178+ path : ~/.cargo/bin/toml
179+ key : toml-cli-${{ runner.os }}
180+
181+ - name : output
182+ run : |
183+ PACKAGE_NAME=$(toml get Cargo.toml package.name --raw)
184+ echo "✅ released version ${{ needs.setup.outputs.version }}"
185+ echo "📦 https://crates.io/crates/$PACKAGE_NAME"
186+ echo "🏷️ tag: ${{ needs.setup.outputs.tag }}"
187+ echo "🚀 release: ${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ needs.setup.outputs.tag }}"
0 commit comments