fix(ci): true zero-downtime release, upload new assets before deletin… #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Release | |
| on: | |
| push: | |
| branches: | |
| - main | |
| permissions: | |
| contents: write | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-go@v5 | |
| with: | |
| go-version-file: go.mod | |
| - run: go test ./... | |
| release: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - uses: actions/setup-go@v5 | |
| with: | |
| go-version-file: go.mod | |
| - name: Determine version | |
| id: version | |
| run: | | |
| VERSION="v0.0.1-build.$(git rev-list --count HEAD).$(git rev-parse --short HEAD)" | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| - name: Build all platforms | |
| run: make dist VERSION=${{ steps.version.outputs.version }} | |
| - name: Delete old assets and update release | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| # Upload new assets first (different filenames, no conflict) | |
| for file in dist/*.tar.gz; do | |
| gh release upload latest "$file" --clobber | |
| done | |
| # Then delete old assets (keep only current version) | |
| CURRENT_VERSION="${{ steps.version.outputs.version }}" | |
| for asset in $(gh api repos/${{ github.repository }}/releases/tags/latest --jq '.assets[] | "\(.id) \(.name)"' 2>/dev/null); do | |
| echo "$asset" | |
| done | while read -r asset_id asset_name; do | |
| if ! echo "$asset_name" | grep -q "$CURRENT_VERSION"; then | |
| gh api -X DELETE repos/${{ github.repository }}/releases/assets/$asset_id 2>/dev/null || true | |
| fi | |
| done | |
| # Update release metadata | |
| gh release edit latest \ | |
| --title "Latest (${{ steps.version.outputs.version }})" \ | |
| --notes "Rolling release from \`main\` branch. | |
| **Version:** \`${{ steps.version.outputs.version }}\` | |
| **Commit:** ${{ github.sha }}" | |
| - name: Ensure release exists | |
| if: failure() | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: latest | |
| name: "Latest (${{ steps.version.outputs.version }})" | |
| files: dist/*.tar.gz | |
| prerelease: true | |
| make_latest: true | |
| generate_release_notes: false | |
| body: | | |
| Rolling release from `main` branch. | |
| **Version:** `${{ steps.version.outputs.version }}` | |
| **Commit:** ${{ github.sha }} | |
| deploy: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Deploy to Cloudflare Pages | |
| uses: cloudflare/wrangler-action@v3 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| command: pages deploy website/public --project-name anycli |