|
| 1 | +name: build CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - "releases/**" |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: write # Needed for creating a GitHub Release, pushing new commits/tags |
| 14 | + steps: |
| 15 | + - name: Checkout code with full history |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 # Required to get full git history for log operations |
| 19 | + - name: Use Node.js |
| 20 | + uses: actions/setup-node@v4 |
| 21 | + with: |
| 22 | + node-version: "24.x" |
| 23 | + - run: npm ci |
| 24 | + - run: npm run build --if-present |
| 25 | + #- run: npm test |
| 26 | + |
| 27 | + # Conditional steps for release process on 'releases/**' branches |
| 28 | + - name: Get latest tag and commit messages for release notes |
| 29 | + id: get_release_info |
| 30 | + if: startsWith(github.ref, 'refs/heads/main/') |
| 31 | + run: | |
| 32 | + # Get the latest semantic version tag reachable from HEAD |
| 33 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 34 | + echo "Found latest tag: '$LATEST_TAG'" |
| 35 | +
|
| 36 | + # Get commit messages since the latest tag, or all if no tag exists |
| 37 | + if [ -z "$LATEST_TAG" ]; then |
| 38 | + echo "No previous tag found. Getting all commit messages." |
| 39 | + COMMIT_MESSAGES=$(git log --pretty=format:"- %s" HEAD) |
| 40 | + else |
| 41 | + echo "Getting commit messages since tag '$LATEST_TAG'." |
| 42 | + COMMIT_MESSAGES=$(git log --pretty=format:"- %s" ${LATEST_TAG}..HEAD) |
| 43 | + fi |
| 44 | +
|
| 45 | + # Ensure COMMIT_MESSAGES is not empty; if it is, provide a default |
| 46 | + if [ -z "$COMMIT_MESSAGES" ]; then |
| 47 | + COMMIT_MESSAGES="No new changes since last release." |
| 48 | + fi |
| 49 | +
|
| 50 | + echo "Collected commit messages:" |
| 51 | + echo "$COMMIT_MESSAGES" |
| 52 | +
|
| 53 | + # Output commit messages for subsequent steps |
| 54 | + echo "commit_messages<<EOF" >> $GITHUB_OUTPUT |
| 55 | + echo "$COMMIT_MESSAGES" >> $GITHUB_OUTPUT |
| 56 | + echo "EOF" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + - name: Increment version, create commit and tag |
| 59 | + id: npm_version_and_tag |
| 60 | + if: startsWith(github.ref, 'refs/heads/main/') |
| 61 | + run: | |
| 62 | + # Use --no-git-tag-version to prevent npm from creating a tag/commit immediately. |
| 63 | + # We'll handle the commit and tag manually to include custom messages and push. |
| 64 | + NEW_VERSION=$(npm version patch --no-git-tag-version) |
| 65 | + echo "Determined new version: $NEW_VERSION" |
| 66 | +
|
| 67 | + # Configure Git user for the commit |
| 68 | + git config user.name "github-actions[bot]" |
| 69 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 70 | +
|
| 71 | + # Add the modified package.json to staging |
| 72 | + git add package.json |
| 73 | +
|
| 74 | + # Create a new commit with the version bump and release notes |
| 75 | + git commit -m "Release $NEW_VERSION" -m "${{ steps.get_release_info.outputs.commit_messages }}" |
| 76 | +
|
| 77 | + # Create the new tag |
| 78 | + git tag $NEW_VERSION |
| 79 | +
|
| 80 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 81 | +
|
| 82 | + - name: Push new commit and tag to GitHub |
| 83 | + if: startsWith(github.ref, 'refs/heads/main/') |
| 84 | + run: | |
| 85 | + # Push the new commit to the current branch |
| 86 | + git push origin HEAD:${{ github.ref_name }} |
| 87 | + # Push the newly created tag |
| 88 | + git push origin ${{ steps.npm_version_and_tag.outputs.new_version }} |
| 89 | +
|
| 90 | + - name: Create GitHub Release |
| 91 | + if: startsWith(github.ref, 'refs/heads/main/') |
| 92 | + uses: softprops/action-gh-release@v1 |
| 93 | + with: |
| 94 | + tag_name: ${{ steps.npm_version_and_tag.outputs.new_version }} |
| 95 | + name: Release ${{ steps.npm_version_and_tag.outputs.new_version }} |
| 96 | + body: | |
| 97 | + Automated release from branch `${{ github.ref_name }}`. |
| 98 | +
|
| 99 | + ### Changes: |
| 100 | + ${{ steps.get_release_info.outputs.commit_messages }} |
| 101 | + draft: false |
| 102 | + prerelease: false |
| 103 | + env: |
| 104 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Use the default GitHub token for authentication |
0 commit comments