Skip to content

Commit e2ca5fe

Browse files
committed
ci: add stlc promote and back-sync workflows
1 parent d66bfcf commit e2ca5fe

2 files changed

Lines changed: 305 additions & 0 deletions

File tree

‎.github/workflows/stlc-promote.yml‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# NOTE(stlc migration, 2026-08-25): repo names below point at the
2+
# stainless-sdks placeholder repos used to validate the migration.
3+
# At cutover, replace:
4+
# - the production repo with openlayer-ai/openlayer-python
5+
# - the staging/config repo owner with your own org after transfer
6+
name: Promote SDKs
7+
8+
# Promote staging to production by fast-forwarding production main up to staging,
9+
# preserving SHAs so the trunks stay identical and linear (nothing to heal on the
10+
# next stlc build). Manual dispatch by design: a maintainer promotes the accumulated
11+
# batch to cut a release; release-please then opens its version PR on production.
12+
on:
13+
workflow_dispatch: {}
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
promote:
20+
# Runner comes from the STLC_RUNNER repo/org variable when set; defaults to GitHub-hosted.
21+
runs-on: ${{ vars.STLC_RUNNER || 'ubuntu-latest' }}
22+
if: github.repository == 'stainless-sdks/openlayer-python-staging'
23+
# Optional gate: add required reviewers to this environment to approve each
24+
# promote. With none it only scopes secrets and adds no gate. Remove if unused.
25+
environment: production
26+
env:
27+
PRODUCTION_REPO: stainless-sdks/openlayer-python-prod
28+
GH_TOKEN: ${{ secrets.PRODUCTION_REPO_TOKEN }}
29+
steps:
30+
- name: Check out staging
31+
uses: actions/checkout@v6
32+
with:
33+
fetch-depth: 0
34+
persist-credentials: false
35+
36+
- name: Fetch production main
37+
run: |
38+
git remote add production \
39+
"https://x-access-token:${GH_TOKEN}@github.com/${PRODUCTION_REPO}.git"
40+
git fetch production main
41+
42+
- name: Check whether production already has staging's content
43+
id: diff
44+
run: |
45+
# After a release, production has release-please commits staging lacks, so compare trees.
46+
MERGED=$(git merge-tree --write-tree production/main origin/main) || MERGED=conflict
47+
PRODUCTION_TREE=$(git rev-parse 'production/main^{tree}')
48+
if [ "$MERGED" = "$PRODUCTION_TREE" ]; then
49+
echo "Production already contains staging's content. Nothing to promote."
50+
echo "synced=true" >> "$GITHUB_OUTPUT"
51+
else
52+
echo "synced=false" >> "$GITHUB_OUTPUT"
53+
fi
54+
55+
- name: Promote staging to production (fast-forward)
56+
if: steps.diff.outputs.synced == 'false'
57+
run: |
58+
# Refuse unless production is an ancestor of staging: otherwise the trunks
59+
# have forked (production advanced without a back-sync) and FF is unsafe.
60+
if ! git merge-base --is-ancestor production/main origin/main; then
61+
echo "::error title=Promote blocked::production/main is not an ancestor of staging main. Back-sync production into staging first."
62+
exit 1
63+
fi
64+
git push production origin/main:refs/heads/main
65+
echo "Fast-forwarded production/main to staging/main."
66+
67+
- name: Alert on failure
68+
if: failure()
69+
env:
70+
ALERT_WEBHOOK_URL: ${{ secrets.STLC_ALERT_WEBHOOK_URL }}
71+
run: |
72+
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
73+
msg="stlc promote failed in ${{ github.repository }}. A stalled promote or back-sync lets custom-code tracking drift, which later builds refuse on — investigate before the next build. Run: $run_url"
74+
echo "::error title=stlc workflow failed::$msg"
75+
{ echo "### ⚠️ stlc workflow failed"; echo ""; echo "$msg"; } >> "$GITHUB_STEP_SUMMARY"
76+
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then
77+
curl -sS -X POST -H 'Content-Type: application/json' \
78+
-d "$(jq -n --arg text "$msg" '{text:$text}')" "$ALERT_WEBHOOK_URL" \
79+
|| echo "::warning::Alert webhook POST failed"
80+
fi

‎.github/workflows/stlc-sync.yml‎

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# NOTE(stlc migration, 2026-08-25): repo names below point at the
2+
# stainless-sdks placeholder repos used to validate the migration.
3+
# At cutover, replace:
4+
# - the production repo with openlayer-ai/openlayer-python
5+
# - the staging/config repo owner with your own org after transfer
6+
name: Sync SDK repos
7+
8+
# Keeps the staging and production trunks in sync and the config repo's
9+
# tracking files fresh. Each job self-routes by repo + event, so this one file
10+
# can live in both repos and only the right job runs. The dispatch jobs are
11+
# eager-only (the polls cover them) and no-op when their token isn't set.
12+
on:
13+
schedule:
14+
# back-sync poll: a cheap pure-git check, twice hourly so an unsynced production
15+
# change (e.g. a community PR between releases) can't hold codegen for long.
16+
- cron: '7,37 * * * *'
17+
workflow_dispatch: {}
18+
repository_dispatch:
19+
types: [prod-released]
20+
release:
21+
types: [published]
22+
push:
23+
# main only. stlc preview/integrated/codegen branches never push to main.
24+
branches: [main]
25+
26+
jobs:
27+
back-sync:
28+
# Fast-forward production main back onto staging so the trunks stay identical.
29+
# Runner comes from the STLC_RUNNER repo/org variable when set; defaults to GitHub-hosted.
30+
runs-on: ${{ vars.STLC_RUNNER || 'ubuntu-latest' }}
31+
if: >-
32+
github.repository == 'stainless-sdks/openlayer-python-staging' &&
33+
(github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
34+
permissions:
35+
contents: write
36+
concurrency:
37+
group: stlc-back-sync
38+
cancel-in-progress: true
39+
env:
40+
PRODUCTION_REPO: stainless-sdks/openlayer-python-prod
41+
steps:
42+
- name: Check out staging
43+
uses: actions/checkout@v6
44+
with:
45+
fetch-depth: 0
46+
47+
- name: Fetch production main
48+
env:
49+
PRODUCTION_REPO_TOKEN: ${{ secrets.PRODUCTION_REPO_TOKEN }}
50+
run: |
51+
# Public production reads with no credential; a private production
52+
# repo needs PRODUCTION_REPO_TOKEN (the same token the promote uses).
53+
if [ -n "${PRODUCTION_REPO_TOKEN:-}" ]; then
54+
git remote add production "https://x-access-token:${PRODUCTION_REPO_TOKEN}@github.com/${PRODUCTION_REPO}.git"
55+
else
56+
git remote add production "https://github.com/${PRODUCTION_REPO}.git"
57+
fi
58+
# actions/checkout persists this repo's token as an auth header, which
59+
# outranks the remote URL credential; blank it for this fetch only.
60+
git -c "http.https://github.com/.extraheader=" fetch production main
61+
62+
- name: Check whether production has content staging lacks
63+
id: diff
64+
run: |
65+
# Content compare: would merging production into staging change its tree?
66+
# If not, staging already has production's content (release-please commits).
67+
MERGED=$(git merge-tree --write-tree origin/main production/main) || MERGED=conflict
68+
STAGING_TREE=$(git rev-parse 'origin/main^{tree}')
69+
if [ "$MERGED" = "$STAGING_TREE" ]; then
70+
echo "Staging already has production's content. Nothing to pull back."
71+
echo "behind=false" >> "$GITHUB_OUTPUT"
72+
else
73+
echo "behind=true" >> "$GITHUB_OUTPUT"
74+
fi
75+
76+
- name: Sync production to staging (fast-forward)
77+
if: steps.diff.outputs.behind == 'true'
78+
run: |
79+
# Refuse unless staging is an ancestor of production: otherwise the
80+
# trunks have forked and a fast-forward would be unsafe.
81+
if ! git merge-base --is-ancestor origin/main production/main; then
82+
echo "::error title=Back-sync blocked::staging main is not an ancestor of production/main."
83+
exit 1
84+
fi
85+
git push origin production/main:refs/heads/main
86+
echo "Fast-forwarded staging/main to production/main."
87+
88+
- name: Alert on failure
89+
if: failure()
90+
env:
91+
ALERT_WEBHOOK_URL: ${{ secrets.STLC_ALERT_WEBHOOK_URL }}
92+
run: |
93+
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
94+
msg="stlc back-sync (sync from production) failed in ${{ github.repository }}. A stalled back-sync lets custom-code tracking drift, which later builds refuse on — investigate before the next build. Run: $run_url"
95+
echo "::error title=stlc workflow failed::$msg"
96+
{ echo "### ⚠️ stlc workflow failed"; echo ""; echo "$msg"; } >> "$GITHUB_STEP_SUMMARY"
97+
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then
98+
curl -sS -X POST -H 'Content-Type: application/json' \
99+
-d "$(jq -n --arg text "$msg" '{text:$text}')" "$ALERT_WEBHOOK_URL" \
100+
|| echo "::warning::Alert webhook POST failed"
101+
fi
102+
103+
notify-back-sync:
104+
# On a published release, tell staging to back-sync now instead of waiting for
105+
# the poll. Dispatch-only: it cannot write production or staging contents.
106+
runs-on: ${{ vars.STLC_RUNNER || 'ubuntu-latest' }}
107+
if: >-
108+
github.repository == 'stainless-sdks/openlayer-python-prod' &&
109+
(github.event_name == 'release' || github.event_name == 'workflow_dispatch')
110+
permissions:
111+
contents: read
112+
env:
113+
STAGING_REPO: stainless-sdks/openlayer-python-staging
114+
steps:
115+
- name: Dispatch back-sync to staging
116+
env:
117+
DISPATCH_TOKEN: ${{ secrets.STAGING_DISPATCH_TOKEN }}
118+
REF_NAME: ${{ github.ref_name }}
119+
run: |
120+
set -euo pipefail
121+
122+
if [ -z "${DISPATCH_TOKEN:-}" ]; then
123+
echo "::notice::STAGING_DISPATCH_TOKEN not configured — skipping the eager back-sync notify. The staging repo's twice-hourly poll covers this."
124+
exit 0
125+
fi
126+
127+
payload=$(jq -n --arg ref "$REF_NAME" '{event_type:"prod-released",client_payload:{ref:$ref}}')
128+
code=$(curl -sS -o /tmp/dispatch.txt -w '%{http_code}' -X POST \
129+
-H "Authorization: Bearer ${DISPATCH_TOKEN}" \
130+
-H "Accept: application/vnd.github+json" \
131+
-H "X-GitHub-Api-Version: 2022-11-28" \
132+
"https://api.github.com/repos/${STAGING_REPO}/dispatches" \
133+
-d "$payload")
134+
if [ "$code" = "204" ]; then
135+
echo "Back-sync dispatched to ${STAGING_REPO}."
136+
else
137+
echo "Dispatch failed (HTTP $code)" >&2; cat /tmp/dispatch.txt >&2; exit 1
138+
fi
139+
140+
- name: Alert on failure
141+
if: failure()
142+
env:
143+
ALERT_WEBHOOK_URL: ${{ secrets.STLC_ALERT_WEBHOOK_URL }}
144+
run: |
145+
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
146+
msg="stlc release back-sync trigger failed in ${{ github.repository }} — the staging repo was NOT notified to back-sync this release (likely an expired STAGING_DISPATCH_TOKEN). Staging catches up on its next poll, but verify the token. Run: $run_url"
147+
echo "::error title=stlc workflow failed::$msg"
148+
{ echo "### ⚠️ stlc workflow failed"; echo ""; echo "$msg"; } >> "$GITHUB_STEP_SUMMARY"
149+
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then
150+
curl -sS -X POST -H 'Content-Type: application/json' \
151+
-d "$(jq -n --arg text "$msg" '{text:$text}')" "$ALERT_WEBHOOK_URL" \
152+
|| echo "::warning::Alert webhook POST failed"
153+
fi
154+
155+
seal-dispatch:
156+
# When out-of-band custom code lands on staging main, tell the config repo to
157+
# re-seal now instead of waiting for its scheduled sync. The loop guards skip
158+
# stlc's own pushes, so the bot's commits can't trigger a re-seal loop.
159+
runs-on: ${{ vars.STLC_RUNNER || 'ubuntu-latest' }}
160+
if: >-
161+
github.repository == 'stainless-sdks/openlayer-python-staging' &&
162+
github.event_name == 'push'
163+
permissions:
164+
contents: read
165+
concurrency:
166+
group: seal-dispatch-${{ github.ref }}
167+
cancel-in-progress: false
168+
env:
169+
CONFIG_REPO: stainless-sdks/openlayer-config
170+
steps:
171+
- name: Loop-guard and send re-seal dispatch
172+
env:
173+
DISPATCH_TOKEN: ${{ secrets.CONFIG_DISPATCH_TOKEN }}
174+
HEAD_MSG: ${{ github.event.head_commit.message }}
175+
HEAD_AUTHOR_NAME: ${{ github.event.head_commit.author.name }}
176+
SHA: ${{ github.sha }}
177+
REPO: ${{ github.repository }}
178+
run: |
179+
set -euo pipefail
180+
181+
# Loop guard 1: skip the stlc "Build SDK" squash commit (Stainless-Generated-From trailer).
182+
if printf '%s' "$HEAD_MSG" | grep -q 'Stainless-Generated-From'; then
183+
echo "Head commit is an stlc build — skipping re-seal dispatch."
184+
exit 0
185+
fi
186+
187+
# Loop guard 2: skip stlc-bot commits (e.g. a regeneration commit).
188+
if [ "$HEAD_AUTHOR_NAME" = "stlc-bot" ]; then
189+
echo "Head commit authored by stlc-bot — skipping re-seal dispatch."
190+
exit 0
191+
fi
192+
193+
if [ -z "${DISPATCH_TOKEN:-}" ]; then
194+
echo "::notice::CONFIG_DISPATCH_TOKEN not configured — skipping the eager re-seal. The config repo's scheduled sync covers this."
195+
exit 0
196+
fi
197+
198+
payload=$(jq -n --arg sha "$SHA" --arg repo "$REPO" \
199+
'{event_type:"seal-custom-code",client_payload:{target:"all",sha:$sha,repo:$repo}}')
200+
code=$(curl -sS -o /tmp/dispatch.txt -w '%{http_code}' -X POST \
201+
-H "Authorization: Bearer ${DISPATCH_TOKEN}" \
202+
-H "Accept: application/vnd.github+json" \
203+
-H "X-GitHub-Api-Version: 2022-11-28" \
204+
"https://api.github.com/repos/${CONFIG_REPO}/dispatches" \
205+
-d "$payload")
206+
if [ "$code" = "204" ]; then
207+
echo "Re-seal dispatched to ${CONFIG_REPO}."
208+
else
209+
echo "Dispatch failed (HTTP $code)" >&2; cat /tmp/dispatch.txt >&2; exit 1
210+
fi
211+
212+
- name: Alert on failure
213+
if: failure()
214+
env:
215+
ALERT_WEBHOOK_URL: ${{ secrets.STLC_ALERT_WEBHOOK_URL }}
216+
run: |
217+
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
218+
msg="stlc seal-dispatch failed in ${{ github.repository }} — the config repo was NOT notified to re-seal (likely an expired CONFIG_DISPATCH_TOKEN). The config repo's scheduled sync is the backstop, but verify the token. Run: $run_url"
219+
echo "::error title=stlc workflow failed::$msg"
220+
{ echo "### ⚠️ stlc workflow failed"; echo ""; echo "$msg"; } >> "$GITHUB_STEP_SUMMARY"
221+
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then
222+
curl -sS -X POST -H 'Content-Type: application/json' \
223+
-d "$(jq -n --arg text "$msg" '{text:$text}')" "$ALERT_WEBHOOK_URL" \
224+
|| echo "::warning::Alert webhook POST failed"
225+
fi

0 commit comments

Comments
 (0)