-
-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (126 loc) · 4.94 KB
/
Copy pathdocs.yaml
File metadata and controls
145 lines (126 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
name: Update Docs
on:
push:
branches:
- stable
paths:
- 'lib/**'
- 'pubspec.yaml'
- '.github/workflows/docs.yaml'
pull_request:
branches:
- stable
paths:
- 'lib/**'
- 'pubspec.yaml'
- '.github/workflows/docs.yaml'
jobs:
update-docs:
permissions:
contents: write
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get_version.outputs.version }}
pr_exists: ${{ steps.update_docs_branch.outputs.pr_exists }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Dart
uses: dart-lang/setup-dart@v1
- name: Install dependencies
run: dart pub get
- name: Analyze project source
run: dart analyze
- name: Get version from pubspec.yaml
id: get_version
run: |
VERSION=$(grep '^version:' pubspec.yaml | head -n1 | cut -d ' ' -f2)
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Generate docs
run: dart doc
- name: Prepare docs for branch
run: |
# Store the generated docs in a temporary location
cp -r doc/api /tmp/generated_docs
- name: Create or update docs branch
id: update_docs_branch
env:
VERSION: ${{ steps.get_version.outputs.version }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
BRANCH=docs-for-$VERSION
# Check for existing PR from docs branch to gh-pages
PR_EXISTS=$(gh pr list --base gh-pages --head "$BRANCH" --state open --json number --jq 'length > 0' || echo false)
echo "pr_exists=$PR_EXISTS" >> $GITHUB_OUTPUT
# Check if docs branch exists
if git ls-remote --exit-code --heads origin "$BRANCH"; then
echo "Branch $BRANCH exists, checking it out..."
git fetch origin "$BRANCH"
git checkout "$BRANCH"
git reset --hard origin/$BRANCH
else
echo "Branch $BRANCH does not exist, creating from gh-pages..."
# Create docs branch from gh-pages so there is a common ancestor
git fetch origin gh-pages:gh-pages || git checkout --orphan gh-pages
git checkout gh-pages || git checkout --orphan gh-pages
git checkout -b "$BRANCH"
fi
# Clean up everything except .git
find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name '.' ! -name '..' -exec rm -rf {} +
# Move generated docs from temp location to root
shopt -s dotglob
mv /tmp/generated_docs/* .
shopt -u dotglob
# Add all files
git add .
echo "Files in working directory:"
ls -alR
echo "Files staged for commit:"
git diff --cached --name-status
echo "Files changed (not staged):"
git diff --name-status
echo "Now checking for changes..."
# Check if there are any changes to commit (FIXED LOGIC)
if ! git diff --cached --quiet; then
echo "Changes detected, committing..."
# Get the commit SHA that triggered the workflow
TRIGGER_COMMIT=${{ github.sha }}
COMMIT_MSG="Update docs for $VERSION (generated from $TRIGGER_COMMIT)"
git commit -m "$COMMIT_MSG"
git push -f origin "$BRANCH"
else
echo "No changes to commit."
fi
create-docs-pr:
needs: [update-docs]
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
if: needs.update-docs.outputs.pr_exists == 'false'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create or update PR to gh-pages
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ needs.update-docs.outputs.version }}
run: |
BRANCH=docs-for-$VERSION
PR_TITLE="Docs for $VERSION"
PR_BODY="Automated documentation update for version $VERSION."
# If triggered by a PR, mention it in the PR body
if [ "${{ github.event_name }}" = "pull_request" ] && [ -n "${{ github.event.pull_request.number }}" ]; then
PR_BODY="$PR_BODY\n\nTriggered by #${{ github.event.pull_request.number }}"
fi
# Check if a PR already exists
PR_NUMBER=$(gh pr list --base gh-pages --head "$BRANCH" --state open --json number --jq '.[0].number' || echo "")
if [ -z "$PR_NUMBER" ]; then
echo "Creating new PR..."
gh pr create --base gh-pages --head "$BRANCH" --title "$PR_TITLE" --body "$PR_BODY"
else
echo "PR #$PR_NUMBER already exists. Updating title and body."
gh pr edit "$PR_NUMBER" --title "$PR_TITLE" --body "$PR_BODY"
fi