Skip to content

Commit c5e99ef

Browse files
authored
fix(ci): check out the sync branch before transforming docs (#16)
1 parent aa07732 commit c5e99ef

1 file changed

Lines changed: 58 additions & 36 deletions

File tree

.github/workflows/docs-sync.yml

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ on:
1212
paths:
1313
- "docs/**"
1414

15+
# Serialize runs. Two overlapping runs would each check out the same revision of
16+
# the open sync branch, commit divergently, and the loser's push would be
17+
# rejected as non-fast-forward, silently dropping its update. Queue instead of
18+
# cancelling: the transform is deterministic from docs/, so the last run to
19+
# finish always produces the correct final state.
20+
concurrency:
21+
group: docs-sync
22+
cancel-in-progress: false
23+
1524
jobs:
1625
sync:
1726
name: Sync docs to cosmos/docs
@@ -37,69 +46,82 @@ jobs:
3746
path: cosmos-docs
3847
persist-credentials: false
3948

49+
# Select the target branch BEFORE transforming. If an open sync PR exists we
50+
# check its branch out while the tree is still clean, so the transform writes
51+
# directly onto that branch. Switching branches after the transform would either
52+
# abort ("local changes would be overwritten") or, via stash, conflict against
53+
# changes the branch already carries.
54+
- name: Select target branch
55+
id: target
56+
env:
57+
GH_TOKEN: ${{ secrets.DOCS_REPO_TOKEN }}
58+
run: |
59+
cd cosmos-docs
60+
61+
EXISTING=$(gh pr list \
62+
--repo cosmos/docs \
63+
--label "docs-sync" \
64+
--state open \
65+
--json number,headRefName \
66+
--jq '.[0]')
67+
68+
if [ -n "$EXISTING" ]; then
69+
PR_NUMBER=$(echo "$EXISTING" | jq -r '.number')
70+
BRANCH=$(echo "$EXISTING" | jq -r '.headRefName')
71+
git fetch origin "refs/heads/${BRANCH}:refs/remotes/origin/${BRANCH}"
72+
git checkout -B "$BRANCH" "origin/${BRANCH}"
73+
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
74+
echo "Reusing open sync PR #$PR_NUMBER on branch $BRANCH"
75+
else
76+
BRANCH="docs-sync/example-$(date +%Y%m%d-%H%M%S)"
77+
git checkout -b "$BRANCH"
78+
echo "pr_number=" >> "$GITHUB_OUTPUT"
79+
echo "No open sync PR, will create branch $BRANCH"
80+
fi
81+
82+
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
83+
4084
- name: Transform docs → Mintlify format
4185
run: |
4286
python3 scripts/docs-sync/transform.py \
4387
--direction to-mintlify \
4488
--input docs/ \
4589
--output-dir cosmos-docs/sdk/next/tutorials/example/
4690
47-
- name: Check for changes
48-
id: diff
49-
run: |
50-
cd cosmos-docs
51-
[ -n "$(git status --porcelain sdk/next/tutorials/example/)" ] \
52-
&& echo "changed=true" >> "$GITHUB_OUTPUT" \
53-
|| echo "changed=false" >> "$GITHUB_OUTPUT"
54-
55-
- name: Open or update PR on cosmos/docs
56-
if: steps.diff.outputs.changed == 'true'
91+
- name: Commit, then open or update the PR on cosmos/docs
5792
env:
5893
GH_TOKEN: ${{ secrets.DOCS_REPO_TOKEN }}
94+
BRANCH: ${{ steps.target.outputs.branch }}
95+
PR_NUMBER: ${{ steps.target.outputs.pr_number }}
5996
run: |
6097
cd cosmos-docs
6198
6299
git config user.name "docs-sync[bot]"
63100
git config user.email "docs-sync[bot]@users.noreply.github.com"
64101
git remote set-url origin "https://x-access-token:${{ secrets.DOCS_REPO_TOKEN }}@github.com/cosmos/docs.git"
65102
66-
# Check for an existing open sync PR
67-
EXISTING=$(gh pr list \
68-
--repo cosmos/docs \
69-
--label "docs-sync" \
70-
--state open \
71-
--json number,headRefName \
72-
--jq '.[0]')
103+
git add sdk/next/tutorials/example/
73104
74-
if [ -n "$EXISTING" ]; then
75-
PR_NUMBER=$(echo "$EXISTING" | jq -r '.number')
76-
BRANCH=$(echo "$EXISTING" | jq -r '.headRefName')
105+
if git diff --cached --quiet; then
106+
echo "No doc changes to sync, nothing to commit."
107+
exit 0
108+
fi
77109
78-
# Update the existing branch
79-
git fetch origin "$BRANCH"
80-
git checkout "$BRANCH"
81-
git add sdk/next/tutorials/example/
82-
git commit -m "docs: sync example tutorials from cosmos/example [docs-sync]"
83-
git push origin "$BRANCH"
110+
git commit -m "docs: sync example tutorials from cosmos/example [docs-sync]"
111+
git push origin "HEAD:refs/heads/${BRANCH}"
84112
113+
if [ -n "$PR_NUMBER" ]; then
85114
gh pr comment "$PR_NUMBER" \
86115
--repo cosmos/docs \
87-
--body "Sync updated: cosmos/example was updated before this PR merged. Branch has been refreshed please re-review."
116+
--body "Sync updated: cosmos/example was updated before this PR merged. Branch has been refreshed, please re-review."
88117
echo "Updated existing PR #$PR_NUMBER"
89-
90118
else
91-
# No existing PR — create a new branch and open one
92-
BRANCH="docs-sync/example-$(date +%Y%m%d-%H%M%S)"
93-
git checkout -b "$BRANCH"
94-
git add sdk/next/tutorials/example/
95-
git commit -m "docs: sync example tutorials from cosmos/example [docs-sync]"
96-
git push origin "$BRANCH"
97-
98119
gh pr create \
99120
--repo cosmos/docs \
100121
--head "$BRANCH" \
101122
--base main \
102123
--title "docs: sync example tutorials from cosmos/example" \
103124
--label "docs-sync" \
104-
--body "Auto-synced from cosmos/example. Transforms docs/*.md to sdk/next/tutorials/example/*.mdx. Do not edit these files directly — edit the source in cosmos/example and let the sync bot update them."
125+
--body "Auto-synced from cosmos/example. Transforms docs/*.md to sdk/next/tutorials/example/*.mdx. Do not edit these files directly, edit the source in cosmos/example and let the sync bot update them."
126+
echo "Opened a new sync PR on branch $BRANCH"
105127
fi

0 commit comments

Comments
 (0)