|
| 1 | +name: Rust |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [master] |
| 5 | +env: |
| 6 | + CARGO_TERM_COLOR: always |
| 7 | +jobs: |
| 8 | + setup: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + outputs: |
| 11 | + version: ${{ steps.read.outputs.version }} |
| 12 | + tag: ${{ steps.read.outputs.tag }} |
| 13 | + package_name: ${{ steps.read.outputs.package_name }} |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + - name: Install rust-toolchain |
| 20 | + uses: dtolnay/rust-toolchain@stable |
| 21 | + with: |
| 22 | + toolchain: stable |
| 23 | + components: rustfmt, clippy |
| 24 | + - name: Cache dependencies |
| 25 | + uses: actions/cache@v3 |
| 26 | + with: |
| 27 | + path: | |
| 28 | + ~/.cargo/registry |
| 29 | + ~/.cargo/git |
| 30 | + target |
| 31 | + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 32 | + - name: Install toml-cli |
| 33 | + run: cargo install toml-cli |
| 34 | + - name: Cache toml-cli |
| 35 | + uses: actions/cache@v3 |
| 36 | + with: |
| 37 | + path: ~/.cargo/bin/toml |
| 38 | + key: toml-cli-${{ runner.os }} |
| 39 | + - name: Read cargo metadata |
| 40 | + id: read |
| 41 | + run: | |
| 42 | + VERSION=$(toml get Cargo.toml package.version --raw) |
| 43 | + PACKAGE_NAME=$(toml get Cargo.toml package.name --raw) |
| 44 | + echo "📦 Detected package: $PACKAGE_NAME v$VERSION" |
| 45 | + if [ -z "$VERSION" ] || [ -z "$PACKAGE_NAME" ]; then |
| 46 | + echo "❌ Failed to read package info from Cargo.toml" |
| 47 | + fi |
| 48 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 49 | + echo "tag=v$VERSION" >> $GITHUB_OUTPUT |
| 50 | + echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT |
| 51 | +
|
| 52 | + check: |
| 53 | + needs: setup |
| 54 | + runs-on: ubuntu-latest |
| 55 | + steps: |
| 56 | + - name: Checkout |
| 57 | + uses: actions/checkout@v4 |
| 58 | + - name: Setup rust |
| 59 | + uses: dtolnay/rust-toolchain@stable |
| 60 | + with: |
| 61 | + toolchain: stable |
| 62 | + components: rustfmt |
| 63 | + - name: Format check |
| 64 | + run: cargo fmt -- --check |
| 65 | + |
| 66 | + tests: |
| 67 | + needs: setup |
| 68 | + runs-on: ubuntu-latest |
| 69 | + steps: |
| 70 | + - name: Checkout |
| 71 | + uses: actions/checkout@v4 |
| 72 | + - name: Prepare environment |
| 73 | + uses: dtolnay/rust-toolchain@stable |
| 74 | + with: |
| 75 | + toolchain: stable |
| 76 | + - name: Run tests |
| 77 | + run: cargo test --all-features -- --nocapture |
| 78 | + |
| 79 | + clippy: |
| 80 | + needs: setup |
| 81 | + runs-on: ubuntu-latest |
| 82 | + steps: |
| 83 | + - name: Checkout |
| 84 | + uses: actions/checkout@v4 |
| 85 | + - name: Load clippy |
| 86 | + uses: dtolnay/rust-toolchain@stable |
| 87 | + with: |
| 88 | + toolchain: stable |
| 89 | + components: clippy |
| 90 | + - name: Run clippy |
| 91 | + run: cargo clippy --all-features |
| 92 | + |
| 93 | + build: |
| 94 | + needs: setup |
| 95 | + runs-on: ubuntu-latest |
| 96 | + steps: |
| 97 | + - name: Checkout |
| 98 | + uses: actions/checkout@v4 |
| 99 | + - name: Setup build |
| 100 | + uses: dtolnay/rust-toolchain@stable |
| 101 | + with: |
| 102 | + toolchain: stable |
| 103 | + - name: Build release |
| 104 | + run: cargo check --release --all-features |
| 105 | + |
| 106 | + publish: |
| 107 | + needs: [setup, check, tests, clippy, build] |
| 108 | + if: needs.setup.outputs.tag != '' |
| 109 | + runs-on: ubuntu-latest |
| 110 | + outputs: |
| 111 | + published: ${{ steps.publish.outputs.published }} |
| 112 | + steps: |
| 113 | + - name: Checkout |
| 114 | + uses: actions/checkout@v4 |
| 115 | + - name: Restore toml-cli |
| 116 | + uses: actions/cache@v3 |
| 117 | + with: |
| 118 | + path: ~/.cargo/bin/toml |
| 119 | + key: toml-cli-${{ runner.os }} |
| 120 | + - name: Publish to crates.io |
| 121 | + id: publish |
| 122 | + env: |
| 123 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
| 124 | + run: | |
| 125 | + set -e |
| 126 | + echo "published=false" >> $GITHUB_OUTPUT |
| 127 | + echo "${{ secrets.CARGO_REGISTRY_TOKEN }}" | cargo login |
| 128 | + PACKAGE_NAME=$(toml get Cargo.toml package.name --raw) |
| 129 | + VERSION=${{ needs.setup.outputs.version }} |
| 130 | + if cargo publish --allow-dirty; then |
| 131 | + echo "published=true" >> $GITHUB_OUTPUT |
| 132 | + echo "🎉🎉🎉 PUBLISH SUCCESSFUL 🎉🎉🎉" |
| 133 | + echo "✅ Successfully published $PACKAGE_NAME v$VERSION to crates.io" |
| 134 | + echo "📦 Crates.io: [https://crates.io/crates/$PACKAGE_NAME/$VERSION](https://crates.io/crates/$PACKAGE_NAME/$VERSION)" |
| 135 | + echo "📚 Docs.rs: [https://docs.rs/$PACKAGE_NAME/$VERSION](https://docs.rs/$PACKAGE_NAME/$VERSION)" |
| 136 | + else |
| 137 | + echo "❌ Publish failed" |
| 138 | + fi |
| 139 | +
|
| 140 | + release: |
| 141 | + needs: [setup, check, tests, clippy, build] |
| 142 | + permissions: |
| 143 | + contents: write |
| 144 | + packages: write |
| 145 | + if: needs.setup.outputs.tag != '' |
| 146 | + runs-on: ubuntu-latest |
| 147 | + outputs: |
| 148 | + released: ${{ steps.release.outputs.released }} |
| 149 | + steps: |
| 150 | + - name: Checkout |
| 151 | + uses: actions/checkout@v4 |
| 152 | + with: |
| 153 | + fetch-depth: 0 |
| 154 | + - name: Get package name |
| 155 | + id: package_info |
| 156 | + run: | |
| 157 | + echo "package_name=${{ needs.setup.outputs.package_name }}" >> $GITHUB_OUTPUT |
| 158 | + - name: Check tag status |
| 159 | + id: check_tag |
| 160 | + run: | |
| 161 | + if git tag -l | grep -q "^${{ needs.setup.outputs.tag }}$"; then |
| 162 | + echo "tag_exists=true" >> $GITHUB_OUTPUT |
| 163 | + echo "🏷️ Tag ${{ needs.setup.outputs.tag }} exists locally" |
| 164 | + else |
| 165 | + echo "tag_exists=false" >> $GITHUB_OUTPUT |
| 166 | + echo "🏷️ Tag ${{ needs.setup.outputs.tag }} does not exist locally" |
| 167 | + fi |
| 168 | + if git ls-remote --tags origin | grep -q "refs/tags/${{ needs.setup.outputs.tag }}$"; then |
| 169 | + echo "remote_tag_exists=true" >> $GITHUB_OUTPUT |
| 170 | + echo "🌐 Tag ${{ needs.setup.outputs.tag }} exists on remote" |
| 171 | + else |
| 172 | + echo "remote_tag_exists=false" >> $GITHUB_OUTPUT |
| 173 | + echo "🌐 Tag ${{ needs.setup.outputs.tag }} does not exist on remote" |
| 174 | + fi |
| 175 | + - name: Check release status |
| 176 | + id: check_release |
| 177 | + run: | |
| 178 | + if gh release view "${{ needs.setup.outputs.tag }}" > /dev/null 2>&1; then |
| 179 | + echo "release_exists=true" >> $GITHUB_OUTPUT |
| 180 | + echo "📦 Release ${{ needs.setup.outputs.tag }} already exists" |
| 181 | + else |
| 182 | + echo "release_exists=false" >> $GITHUB_OUTPUT |
| 183 | + echo "📦 Release ${{ needs.setup.outputs.tag }} does not exist" |
| 184 | + fi |
| 185 | + env: |
| 186 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 187 | + - name: Create or update release |
| 188 | + id: release |
| 189 | + run: | |
| 190 | + set -e |
| 191 | + echo "released=false" >> $GITHUB_OUTPUT |
| 192 | + PACKAGE_NAME="${{ steps.package_info.outputs.package_name }}" |
| 193 | + VERSION="${{ needs.setup.outputs.version }}" |
| 194 | + TAG="${{ needs.setup.outputs.tag }}" |
| 195 | + echo "📦 Building source archives..." |
| 196 | + git archive --format=zip --prefix="${PACKAGE_NAME}-${VERSION}/" HEAD > "${PACKAGE_NAME}-${VERSION}.zip" |
| 197 | + git archive --format=tar.gz --prefix="${PACKAGE_NAME}-${VERSION}/" HEAD > "${PACKAGE_NAME}-${VERSION}.tar.gz" |
| 198 | + if [ "${{ steps.check_release.outputs.release_exists }}" = "true" ]; then |
| 199 | + echo "🔄 Updating existing release: $TAG" |
| 200 | + gh release view "$TAG" --json assets --jq '.assets[].name' | while read asset; do |
| 201 | + if [ -n "$asset" ]; then |
| 202 | + echo "🗑️ Deleting asset: $asset" |
| 203 | + gh release delete-asset "$TAG" "$asset" --yes || true |
| 204 | + fi |
| 205 | + done |
| 206 | + if gh release edit "$TAG" \ |
| 207 | + --title "$TAG (Updated $(date '+%Y-%m-%d %H:%M:%S'))" \ |
| 208 | + --notes "Release $TAG - Updated at $(date '+%Y-%m-%d %H:%M:%S UTC') |
| 209 | + ## Changes |
| 210 | + - Version: $VERSION |
| 211 | + - Package: $PACKAGE_NAME |
| 212 | + ## Links |
| 213 | + 📦 [Crate on crates.io](https://crates.io/crates/$PACKAGE_NAME/$VERSION) |
| 214 | + 📚 [Documentation on docs.rs](https://docs.rs/$PACKAGE_NAME/$VERSION) |
| 215 | + 📋 [Commit History](https://github.com/${{ github.repository }}/commits/$TAG)" && \ |
| 216 | + gh release upload "$TAG" "${PACKAGE_NAME}-${VERSION}.zip" "${PACKAGE_NAME}-${VERSION}.tar.gz" --clobber; then |
| 217 | + echo "released=true" >> $GITHUB_OUTPUT |
| 218 | + echo "✅ Updated release $TAG" |
| 219 | + echo "🔖 Tag: $TAG" |
| 220 | + echo "🚀 Release: [GitHub Release](${{ github.server_url }}/${{ github.repository }}/releases/tag/$TAG)" |
| 221 | + else |
| 222 | + echo "❌ Failed to update release" |
| 223 | + fi |
| 224 | + else |
| 225 | + if [ "${{ steps.check_tag.outputs.remote_tag_exists }}" = "false" ]; then |
| 226 | + echo "🏷️ Creating and pushing tag: $TAG" |
| 227 | + git tag "$TAG" |
| 228 | + git push origin "$TAG" |
| 229 | + fi |
| 230 | + echo "🆕 Creating new release: $TAG" |
| 231 | + if gh release create "$TAG" \ |
| 232 | + --title "$TAG (Created $(date '+%Y-%m-%d %H:%M:%S'))" \ |
| 233 | + --notes "Release $TAG - Created at $(date '+%Y-%m-%d %H:%M:%S UTC') |
| 234 | + ## Changes |
| 235 | + - Version: $VERSION |
| 236 | + - Package: $PACKAGE_NAME |
| 237 | + ## Links |
| 238 | + 📦 [Crate on crates.io](https://crates.io/crates/$PACKAGE_NAME/$VERSION) |
| 239 | + 📚 [Documentation on docs.rs](https://docs.rs/$PACKAGE_NAME/$VERSION) |
| 240 | + 📋 [Commit History](https://github.com/${{ github.repository }}/commits/$TAG)" \ |
| 241 | + --latest && \ |
| 242 | + gh release upload "$TAG" "${PACKAGE_NAME}-${VERSION}.zip" "${PACKAGE_NAME}-${VERSION}.tar.gz"; then |
| 243 | + echo "released=true" >> $GITHUB_OUTPUT |
| 244 | + echo "✅ Created release $TAG" |
| 245 | + echo "🔖 Tag: $TAG" |
| 246 | + echo "🚀 Release: [GitHub Release](${{ github.server_url }}/${{ github.repository }}/releases/tag/$TAG)" |
| 247 | + else |
| 248 | + echo "❌ Failed to create release" |
| 249 | + fi |
| 250 | + fi |
| 251 | + env: |
| 252 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments