|
| 1 | +name: Monthly 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 | + check: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: ${{ github.repository == 'p4lang/p4runtime' && github.ref == 'refs/heads/main' }} |
| 16 | + outputs: |
| 17 | + has_commits: ${{ steps.check_commits.outputs.has_commits }} |
| 18 | + steps: |
| 19 | + - name: Check for new commits since latest tag |
| 20 | + id: check_commits |
| 21 | + env: |
| 22 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + run: | |
| 24 | + REPO="${{ github.repository }}" |
| 25 | + TAG="$(gh api "repos/${REPO}/tags" --jq '.[0].name' 2>/dev/null || echo '')" |
| 26 | + if [ -z "$TAG" ]; then |
| 27 | + echo "No tags found, proceeding with release." |
| 28 | + echo "has_commits=true" >> $GITHUB_OUTPUT |
| 29 | + else |
| 30 | + AHEAD="$(gh api "repos/${REPO}/compare/${TAG}...main" --jq '.ahead_by')" |
| 31 | + if [ "$AHEAD" -eq 0 ]; then |
| 32 | + echo "No new commits since $TAG, skipping release." |
| 33 | + echo "has_commits=false" >> $GITHUB_OUTPUT |
| 34 | + else |
| 35 | + echo "$AHEAD new commits since $TAG." |
| 36 | + echo "has_commits=true" >> $GITHUB_OUTPUT |
| 37 | + fi |
| 38 | + fi |
| 39 | +
|
| 40 | + release-pr: |
| 41 | + needs: check |
| 42 | + if: needs.check.outputs.has_commits == 'true' |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - name: Checkout |
| 46 | + uses: actions/checkout@v6 |
| 47 | + with: |
| 48 | + fetch-depth: 0 |
| 49 | + |
| 50 | + - name: Increment version number |
| 51 | + run: perl -i -pe 's/\b(\d+)(?=\D*$)/$1+1/e' VERSION |
| 52 | + |
| 53 | + - name: Get changelog |
| 54 | + id: changelog |
| 55 | + run: | |
| 56 | + TAG="$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)" |
| 57 | + GIT_LOG="$(git log $TAG..HEAD --oneline --pretty=format:"- %s [%an]")" |
| 58 | + CHANGELOG=$(cat << EOF |
| 59 | + Changelog: |
| 60 | + $GIT_LOG |
| 61 | + EOF |
| 62 | + ) |
| 63 | + CHANGELOG="${CHANGELOG//'#'/''}" |
| 64 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 65 | + echo "$CHANGELOG" >> "$GITHUB_OUTPUT" |
| 66 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + - name: Display changelog |
| 69 | + run: | |
| 70 | + cat <<'EOF' |
| 71 | + ${{ steps.changelog.outputs.content }} |
| 72 | + EOF |
| 73 | +
|
| 74 | + - name: Get version |
| 75 | + run: | |
| 76 | + VERSION="$(cat VERSION)" |
| 77 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 78 | +
|
| 79 | + - name: Get commit message |
| 80 | + id: message |
| 81 | + run: | |
| 82 | + COMMIT_MSG=$(cat << 'EOF' |
| 83 | + Release v${{ env.VERSION }} |
| 84 | +
|
| 85 | + ${{ steps.changelog.outputs.content }} |
| 86 | + EOF |
| 87 | + ) |
| 88 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 89 | + echo "$COMMIT_MSG" >> "$GITHUB_OUTPUT" |
| 90 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 91 | +
|
| 92 | + - name: Get pull request body message |
| 93 | + id: body |
| 94 | + run: | |
| 95 | + MSG=$(cat << 'EOF' |
| 96 | + Auto-generated pull request for version ${{ env.VERSION }}. |
| 97 | +
|
| 98 | + Please use **Squash and merge** to include the changelog in the release message. |
| 99 | +
|
| 100 | + ${{ steps.changelog.outputs.content }} |
| 101 | + EOF |
| 102 | + ) |
| 103 | + echo "content<<EOF" >> "$GITHUB_OUTPUT" |
| 104 | + echo "$MSG" >> "$GITHUB_OUTPUT" |
| 105 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 106 | +
|
| 107 | + - name: Create Pull Request |
| 108 | + uses: peter-evans/create-pull-request@v8 |
| 109 | + with: |
| 110 | + base: main |
| 111 | + add-paths: VERSION |
| 112 | + reviewers: antoninbas, jafingerhut, smolkaj |
| 113 | + commit-message: ${{ steps.message.outputs.content }} |
| 114 | + signoff: false |
| 115 | + branch: v${{ env.VERSION }} |
| 116 | + delete-branch: true |
| 117 | + title: Automated Release v${{ env.VERSION }} |
| 118 | + body: ${{ steps.body.outputs.content }} |
0 commit comments