|
| 1 | +name: Fork follow upstream |
| 2 | + |
| 3 | +# Daily: keeps this fork's patch series on upstream's newest stable release and publishes it. |
| 4 | +# - Upstream has a newer stable tag: rebase the series onto it, test, build, publish, then move the |
| 5 | +# series branch to the result. |
| 6 | +# - Same tag, but the series has commits no fork release has: test, build and publish them. |
| 7 | +# - Anything fails (conflict, schema change, red test, failed build): open or update one issue and |
| 8 | +# publish nothing, so the last fork release stays current. |
| 9 | +# |
| 10 | +# Settings: variable FORK_SERIES_BRANCH (default main). Secret FORK_PUSH_TOKEN: a token that may push |
| 11 | +# this repository's contents and workflow files (GITHUB_TOKEN may not push commits that change |
| 12 | +# .github/workflows); only a rebase needs it. |
| 13 | +on: |
| 14 | + schedule: |
| 15 | + - cron: "23 6 * * *" |
| 16 | + workflow_dispatch: |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: fork-follow |
| 20 | + cancel-in-progress: false |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +env: |
| 26 | + SERIES_BRANCH: ${{ vars.FORK_SERIES_BRANCH || 'main' }} |
| 27 | + |
| 28 | +jobs: |
| 29 | + plan: |
| 30 | + name: Plan |
| 31 | + runs-on: ubuntu-latest |
| 32 | + timeout-minutes: 15 |
| 33 | + outputs: |
| 34 | + action: ${{ steps.plan.outputs.action }} |
| 35 | + base_tag: ${{ steps.plan.outputs.base_tag }} |
| 36 | + candidate: ${{ steps.plan.outputs.candidate }} |
| 37 | + previous_head: ${{ steps.plan.outputs.previous_head }} |
| 38 | + dropped: ${{ steps.plan.outputs.dropped }} |
| 39 | + steps: |
| 40 | + - name: Checkout the series |
| 41 | + uses: actions/checkout@v6 |
| 42 | + with: |
| 43 | + ref: ${{ env.SERIES_BRANCH }} |
| 44 | + fetch-depth: 0 |
| 45 | + filter: blob:none |
| 46 | + token: ${{ secrets.FORK_PUSH_TOKEN || github.token }} |
| 47 | + |
| 48 | + - id: plan |
| 49 | + name: Plan and rebase |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ github.token }} |
| 52 | + REPORT: ${{ runner.temp }}/fork-follow-report.md |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | + UPSTREAM="$(gh api "repos/$GITHUB_REPOSITORY" --jq '.parent.full_name // empty')" |
| 56 | + if [[ -z "$UPSTREAM" ]]; then |
| 57 | + echo "$GITHUB_REPOSITORY is not a fork, so there is no upstream to follow." > "$REPORT" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + export UPSTREAM |
| 61 | + bash .github/scripts/fork-follow.sh |
| 62 | +
|
| 63 | + - name: Push the rebased series as a candidate |
| 64 | + if: steps.plan.outputs.action == 'rebase' |
| 65 | + env: |
| 66 | + HAS_PUSH_TOKEN: ${{ secrets.FORK_PUSH_TOKEN != '' }} |
| 67 | + REPORT: ${{ runner.temp }}/fork-follow-report.md |
| 68 | + run: | |
| 69 | + set -euo pipefail |
| 70 | + if [[ "$HAS_PUSH_TOKEN" != "true" ]]; then |
| 71 | + echo "Rebasing onto ${{ steps.plan.outputs.base_tag }} needs the FORK_PUSH_TOKEN secret (contents and workflows write)." > "$REPORT" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + git push --force origin "HEAD:refs/heads/fork-follow/candidate" |
| 75 | +
|
| 76 | + - name: Keep the failure report |
| 77 | + if: failure() |
| 78 | + uses: actions/upload-artifact@v7 |
| 79 | + with: |
| 80 | + name: fork-follow-report |
| 81 | + path: ${{ runner.temp }}/fork-follow-report.md |
| 82 | + if-no-files-found: ignore |
| 83 | + retention-days: 7 |
| 84 | + |
| 85 | + release: |
| 86 | + name: Release |
| 87 | + needs: [plan] |
| 88 | + if: needs.plan.outputs.action == 'release' || needs.plan.outputs.action == 'rebase' |
| 89 | + permissions: |
| 90 | + actions: read |
| 91 | + contents: write |
| 92 | + uses: ./.github/workflows/fork-release.yml |
| 93 | + secrets: inherit |
| 94 | + with: |
| 95 | + ref: ${{ needs.plan.outputs.candidate }} |
| 96 | + publish: true |
| 97 | + |
| 98 | + advance: |
| 99 | + name: Move the series branch |
| 100 | + needs: [plan, release] |
| 101 | + if: needs.plan.outputs.action == 'rebase' && needs.release.result == 'success' |
| 102 | + runs-on: ubuntu-latest |
| 103 | + timeout-minutes: 10 |
| 104 | + steps: |
| 105 | + - name: Checkout |
| 106 | + uses: actions/checkout@v6 |
| 107 | + with: |
| 108 | + ref: ${{ needs.plan.outputs.candidate }} |
| 109 | + fetch-depth: 0 |
| 110 | + filter: blob:none |
| 111 | + token: ${{ secrets.FORK_PUSH_TOKEN }} |
| 112 | + |
| 113 | + # The lease refuses if the branch moved since the plan (a fork PR merged meanwhile); the next |
| 114 | + # run rebases again with that change, and the release just published stays valid. |
| 115 | + - name: Move the series branch to the released commit |
| 116 | + run: | |
| 117 | + set -euo pipefail |
| 118 | + git push \ |
| 119 | + --force-with-lease="refs/heads/$SERIES_BRANCH:${{ needs.plan.outputs.previous_head }}" \ |
| 120 | + origin "${{ needs.plan.outputs.candidate }}:refs/heads/$SERIES_BRANCH" |
| 121 | + git push origin --delete fork-follow/candidate || true |
| 122 | +
|
| 123 | + report: |
| 124 | + name: Report |
| 125 | + needs: [plan, release, advance] |
| 126 | + if: always() && needs.plan.outputs.action != 'none' |
| 127 | + runs-on: ubuntu-latest |
| 128 | + timeout-minutes: 5 |
| 129 | + permissions: |
| 130 | + issues: write |
| 131 | + steps: |
| 132 | + - name: Download the failure report |
| 133 | + if: contains(needs.*.result, 'failure') |
| 134 | + continue-on-error: true |
| 135 | + uses: actions/download-artifact@v8 |
| 136 | + with: |
| 137 | + name: fork-follow-report |
| 138 | + path: report |
| 139 | + |
| 140 | + - name: Open, update or close the follow issue |
| 141 | + env: |
| 142 | + GH_TOKEN: ${{ github.token }} |
| 143 | + FAILED: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }} |
| 144 | + BASE_TAG: ${{ needs.plan.outputs.base_tag }} |
| 145 | + DROPPED: ${{ needs.plan.outputs.dropped }} |
| 146 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 147 | + run: | |
| 148 | + set -euo pipefail |
| 149 | + title="Fork cannot follow upstream" |
| 150 | + issue="$(gh issue list --repo "$GITHUB_REPOSITORY" --state open --search "\"$title\" in:title" \ |
| 151 | + --json number,title --jq ".[] | select(.title == \"$title\") | .number" | head -n1)" |
| 152 | + if [[ "$FAILED" == "true" ]]; then |
| 153 | + { |
| 154 | + echo "Run: $RUN_URL" |
| 155 | + echo |
| 156 | + if [[ -f report/fork-follow-report.md ]]; then |
| 157 | + cat report/fork-follow-report.md |
| 158 | + else |
| 159 | + echo "A test, build or publish job failed${BASE_TAG:+ for $BASE_TAG}; see the run. Nothing was published." |
| 160 | + fi |
| 161 | + } > body.md |
| 162 | + if [[ -n "$issue" ]]; then |
| 163 | + gh issue comment "$issue" --repo "$GITHUB_REPOSITORY" --body-file body.md |
| 164 | + else |
| 165 | + gh issue create --repo "$GITHUB_REPOSITORY" --title "$title" --body-file body.md |
| 166 | + fi |
| 167 | + exit 0 |
| 168 | + fi |
| 169 | + note="Published the fork release on $BASE_TAG: $RUN_URL" |
| 170 | + if [[ -n "$DROPPED" ]]; then |
| 171 | + note="$note"$'\n\n'"Dropped because upstream now has them: ${DROPPED//;/, }" |
| 172 | + fi |
| 173 | + echo "$note" >> "$GITHUB_STEP_SUMMARY" |
| 174 | + if [[ -n "$issue" ]]; then |
| 175 | + gh issue close "$issue" --repo "$GITHUB_REPOSITORY" --comment "$note" |
| 176 | + fi |
0 commit comments