ci: stabilize Release workflow PR creation by replacing `gh pr create… #84
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: | |
| - master | |
| env: | |
| HUSKY: 0 | |
| jobs: | |
| release: | |
| permissions: | |
| contents: write # for release publishing | |
| pull-requests: write # for creating PRs | |
| issues: write # additional permission that might be needed | |
| id-token: write # for NPM OIDC trusted publishing | |
| name: Release | |
| env: | |
| commitmsg: ${{ github.event.head_commit.message }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v3 | |
| with: | |
| node-version: lts/* | |
| cache: 'yarn' | |
| - name: Install dependencies | |
| run: yarn install --frozen-lockfile --non-interactive | |
| - name: Update npm for OIDC trusted publishing | |
| run: npm install -g npm@latest | |
| - run: yarn build | |
| - name: Release | |
| id: semantic_release | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: npx semantic-release | |
| - name: Create PR with release changes | |
| if: ${{ success() && steps.semantic_release.outcome == 'success' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # Check if there are any changes to commit | |
| if [[ -n $(git status --porcelain) ]]; then | |
| # Get the version from package.json | |
| VERSION=$(node -p "require('./package.json').version") | |
| BRANCH_NAME="release/v${VERSION}" | |
| echo "Creating PR for release version: ${VERSION}" | |
| # Create and switch to new branch | |
| git checkout -b $BRANCH_NAME | |
| # Configure git | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| # Add and commit changes (only add files that exist) | |
| git add package.json || true | |
| git add yarn.lock || true | |
| git add CHANGELOG.md || true | |
| # Check if there are actually changes to commit after adding | |
| if [[ -n $(git diff --cached --name-only) ]]; then | |
| git commit -m "chore(release): ${VERSION}" | |
| # Push the branch | |
| git push origin $BRANCH_NAME | |
| PR_TITLE="chore(release): ${VERSION}" | |
| PR_BODY="Automated release PR for version ${VERSION} | |
| This PR contains: | |
| - Updated package.json version | |
| - Updated yarn.lock (if changed) | |
| - Updated CHANGELOG.md | |
| Please review and merge to complete the release process." | |
| EXISTING_PR_URL=$(gh api "repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${BRANCH_NAME}&base=master&state=open" --jq '.[0].html_url // empty') | |
| if [[ -n "$EXISTING_PR_URL" ]]; then | |
| echo "Release PR already exists: ${EXISTING_PR_URL}" | |
| else | |
| gh api \ | |
| --method POST \ | |
| "repos/${{ github.repository }}/pulls" \ | |
| -f title="$PR_TITLE" \ | |
| -f body="$PR_BODY" \ | |
| -f head="$BRANCH_NAME" \ | |
| -f base="master" \ | |
| --jq '.html_url' | |
| echo "Created PR for release ${VERSION}" | |
| fi | |
| else | |
| echo "No changes to commit after staging files" | |
| git checkout master | |
| git branch -D $BRANCH_NAME | |
| fi | |
| else | |
| echo "No changes detected after semantic-release" | |
| fi |