|
| 1 | +name: Create Release Pull Request |
| 2 | +description: Create a pull request to release a new version |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + version: |
| 8 | + description: 'Version type' |
| 9 | + required: true |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | + |
| 16 | +jobs: |
| 17 | + create-release-pr: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + pull-requests: write |
| 22 | + steps: |
| 23 | + - name: Checkout |
| 24 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 25 | + with: |
| 26 | + persist-credentials: false |
| 27 | + |
| 28 | + - name: Configure Git |
| 29 | + run: | |
| 30 | + git config user.name "github-actions[bot]" |
| 31 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 32 | + |
| 33 | + |
| 34 | + - name: Setup Node.js |
| 35 | + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 |
| 36 | + with: |
| 37 | + node-version: 'lts/*' |
| 38 | + check-latest: true |
| 39 | + package-manager-cache: false |
| 40 | + |
| 41 | + - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 |
| 42 | + with: |
| 43 | + run_install: | |
| 44 | + - recursive: true |
| 45 | + args: [--no-frozen-lockfile] |
| 46 | + |
| 47 | + # No need to install dependencies - npm version works without them |
| 48 | + - name: Version bump |
| 49 | + id: version |
| 50 | + run: | |
| 51 | + VERSION=$(pnpm version "$VERSION_TYPE" --no-git-tag-version) |
| 52 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 53 | + pnpm --recursive exec pnpm pkg set version=$(node -p "JSON.parse(fs.readFileSync('package.json', 'utf8')).version") |
| 54 | + env: |
| 55 | + VERSION_TYPE: ${{ github.event.inputs.version }} |
| 56 | + |
| 57 | + - name: Get release notes |
| 58 | + id: release-notes |
| 59 | + run: | |
| 60 | + # Get the default branch |
| 61 | + DEFAULT_BRANCH=$(gh api "repos/$GITHUB_REPOSITORY" --jq '.default_branch') |
| 62 | + |
| 63 | + # Get the latest release tag using GitHub API |
| 64 | + # Use the exit code to determine if a release exists |
| 65 | + if LAST_TAG=$(gh api "repos/$GITHUB_REPOSITORY/releases/latest" --jq '.tag_name' 2>/dev/null); then |
| 66 | + echo "Previous release found: $LAST_TAG" |
| 67 | + else |
| 68 | + LAST_TAG="" |
| 69 | + echo "No previous releases found - this will be the first release" |
| 70 | + fi |
| 71 | + |
| 72 | + # Generate release notes - only include previous_tag_name if we have a valid previous tag |
| 73 | + echo "Generating release notes for tag: $VERSION" |
| 74 | + if [ -n "$LAST_TAG" ]; then |
| 75 | + echo "Using previous tag: $LAST_TAG" |
| 76 | + RELEASE_NOTES=$(gh api \ |
| 77 | + --method POST \ |
| 78 | + -H "Accept: application/vnd.github+json" \ |
| 79 | + "/repos/$GITHUB_REPOSITORY/releases/generate-notes" \ |
| 80 | + -f "tag_name=$VERSION" \ |
| 81 | + -f "target_commitish=$DEFAULT_BRANCH" \ |
| 82 | + -f "previous_tag_name=$LAST_TAG" \ |
| 83 | + --jq '.body') |
| 84 | + else |
| 85 | + echo "Generating notes from all commits" |
| 86 | + RELEASE_NOTES=$(gh api \ |
| 87 | + --method POST \ |
| 88 | + -H "Accept: application/vnd.github+json" \ |
| 89 | + "/repos/$GITHUB_REPOSITORY/releases/generate-notes" \ |
| 90 | + -f "tag_name=$VERSION" \ |
| 91 | + -f "target_commitish=$DEFAULT_BRANCH" \ |
| 92 | + --jq '.body') |
| 93 | + fi |
| 94 | + |
| 95 | + # Set release notes as environment variable |
| 96 | + echo "RELEASE_NOTES<<EOF" >> "$GITHUB_OUTPUT" |
| 97 | + echo "$RELEASE_NOTES" >> "$GITHUB_OUTPUT" |
| 98 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 99 | + env: |
| 100 | + GH_TOKEN: ${{ github.token }} |
| 101 | + VERSION: ${{ steps.version.outputs.version }} |
| 102 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 103 | + |
| 104 | + - name: Create Pull Request |
| 105 | + uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8 |
| 106 | + env: |
| 107 | + RELEASE_NOTES: ${{ steps.release-notes.outputs.RELEASE_NOTES }} |
| 108 | + VERSION: ${{ steps.version.outputs.version }} |
| 109 | + with: |
| 110 | + branch: release/${{ steps.version.outputs.version }} |
| 111 | + delete-branch: true |
| 112 | + title: "Release ${{ steps.version.outputs.version }}" |
| 113 | + body: | |
| 114 | + ${{ env.RELEASE_NOTES }} |
| 115 | + commit-message: "chore: release ${{ steps.version.outputs.version }}" |
| 116 | + labels: | |
| 117 | + Type: Release |
| 118 | + assignees: ${{ github.actor }} |
| 119 | + draft: true |
0 commit comments