|
| 1 | +name: Release on Version Change |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + uses: ./.github/workflows/build-static.yml |
| 11 | + release: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + needs: ['build'] |
| 14 | + steps: |
| 15 | + - name: Checkout repository |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Get latest released version |
| 21 | + id: releases |
| 22 | + run: | |
| 23 | + LATEST_RELEASE_JSON=$(curl -sL \ |
| 24 | + -H "Accept: application/vnd.github+json" \ |
| 25 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 26 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 27 | + "${{ github.api_url }}/repos/${{ github.repository }}/releases/latest") |
| 28 | +
|
| 29 | + TAG_NAME=$(echo "$LATEST_RELEASE_JSON" | jq -r .tag_name) |
| 30 | + # remove the leading 'v' from the tag name |
| 31 | + echo "latest_version=${TAG_NAME:1}" >> "$GITHUB_OUTPUT" |
| 32 | + echo "🕰️ Latest released version: $TAG_NAME" >> "$GITHUB_STEP_SUMMARY" |
| 33 | +
|
| 34 | + - name: Get current version from Cargo.toml |
| 35 | + id: main_branch |
| 36 | + run: | |
| 37 | + CURRENT_VERSION=$(grep '^version' Cargo.toml | head -n 1 | awk -F '"' '{print $2}') |
| 38 | + echo "version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" |
| 39 | + echo "📝 Version detected in Cargo.toml: $CURRENT_VERSION" >> "$GITHUB_STEP_SUMMARY" |
| 40 | +
|
| 41 | + - name: Compare versions |
| 42 | + id: compare_versions |
| 43 | + run: | |
| 44 | + MAIN_BRANCH_VERSION="${{ steps.main_branch.outputs.version }}" |
| 45 | + LATEST_VERSION="${{ steps.releases.outputs.latest_version }}" |
| 46 | +
|
| 47 | + # Use sort -V to compare the versions numerically. |
| 48 | + # If MAIN_BRANCH_VERSION is truly greater, it will be the second item |
| 49 | + # when sorted, and they must not be identical. |
| 50 | + if [ "$(printf '%s\n' "$MAIN_BRANCH_VERSION" "$LATEST_VERSION" | sort -V | head -n 1)" = "$LATEST_VERSION" ] && \ |
| 51 | + [ "$MAIN_BRANCH_VERSION" != "$LATEST_VERSION" ]; then |
| 52 | + echo "needs_release=true" >> "$GITHUB_OUTPUT" |
| 53 | + fi |
| 54 | +
|
| 55 | + - name: Download x86_64 binary |
| 56 | + if: steps.compare_versions.outputs.needs_release |
| 57 | + uses: actions/download-artifact@v4 |
| 58 | + with: |
| 59 | + name: fb-linux-static-x86_64 |
| 60 | + |
| 61 | + - name: Download aarch64 binary |
| 62 | + if: steps.compare_versions.outputs.needs_release |
| 63 | + uses: actions/download-artifact@v4 |
| 64 | + with: |
| 65 | + name: fb-linux-static-aarch64 |
| 66 | + |
| 67 | + - name: Generate release notes |
| 68 | + if: steps.compare_versions.outputs.needs_release |
| 69 | + id: release_notes |
| 70 | + run: | |
| 71 | + git log v${{ steps.releases.outputs.latest_version }}..HEAD --pretty=format:"* %s [%h](https://github.com/${{ github.repository }}/commit/%H)" > release_notes.md |
| 72 | +
|
| 73 | + - name: Create GitHub Release if version changed |
| 74 | + if: steps.compare_versions.outputs.needs_release |
| 75 | + id: create_release |
| 76 | + uses: actions/create-release@v1 |
| 77 | + env: |
| 78 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + with: |
| 80 | + tag_name: v${{ steps.main_branch.outputs.version }} |
| 81 | + release_name: Release v${{ steps.main_branch.outputs.version }} |
| 82 | + body_path: release_notes.md |
| 83 | + |
| 84 | + - name: Upload x86_64 binary onto release |
| 85 | + if: steps.compare_versions.outputs.needs_release |
| 86 | + uses: actions/upload-release-asset@v1 |
| 87 | + env: |
| 88 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + with: |
| 90 | + upload_url: ${{ steps.create_release.outputs.upload_url }} |
| 91 | + asset_path: fb-linux-static-x86_64 |
| 92 | + asset_name: fb-linux-static-x86_64 |
| 93 | + asset_content_type: application/octet-stream |
| 94 | + |
| 95 | + - name: Upload aarch64 binary onto release |
| 96 | + if: steps.compare_versions.outputs.needs_release |
| 97 | + uses: actions/upload-release-asset@v1 |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + with: |
| 101 | + upload_url: ${{ steps.create_release.outputs.upload_url }} |
| 102 | + asset_path: fb-linux-static-aarch64 |
| 103 | + asset_name: fb-linux-static-aarch64 |
| 104 | + asset_content_type: application/octet-stream |
0 commit comments