@@ -23,70 +23,91 @@ jobs:
2323 runs-on : ubuntu-latest
2424 outputs :
2525 version : ${{ steps.get_version.outputs.version }}
26- branch_exists : ${{ steps.update_docs_branch.outputs.branch_exists }}
27- changes : ${{ steps.update_docs_branch.outputs.changes }}
26+ pr_exists : ${{ steps.update_docs_branch.outputs.pr_exists }}
2827 steps :
2928 - name : Checkout code
3029 uses : actions/checkout@v4
30+
3131 - name : Setup Dart
3232 uses : dart-lang/setup-dart@v1
33+
3334 - name : Install dependencies
3435 run : dart pub get
36+
3537 - name : Analyze project source
3638 run : dart analyze
39+
3740 - name : Get version from pubspec.yaml
3841 id : get_version
3942 run : |
4043 VERSION=$(grep '^version:' pubspec.yaml | head -n1 | cut -d ' ' -f2)
4144 echo "version=$VERSION" >> $GITHUB_OUTPUT
45+
4246 - name : Generate docs
4347 run : dart doc
44- - name : Clean up everything except doc/
45- run : |
46- find . -mindepth 1 -maxdepth 1 ! -name 'doc' ! -name '.git' ! -name '.' ! -name '..' -exec rm -rf {} +
47- - name : Move doc/api/* to root
48- run : |
49- shopt -s dotglob
50- mv doc/api/* .
51- shopt -u dotglob
52- - name : Remove doc/api if empty
48+
49+ - name : Prepare docs for branch
5350 run : |
54- rmdir doc/api || true
51+ # Store the generated docs in a temporary location
52+ cp -r doc/api /tmp/generated_docs
53+
5554 - name : Create or update docs branch
5655 id : update_docs_branch
5756 env :
5857 VERSION : ${{ steps.get_version.outputs.version }}
58+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
5959 run : |
6060 git config user.name "github-actions[bot]"
6161 git config user.email "github-actions[bot]@users.noreply.github.com"
6262 BRANCH=docs-for-$VERSION
63+
64+ # Check for existing PR from docs branch to gh-pages
65+ PR_EXISTS=$(gh pr list --base gh-pages --head "$BRANCH" --state open --json number --jq 'length > 0' || echo false)
66+ echo "pr_exists=$PR_EXISTS" >> $GITHUB_OUTPUT
67+
68+ # Check if docs branch exists
6369 if git ls-remote --exit-code --heads origin "$BRANCH"; then
64- echo "branch_exists=true" >> $GITHUB_OUTPUT
70+ echo "Branch $BRANCH exists, checking it out..."
6571 git fetch origin "$BRANCH"
66- git clean -fd
6772 git checkout "$BRANCH"
6873 git reset --hard origin/$BRANCH
6974 else
70- echo "branch_exists=false" >> $GITHUB_OUTPUT
71- git checkout --orphan "$BRANCH"
72- git reset --hard
75+ echo "Branch $BRANCH does not exist, creating from gh-pages..."
76+ # Create docs branch from gh-pages so there is a common ancestor
77+ git fetch origin gh-pages:gh-pages || git checkout --orphan gh-pages
78+ git checkout gh-pages || git checkout --orphan gh-pages
79+ git checkout -b "$BRANCH"
7380 fi
81+
82+ # Clean up everything except .git
83+ find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name '.' ! -name '..' -exec rm -rf {} +
84+
85+ # Move generated docs from temp location to root
86+ shopt -s dotglob
87+ mv /tmp/generated_docs/* .
88+ shopt -u dotglob
89+
90+ # Add all files
7491 git add .
92+
7593 echo "Files in working directory:"
7694 ls -alR
7795 echo "Files staged for commit:"
7896 git diff --cached --name-status
7997 echo "Files changed (not staged):"
8098 git diff --name-status
8199 echo "Now checking for changes..."
82- # Compare current files against origin/gh-pages by path
83- if git diff --cached --quiet origin/gh-pages -- . 2>/dev/null; then
84- git commit -m "Update docs for $VERSION"
100+
101+ # Check if there are any changes to commit (FIXED LOGIC)
102+ if ! git diff --cached --quiet; then
103+ echo "Changes detected, committing..."
104+ # Get the commit SHA that triggered the workflow
105+ TRIGGER_COMMIT=${{ github.sha }}
106+ COMMIT_MSG="Update docs for $VERSION (generated from $TRIGGER_COMMIT)"
107+ git commit -m "$COMMIT_MSG"
85108 git push -f origin "$BRANCH"
86- echo "changes=true" >> $GITHUB_OUTPUT
87109 else
88110 echo "No changes to commit."
89- echo "changes=false" >> $GITHUB_OUTPUT
90111 fi
91112
92113 create-docs-pr :
@@ -95,15 +116,36 @@ jobs:
95116 contents : write
96117 pull-requests : write
97118 runs-on : ubuntu-latest
98- if : needs.update-docs.outputs.changes == 'true' && needs.update-docs.outputs.branch_exists == 'false'
119+ if : needs.update-docs.outputs.pr_exists == 'false'
99120 steps :
100121 - name : Checkout code
101122 uses : actions/checkout@v4
102123 - name : Create or update PR to gh-pages
103- uses : peter-evans/create-pull-request@v7
104- with :
105- token : ${{ secrets.GITHUB_TOKEN }}
106- base : gh-pages
107- title : Docs for ${{ needs.update-docs.outputs.version }}
108- body : " Automated documentation update for version ${{ needs.update-docs.outputs.version }}."
109- branch : docs-for-${{ needs.update-docs.outputs.version }}
124+ env :
125+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
126+ VERSION : ${{ needs.update-docs.outputs.version }}
127+ run : |
128+ BRANCH=docs-for-$VERSION
129+ PR_TITLE="Docs for $VERSION"
130+
131+ TRIGGER_COMMIT=${{ github.sha }}
132+ PR_BODY="Automated documentation update for version $VERSION.
133+
134+ Triggered by commit $TRIGGER_COMMIT."
135+
136+ # If triggered by a PR, mention it in the PR body
137+ if [ "${{ github.event_name }}" = "pull_request" ] && [ -n "${{ github.event.pull_request.number }}" ]; then
138+ PR_BODY="$PR_BODY
139+
140+ Triggered by #${{ github.event.pull_request.number }}"
141+ fi
142+
143+ # Check if a PR already exists
144+ PR_NUMBER=$(gh pr list --base gh-pages --head "$BRANCH" --state open --json number --jq '.[0].number' || echo "")
145+ if [ -z "$PR_NUMBER" ]; then
146+ echo "Creating new PR..."
147+ gh pr create --base gh-pages --head "$BRANCH" --title "$PR_TITLE" --body "$PR_BODY"
148+ else
149+ echo "PR #$PR_NUMBER already exists. Updating title and body."
150+ gh pr edit "$PR_NUMBER" --title "$PR_TITLE" --body "$PR_BODY"
151+ fi
0 commit comments