|
| 1 | +name: Promote SDK changes |
| 2 | + |
| 3 | +# Staging is the generator's integration history. Production `next` is the |
| 4 | +# developer-facing queue for the next release. This workflow combines the |
| 5 | +# latest released state with validated staging changes, then advances `next`. |
| 6 | +# Release automation maintains the single versioned PR from `next` to `main`. |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: [main] |
| 10 | + workflow_dispatch: {} |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + |
| 15 | +jobs: |
| 16 | + promote: |
| 17 | + if: github.repository == 'kernel/hypeman-python-staging' |
| 18 | + runs-on: ${{ vars.STLC_RUNNER || 'ubuntu-latest' }} |
| 19 | + concurrency: |
| 20 | + group: stlc-promote |
| 21 | + cancel-in-progress: true |
| 22 | + steps: |
| 23 | + - name: Check out staging |
| 24 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + persist-credentials: false |
| 28 | + |
| 29 | + - name: Mint production token |
| 30 | + id: production-token |
| 31 | + uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1 |
| 32 | + with: |
| 33 | + app-id: ${{ secrets.ADMIN_APP_ID }} |
| 34 | + private-key: ${{ secrets.ADMIN_APP_PRIVATE_KEY }} |
| 35 | + owner: kernel |
| 36 | + repositories: hypeman-python |
| 37 | + permission-contents: write |
| 38 | + permission-pull-requests: write |
| 39 | + permission-workflows: write |
| 40 | + |
| 41 | + - name: Fetch production branches |
| 42 | + id: production |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ steps.production-token.outputs.token }} |
| 45 | + PRODUCTION_REPO: kernel/hypeman-python |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | + git remote add production \ |
| 49 | + "https://x-access-token:${GH_TOKEN}@github.com/${PRODUCTION_REPO}.git" |
| 50 | + if git ls-remote --exit-code --heads production main >/dev/null 2>&1; then |
| 51 | + git fetch production main |
| 52 | + echo "has_main=true" >> "$GITHUB_OUTPUT" |
| 53 | + else |
| 54 | + echo "has_main=false" >> "$GITHUB_OUTPUT" |
| 55 | + fi |
| 56 | + if git ls-remote --exit-code --heads production next >/dev/null 2>&1; then |
| 57 | + git fetch production next |
| 58 | + echo "has_next=true" >> "$GITHUB_OUTPUT" |
| 59 | + else |
| 60 | + echo "has_next=false" >> "$GITHUB_OUTPUT" |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Prepare the next release branch |
| 64 | + env: |
| 65 | + APP_SLUG: ${{ steps.production-token.outputs.app-slug }} |
| 66 | + GH_TOKEN: ${{ steps.production-token.outputs.token }} |
| 67 | + HAS_MAIN: ${{ steps.production.outputs.has_main }} |
| 68 | + HAS_NEXT: ${{ steps.production.outputs.has_next }} |
| 69 | + PRODUCTION_REPO: kernel/hypeman-python |
| 70 | + run: | |
| 71 | + set -euo pipefail |
| 72 | + bot_id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id) |
| 73 | + git config user.name "${APP_SLUG}[bot]" |
| 74 | + git config user.email "${bot_id}+${APP_SLUG}[bot]@users.noreply.github.com" |
| 75 | +
|
| 76 | + open_conflict_pr() { |
| 77 | + source_ref=$1 |
| 78 | + source_name=$2 |
| 79 | + advance_next=$3 |
| 80 | + conflict_branch=stlc/promotion-conflict |
| 81 | +
|
| 82 | + git merge --abort |
| 83 | + existing=$(gh pr list --repo "$PRODUCTION_REPO" --base next \ |
| 84 | + --head "$conflict_branch" --state open --json url --jq '.[0].url // ""') |
| 85 | + if [ -n "$existing" ]; then |
| 86 | + echo "::error title=SDK promotion blocked::Resolve the existing recovery PR: $existing" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | +
|
| 90 | + if [ "$advance_next" = "true" ]; then |
| 91 | + git push production HEAD:refs/heads/next |
| 92 | + fi |
| 93 | + git push production "$source_ref:refs/heads/$conflict_branch" --force |
| 94 | +
|
| 95 | + body=$(mktemp) |
| 96 | + printf '%s\n' \ |
| 97 | + '## SDK promotion conflict' \ |
| 98 | + '' \ |
| 99 | + "The automated promotion could not merge $source_name into the pending next release." \ |
| 100 | + '' \ |
| 101 | + 'Resolve the conflicts on this branch, validate the SDK, mark this PR ready, and merge it with a merge commit.' \ |
| 102 | + '' \ |
| 103 | + 'After merging, rerun the staging Promote SDK changes workflow to include any newer generated changes.' \ |
| 104 | + > "$body" |
| 105 | + recovery_url=$(gh pr create --repo "$PRODUCTION_REPO" --draft \ |
| 106 | + --base next --head "$conflict_branch" \ |
| 107 | + --title 'chore: resolve SDK promotion conflict' --body-file "$body") |
| 108 | + echo "::error title=SDK promotion conflict::Resolve the recovery PR: $recovery_url" |
| 109 | + exit 1 |
| 110 | + } |
| 111 | +
|
| 112 | + if [ "$HAS_MAIN" != "true" ]; then |
| 113 | + # The Python production repository started empty. Seed a minimal |
| 114 | + # default branch so release-please can open the first next -> main |
| 115 | + # release PR while preserving staging as a parent of that release. |
| 116 | + git checkout --orphan stlc/bootstrap-main |
| 117 | + git rm -rf . |
| 118 | + git checkout origin/main -- \ |
| 119 | + .github/workflows/release-please.yml \ |
| 120 | + .release-please-manifest.json \ |
| 121 | + release-please-config.json |
| 122 | + git commit -m 'chore: initialize SDK release history' |
| 123 | + git push production HEAD:refs/heads/main |
| 124 | + git fetch production main |
| 125 | + fi |
| 126 | +
|
| 127 | + if [ "$HAS_NEXT" = "true" ]; then |
| 128 | + git checkout -B stlc/promote-next production/next |
| 129 | + else |
| 130 | + git checkout -B stlc/promote-next production/main |
| 131 | + fi |
| 132 | +
|
| 133 | + if ! git merge-base --is-ancestor production/main HEAD; then |
| 134 | + if ! git merge --no-edit production/main; then |
| 135 | + open_conflict_pr production/main 'production main' false |
| 136 | + fi |
| 137 | + fi |
| 138 | + if ! git merge-base --is-ancestor origin/main HEAD; then |
| 139 | + merge_args=(--no-edit) |
| 140 | + if ! git merge-base production/main origin/main >/dev/null 2>&1; then |
| 141 | + merge_args+=(--allow-unrelated-histories) |
| 142 | + fi |
| 143 | + if ! git merge "${merge_args[@]}" origin/main; then |
| 144 | + open_conflict_pr origin/main 'validated staging changes' true |
| 145 | + fi |
| 146 | + fi |
| 147 | +
|
| 148 | + if [ "$HAS_NEXT" = "true" ]; then |
| 149 | + git merge-base --is-ancestor production/next HEAD |
| 150 | + fi |
| 151 | +
|
| 152 | + - name: Update the pending release |
| 153 | + env: |
| 154 | + GH_TOKEN: ${{ steps.production-token.outputs.token }} |
| 155 | + run: | |
| 156 | + set -euo pipefail |
| 157 | + git push production HEAD:refs/heads/next |
| 158 | + echo "Updated production next; the versioned release PR will be opened or refreshed." |
0 commit comments