Update release.yml #4
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 Static Site | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| build-and-release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Setup pnpm | |
| uses: pnpm/action-setup@v2 | |
| with: | |
| version: 8 | |
| - name: Install Dependencies | |
| run: pnpm install | |
| - name: Build and Export | |
| run: | | |
| npx next build | |
| npx next export | |
| - name: Get Version from package.json | |
| id: get_version | |
| run: | | |
| version=v$(jq -r .version package.json) | |
| echo "version=$version" >> $GITHUB_OUTPUT | |
| - name: Check if Release Already Exists | |
| id: check_release | |
| run: | | |
| exists=$(gh release view ${{ steps.get_version.outputs.version }} --json tagName -q '.tagName' || echo "not_found") | |
| if [ "$exists" != "not_found" ]; then | |
| echo "release_exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "release_exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Exit if Release Already Exists | |
| if: steps.check_release.outputs.release_exists == 'true' | |
| run: | | |
| echo "Release already exists. Skipping..." | |
| exit 0 | |
| - name: Get Previous Tag | |
| id: prev_tag | |
| run: | | |
| tag=$(git tag --sort=-creatordate | grep '^v' | grep -v "${{ steps.get_version.outputs.version }}" | head -n1 || echo "none") | |
| echo "previous_tag=$tag" >> $GITHUB_OUTPUT | |
| - name: Generate Release Notes | |
| id: release_notes | |
| run: | | |
| echo "🔄 更新日志(${{ steps.prev_tag.outputs.previous_tag }} → ${{ steps.get_version.outputs.version }})" > release.md | |
| echo "" >> release.md | |
| if [ "${{ steps.prev_tag.outputs.previous_tag }}" != "none" ]; then | |
| git log ${{ steps.prev_tag.outputs.previous_tag }}..HEAD --pretty=format:'- [%s](https://github.com/${{ github.repository }}/commit/%H) by @%an' >> release.md | |
| else | |
| git log --pretty=format:'- [%s](https://github.com/${{ github.repository }}/commit/%H) by @%an' >> release.md | |
| fi | |
| echo "release_body<<EOF" >> $GITHUB_OUTPUT | |
| cat release.md >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Create ZIP Archive | |
| run: | | |
| cd out | |
| zip -r ../site-${{ steps.get_version.outputs.version }}.zip ./* | |
| cd .. | |
| - name: Create GitHub Release | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: ${{ steps.get_version.outputs.version }} | |
| name: Release ${{ steps.get_version.outputs.version }} | |
| body: ${{ steps.release_notes.outputs.release_body }} | |
| files: site-${{ steps.get_version.outputs.version }}.zip | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |