|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + name: Create GitHub Release |
| 15 | + runs-on: ubuntu-latest |
| 16 | + timeout-minutes: 10 |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v6 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Detect new changelog |
| 24 | + id: changelog |
| 25 | + shell: bash |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | +
|
| 29 | + BEFORE_SHA="${{ github.event.before }}" |
| 30 | + if [ -z "$BEFORE_SHA" ] || [ "$BEFORE_SHA" = "0000000000000000000000000000000000000000" ]; then |
| 31 | + BEFORE_SHA="HEAD^" |
| 32 | + fi |
| 33 | +
|
| 34 | + NEW_FILE="$(git diff --name-status "$BEFORE_SHA" "${{ github.sha }}" | awk '$1 == "A" {print $2}' | grep -E '^changelog/v[0-9]+\.[0-9]+\.[0-9]+.*\.md$' | head -n 1 || true)" |
| 35 | +
|
| 36 | + if [ -z "$NEW_FILE" ]; then |
| 37 | + echo "has_release=false" >> "$GITHUB_OUTPUT" |
| 38 | + echo "version=" >> "$GITHUB_OUTPUT" |
| 39 | + echo "file_path=" >> "$GITHUB_OUTPUT" |
| 40 | + echo "No new changelog file found." |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | +
|
| 44 | + VERSION="$(basename "$NEW_FILE" .md)" |
| 45 | + echo "has_release=true" >> "$GITHUB_OUTPUT" |
| 46 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 47 | + echo "file_path=$NEW_FILE" >> "$GITHUB_OUTPUT" |
| 48 | + echo "Detected release changelog: $NEW_FILE" |
| 49 | +
|
| 50 | + - name: Sync package versions |
| 51 | + if: steps.changelog.outputs.has_release == 'true' |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | +
|
| 56 | + TARGET_VERSION="${{ steps.changelog.outputs.version }}" |
| 57 | + TARGET_VERSION="${TARGET_VERSION#v}" |
| 58 | +
|
| 59 | + node - <<'NODE' "$TARGET_VERSION" |
| 60 | + const fs = require("node:fs"); |
| 61 | + const version = process.argv[2]; |
| 62 | +
|
| 63 | + function updatePackageJson(filePath) { |
| 64 | + const raw = fs.readFileSync(filePath, "utf8"); |
| 65 | + const json = JSON.parse(raw); |
| 66 | + json.version = version; |
| 67 | + fs.writeFileSync(filePath, JSON.stringify(json, null, 2) + "\n"); |
| 68 | + } |
| 69 | +
|
| 70 | + function updatePackageLock(filePath) { |
| 71 | + if (!fs.existsSync(filePath)) return; |
| 72 | +
|
| 73 | + const raw = fs.readFileSync(filePath, "utf8"); |
| 74 | + const json = JSON.parse(raw); |
| 75 | + json.version = version; |
| 76 | + if (json.packages && json.packages[""]) { |
| 77 | + json.packages[""].version = version; |
| 78 | + } |
| 79 | + fs.writeFileSync(filePath, JSON.stringify(json, null, 2) + "\n"); |
| 80 | + } |
| 81 | +
|
| 82 | + updatePackageJson("package.json"); |
| 83 | + updatePackageLock("package-lock.json"); |
| 84 | + NODE |
| 85 | +
|
| 86 | + git add package.json package-lock.json |
| 87 | +
|
| 88 | + if git diff --cached --quiet; then |
| 89 | + echo "Package versions are already up to date." |
| 90 | + exit 0 |
| 91 | + fi |
| 92 | +
|
| 93 | + git config user.name "github-actions[bot]" |
| 94 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 95 | + git commit -m "chore(release): sync package version to ${{ steps.changelog.outputs.version }} [skip ci]" |
| 96 | + git push origin HEAD:${{ github.ref_name }} |
| 97 | +
|
| 98 | + - name: Finalize release commit |
| 99 | + id: finalize |
| 100 | + if: steps.changelog.outputs.has_release == 'true' |
| 101 | + shell: bash |
| 102 | + run: | |
| 103 | + echo "build_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" |
| 104 | +
|
| 105 | + - name: Generate release notes |
| 106 | + if: steps.changelog.outputs.has_release == 'true' |
| 107 | + shell: bash |
| 108 | + run: | |
| 109 | + set -euo pipefail |
| 110 | +
|
| 111 | + FILE_PATH="${{ steps.changelog.outputs.file_path }}" |
| 112 | +
|
| 113 | + cat "$FILE_PATH" > release.md |
| 114 | +
|
| 115 | + - name: Create release |
| 116 | + if: steps.changelog.outputs.has_release == 'true' |
| 117 | + uses: softprops/action-gh-release@v2 |
| 118 | + with: |
| 119 | + tag_name: ${{ steps.changelog.outputs.version }} |
| 120 | + name: Release ${{ steps.changelog.outputs.version }} |
| 121 | + body_path: release.md |
| 122 | + target_commitish: ${{ steps.finalize.outputs.build_sha }} |
| 123 | + fail_on_unmatched_files: true |
| 124 | + env: |
| 125 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments