|
| 1 | +name: Automated Release for ndc-nodejs-lambda |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Version to release (e.g., v1.20.0)" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + |
| 11 | +jobs: |
| 12 | + create-release-pr: |
| 13 | + name: Create Release PR |
| 14 | + runs-on: ubuntu-latest |
| 15 | + timeout-minutes: 10 |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + pull-requests: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Validate version format |
| 22 | + run: | |
| 23 | + VERSION="${{ github.event.inputs.version }}" |
| 24 | + if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 25 | + echo "::error::Version must be in format vX.Y.Z (e.g., v1.20.0)" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +
|
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + fetch-depth: 0 |
| 33 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + |
| 35 | + - name: Check tag doesn't already exist |
| 36 | + run: | |
| 37 | + if git rev-parse "${{ github.event.inputs.version }}" >/dev/null 2>&1; then |
| 38 | + echo "::error::Tag ${{ github.event.inputs.version }} already exists" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Check release branch doesn't exist |
| 43 | + run: | |
| 44 | + BRANCH_NAME="release/${{ github.event.inputs.version }}" |
| 45 | + if git ls-remote --exit-code --heads origin "$BRANCH_NAME" >/dev/null 2>&1; then |
| 46 | + echo "::error::Branch $BRANCH_NAME already exists. Delete it first or use a different version." |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + - name: Setup Node.js |
| 51 | + uses: actions/setup-node@v4 |
| 52 | + with: |
| 53 | + node-version-file: .nvmrc |
| 54 | + registry-url: https://registry.npmjs.org |
| 55 | + |
| 56 | + - name: Configure Git |
| 57 | + run: | |
| 58 | + git config user.name "github-actions[bot]" |
| 59 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 60 | +
|
| 61 | + - name: Get previous tag |
| 62 | + id: get-previous-tag |
| 63 | + run: | |
| 64 | + PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 65 | + echo "previous_tag=$PREVIOUS_TAG" >> $GITHUB_OUTPUT |
| 66 | + echo "Previous tag: ${PREVIOUS_TAG:-'(none)'}" |
| 67 | +
|
| 68 | + - name: Create release branch |
| 69 | + run: | |
| 70 | + BRANCH_NAME="release/${{ github.event.inputs.version }}" |
| 71 | + git checkout -b "$BRANCH_NAME" |
| 72 | + echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV |
| 73 | +
|
| 74 | + - name: Generate changelog entry |
| 75 | + run: | |
| 76 | + VERSION="${{ github.event.inputs.version }}" |
| 77 | + VERSION_NO_V="${VERSION#v}" |
| 78 | + PREVIOUS_TAG="${{ steps.get-previous-tag.outputs.previous_tag }}" |
| 79 | + DATE=$(date +%Y-%m-%d) |
| 80 | +
|
| 81 | + # Generate commit log since last tag |
| 82 | + if [ -n "$PREVIOUS_TAG" ]; then |
| 83 | + echo "Generating changelog from ${PREVIOUS_TAG} to HEAD" |
| 84 | + COMMITS=$(git log "${PREVIOUS_TAG}..HEAD" --pretty=format:"- %s" --no-merges | grep -v "^- Release v" || true) |
| 85 | + else |
| 86 | + echo "No previous tag found, generating full history" |
| 87 | + COMMITS=$(git log --pretty=format:"- %s" --no-merges | grep -v "^- Release v" || true) |
| 88 | + fi |
| 89 | +
|
| 90 | + # Handle empty commits |
| 91 | + if [ -z "$COMMITS" ]; then |
| 92 | + COMMITS="- No notable changes" |
| 93 | + fi |
| 94 | +
|
| 95 | + # Create changelog entry |
| 96 | + CHANGELOG_ENTRY="## [${VERSION_NO_V}] - ${DATE} |
| 97 | +
|
| 98 | + ${COMMITS}" |
| 99 | +
|
| 100 | + # Create or update CHANGELOG.md |
| 101 | + if [ -f CHANGELOG.md ]; then |
| 102 | + # Read existing content, skip header line |
| 103 | + EXISTING_CONTENT=$(tail -n +2 CHANGELOG.md) |
| 104 | +
|
| 105 | + # Write new changelog |
| 106 | + { |
| 107 | + head -1 CHANGELOG.md |
| 108 | + echo "" |
| 109 | + echo "$CHANGELOG_ENTRY" |
| 110 | + echo "" |
| 111 | + echo "$EXISTING_CONTENT" |
| 112 | + } > CHANGELOG.tmp |
| 113 | + mv CHANGELOG.tmp CHANGELOG.md |
| 114 | + else |
| 115 | + { |
| 116 | + echo "# Changelog" |
| 117 | + echo "" |
| 118 | + echo "$CHANGELOG_ENTRY" |
| 119 | + } > CHANGELOG.md |
| 120 | + fi |
| 121 | +
|
| 122 | + echo "Generated changelog entry:" |
| 123 | + echo "$CHANGELOG_ENTRY" |
| 124 | +
|
| 125 | + - name: Update package.json version |
| 126 | + working-directory: ./ndc-lambda-sdk |
| 127 | + run: | |
| 128 | + VERSION="${{ github.event.inputs.version }}" |
| 129 | + VERSION_NO_V="${VERSION#v}" |
| 130 | +
|
| 131 | + # Update package.json version |
| 132 | + npm version "$VERSION_NO_V" --no-git-tag-version |
| 133 | +
|
| 134 | + # Regenerate package-lock.json to ensure consistency |
| 135 | + npm install --package-lock-only |
| 136 | +
|
| 137 | + echo "Updated package.json to version $VERSION_NO_V" |
| 138 | +
|
| 139 | + - name: Commit and push changes |
| 140 | + run: | |
| 141 | + git add CHANGELOG.md ndc-lambda-sdk/package.json ndc-lambda-sdk/package-lock.json |
| 142 | + git commit -m "Release ${{ github.event.inputs.version }}" |
| 143 | + git push origin "${{ env.branch_name }}" |
| 144 | +
|
| 145 | + - name: Generate PR body |
| 146 | + id: pr-body |
| 147 | + run: | |
| 148 | + VERSION="${{ github.event.inputs.version }}" |
| 149 | + VERSION_NO_V="${VERSION#v}" |
| 150 | +
|
| 151 | + # Extract the changelog section for this version |
| 152 | + RELEASE_NOTES=$(awk "/## \[${VERSION_NO_V}\]/{flag=1; next} /## \[/{flag=0} flag" CHANGELOG.md) |
| 153 | +
|
| 154 | + cat > pr-body.md << 'EOF' |
| 155 | + ## Release ${{ github.event.inputs.version }} |
| 156 | +
|
| 157 | + This PR prepares the release for version ${{ github.event.inputs.version }}. |
| 158 | +
|
| 159 | + ### Changes |
| 160 | + EOF |
| 161 | +
|
| 162 | + echo "$RELEASE_NOTES" >> pr-body.md |
| 163 | +
|
| 164 | + cat >> pr-body.md << 'EOF' |
| 165 | +
|
| 166 | + ### Pre-merge Checklist |
| 167 | + - [ ] Review changelog entries for accuracy |
| 168 | + - [ ] Verify version in `ndc-lambda-sdk/package.json` is correct |
| 169 | + - [ ] Ensure all CI checks pass |
| 170 | +
|
| 171 | + ### Post-merge Instructions |
| 172 | + After merging, create and push the tag to trigger the release workflow: |
| 173 | +
|
| 174 | + ```bash |
| 175 | + git checkout main |
| 176 | + git pull origin main |
| 177 | + git tag ${{ github.event.inputs.version }} |
| 178 | + git push origin ${{ github.event.inputs.version }} |
| 179 | + ``` |
| 180 | +
|
| 181 | + > **Note:** The release workflow will automatically publish to npm and create the GitHub release. |
| 182 | + EOF |
| 183 | +
|
| 184 | + - name: Create Pull Request |
| 185 | + env: |
| 186 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 187 | + run: | |
| 188 | + gh pr create \ |
| 189 | + --base main \ |
| 190 | + --head "${{ env.branch_name }}" \ |
| 191 | + --title "Release ${{ github.event.inputs.version }}" \ |
| 192 | + --body-file pr-body.md \ |
| 193 | + --label "release" |
0 commit comments