|
| 1 | +name: Automated Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + schedule: |
| 6 | + - cron: "17 12 1 * *" # At 12:17 on day-of-month 1 (avoid top-of-hour load) |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + # Check if the event is not triggered by a fork |
| 16 | + if: ${{ github.repository == 'p4lang/ptf' && github.ref == 'refs/heads/main' }} |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v6 |
| 20 | + with: |
| 21 | + # Fetch all history for all branches and tags |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Check for new commits |
| 25 | + id: check_commits |
| 26 | + run: | |
| 27 | + TAG="$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)" |
| 28 | + COMMIT_COUNT="$(git rev-list $TAG..HEAD --count)" |
| 29 | + echo "count=$COMMIT_COUNT" >> $GITHUB_OUTPUT |
| 30 | + if [ "$COMMIT_COUNT" -eq 0 ]; then |
| 31 | + echo "No new commits since $TAG, skipping release." |
| 32 | + else |
| 33 | + echo "$COMMIT_COUNT new commits since $TAG." |
| 34 | + fi |
| 35 | +
|
| 36 | + - name: Increment version number |
| 37 | + if: steps.check_commits.outputs.count != '0' |
| 38 | + run: perl -i -pe 's/\b(\d+)(?=\D*$)/$1+1/e' Version.txt |
| 39 | + |
| 40 | + - name: Get changelog |
| 41 | + if: steps.check_commits.outputs.count != '0' |
| 42 | + id: changelog |
| 43 | + run: | |
| 44 | + TAG="$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)" |
| 45 | + GIT_LOG="$(git log $TAG..HEAD --oneline --pretty=format:"- %s [%an]")" |
| 46 | + CHANGELOG=$(cat << EOF |
| 47 | + Changelog: |
| 48 | + $GIT_LOG |
| 49 | + EOF |
| 50 | + ) |
| 51 | + CHANGELOG="${CHANGELOG//'#'/''}" |
| 52 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 53 | + echo "$CHANGELOG" >> "$GITHUB_OUTPUT" |
| 54 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 55 | +
|
| 56 | + - name: Display changelog |
| 57 | + if: steps.check_commits.outputs.count != '0' |
| 58 | + run: | |
| 59 | + cat <<'EOF' |
| 60 | + ${{ steps.changelog.outputs.content }} |
| 61 | + EOF |
| 62 | +
|
| 63 | + - name: Get version |
| 64 | + if: steps.check_commits.outputs.count != '0' |
| 65 | + run: | |
| 66 | + VERSION="$(cat Version.txt)" |
| 67 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 68 | +
|
| 69 | + - name: Get commit message |
| 70 | + if: steps.check_commits.outputs.count != '0' |
| 71 | + id: message |
| 72 | + run: | |
| 73 | + COMMIT_MSG=$(cat << 'EOF' |
| 74 | + Release v${{ env.VERSION }} |
| 75 | +
|
| 76 | + ${{ steps.changelog.outputs.content }} |
| 77 | + EOF |
| 78 | + ) |
| 79 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 80 | + echo "$COMMIT_MSG" >> "$GITHUB_OUTPUT" |
| 81 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 82 | +
|
| 83 | + - name: Get pull request body message |
| 84 | + if: steps.check_commits.outputs.count != '0' |
| 85 | + id: body |
| 86 | + run: | |
| 87 | + MSG=$(cat << 'EOF' |
| 88 | + Auto-generated pull request for version ${{ env.VERSION }}. |
| 89 | +
|
| 90 | + Please use **Squash and merge** to include the changelog in the release message. |
| 91 | +
|
| 92 | + ${{ steps.changelog.outputs.content }} |
| 93 | + EOF |
| 94 | + ) |
| 95 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 96 | + echo "$MSG" >> "$GITHUB_OUTPUT" |
| 97 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 98 | +
|
| 99 | + - name: Create Pull Request |
| 100 | + if: steps.check_commits.outputs.count != '0' |
| 101 | + uses: peter-evans/create-pull-request@v8 |
| 102 | + with: |
| 103 | + base: main |
| 104 | + add-paths: Version.txt |
| 105 | + reviewers: fruffy, jafingerhut |
| 106 | + commit-message: ${{ steps.message.outputs.content }} |
| 107 | + signoff: false |
| 108 | + branch: v${{ env.VERSION }} |
| 109 | + delete-branch: true |
| 110 | + title: Automated Release v${{ env.VERSION }} |
| 111 | + body: ${{ steps.body.outputs.content }} |
0 commit comments