|
| 1 | +name: Post release after develop synced (Scheme A) |
| 2 | + |
| 3 | +on: |
| 4 | + repository_dispatch: |
| 5 | + types: [develop-synced] |
| 6 | + |
| 7 | +jobs: |
| 8 | + post_release: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Read version from repository_dispatch payload |
| 16 | + id: meta |
| 17 | + env: |
| 18 | + PAYLOAD_VERSION: ${{ github.event.client_payload.version }} |
| 19 | + run: | |
| 20 | + set -euo pipefail |
| 21 | + if [ -z "${PAYLOAD_VERSION}" ]; then |
| 22 | + echo "No version in client_payload, skip post-release." |
| 23 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 24 | + exit 0 |
| 25 | + fi |
| 26 | + echo "Using version from payload: ${PAYLOAD_VERSION}" |
| 27 | + echo "version=${PAYLOAD_VERSION}" >> "$GITHUB_OUTPUT" |
| 28 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 29 | +
|
| 30 | + - name: Checkout main |
| 31 | + if: steps.meta.outputs.skip != 'true' |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + ref: main |
| 35 | + fetch-depth: 0 |
| 36 | + |
| 37 | + - name: Fetch tags |
| 38 | + if: steps.meta.outputs.skip != 'true' |
| 39 | + run: | |
| 40 | + set -euo pipefail |
| 41 | + git fetch --tags --force |
| 42 | +
|
| 43 | + - name: Check existing tag and release |
| 44 | + id: exist |
| 45 | + if: steps.meta.outputs.skip != 'true' |
| 46 | + env: |
| 47 | + VERSION: ${{ steps.meta.outputs.version }} |
| 48 | + GH_TOKEN: ${{ github.token }} |
| 49 | + run: | |
| 50 | + set -euo pipefail |
| 51 | + TAG="v${VERSION}" |
| 52 | +
|
| 53 | + if git rev-parse "refs/tags/${TAG}" >/dev/null 2>&1; then |
| 54 | + echo "Tag ${TAG} already exists, skip post-release." |
| 55 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | +
|
| 59 | + if gh release view "${TAG}" >/dev/null 2>&1; then |
| 60 | + echo "Release ${TAG} already exists, skip post-release." |
| 61 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | +
|
| 65 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 66 | +
|
| 67 | + - name: Fetch develop changelog |
| 68 | + if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' |
| 69 | + run: | |
| 70 | + set -euo pipefail |
| 71 | + git fetch origin develop:refs/remotes/origin/develop --depth=1 |
| 72 | + git show origin/develop:docs/assets/changelog/en/release.md > release-develop.md |
| 73 | +
|
| 74 | + - name: Extract release body from develop changelog |
| 75 | + id: body |
| 76 | + if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' |
| 77 | + env: |
| 78 | + VERSION: ${{ steps.meta.outputs.version }} |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | + if [ ! -f "release-develop.md" ]; then |
| 82 | + echo "develop changelog file not found, skip post-release." |
| 83 | + echo "has_body=false" >> "$GITHUB_OUTPUT" |
| 84 | + exit 0 |
| 85 | + fi |
| 86 | +
|
| 87 | + if node <<'NODE' |
| 88 | + const fs = require('fs'); |
| 89 | +
|
| 90 | + const version = process.env.VERSION; |
| 91 | + const content = fs.readFileSync('release-develop.md', 'utf8'); |
| 92 | +
|
| 93 | + function escapeRegExp(str) { |
| 94 | + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 95 | + } |
| 96 | +
|
| 97 | + const headerPattern = new RegExp('^#\\s*v?' + escapeRegExp(version) + '\\b', 'm'); |
| 98 | + const match = headerPattern.exec(content); |
| 99 | +
|
| 100 | + if (!match) { |
| 101 | + console.log('No changelog block for version', version, 'found in develop.'); |
| 102 | + process.exit(1); |
| 103 | + } |
| 104 | +
|
| 105 | + const startIndex = match.index; |
| 106 | + const rest = content.slice(startIndex); |
| 107 | +
|
| 108 | + const nextHeaderPattern = /^#\s*v?\d+\.\d+\.\d+[^\n]*$/gm; |
| 109 | + let nextIndex = rest.length; |
| 110 | + let m; |
| 111 | + if ((m = nextHeaderPattern.exec(rest)) !== null && m.index !== 0) { |
| 112 | + nextIndex = m.index; |
| 113 | + } |
| 114 | +
|
| 115 | + const block = rest.slice(0, nextIndex).trimEnd() + '\n'; |
| 116 | + fs.writeFileSync('release-body.md', block, 'utf8'); |
| 117 | + NODE |
| 118 | + then |
| 119 | + echo "has_body=true" >> "$GITHUB_OUTPUT" |
| 120 | + else |
| 121 | + echo "has_body=false" >> "$GITHUB_OUTPUT" |
| 122 | + fi |
| 123 | +
|
| 124 | + - name: Create tag and GitHub Release |
| 125 | + if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' && steps.body.outputs.has_body == 'true' |
| 126 | + env: |
| 127 | + VERSION: ${{ steps.meta.outputs.version }} |
| 128 | + GH_TOKEN: ${{ github.token }} |
| 129 | + run: | |
| 130 | + set -euo pipefail |
| 131 | + TAG="v${VERSION}" |
| 132 | +
|
| 133 | + git fetch origin main:refs/remotes/origin/main --depth=1 |
| 134 | + MAIN_SHA="$(git rev-parse origin/main)" |
| 135 | +
|
| 136 | + echo "Creating tag ${TAG} at ${MAIN_SHA}" |
| 137 | + git tag "${TAG}" "${MAIN_SHA}" |
| 138 | + git push origin "${TAG}" |
| 139 | +
|
| 140 | + echo "Creating GitHub Release ${TAG}" |
| 141 | + gh release create "${TAG}" \ |
| 142 | + --target "main" \ |
| 143 | + --title "${TAG}" \ |
| 144 | + --notes-file "release-body.md" |
0 commit comments