Fixing docs workflow #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }} | |
| branch_exists: ${{ steps.update_docs_branch.outputs.branch_exists }} | |
| changes: ${{ steps.update_docs_branch.outputs.changes }} | |
| 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: Clean up everything except doc/ | |
| run: | | |
| find . -mindepth 1 -maxdepth 1 ! -name 'doc' ! -name '.git' ! -name '.' ! -name '..' -exec rm -rf {} + | |
| - name: Move doc/api/* to root | |
| run: | | |
| shopt -s dotglob | |
| mv doc/api/* . | |
| shopt -u dotglob | |
| - name: Remove doc/api if empty | |
| run: | | |
| rmdir doc/api || true | |
| - name: Create or update docs branch | |
| id: update_docs_branch | |
| env: | |
| VERSION: ${{ steps.get_version.outputs.version }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| BRANCH=docs-for-$VERSION | |
| if git ls-remote --exit-code --heads origin "$BRANCH"; then | |
| echo "branch_exists=true" >> $GITHUB_OUTPUT | |
| git fetch origin "$BRANCH" | |
| git clean -fd | |
| git checkout "$BRANCH" | |
| git reset --hard origin/$BRANCH | |
| else | |
| echo "branch_exists=false" >> $GITHUB_OUTPUT | |
| git checkout --orphan "$BRANCH" | |
| git reset --hard | |
| fi | |
| 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..." | |
| # Compare current files against origin/gh-pages by path | |
| if git diff --cached --quiet origin/gh-pages -- . 2>/dev/null; then | |
| git commit -m "Update docs for $VERSION" | |
| git push -f origin "$BRANCH" | |
| echo "changes=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No changes to commit." | |
| echo "changes=false" >> $GITHUB_OUTPUT | |
| fi | |
| create-docs-pr: | |
| needs: [update-docs] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| runs-on: ubuntu-latest | |
| if: needs.update-docs.outputs.changes == 'true' && needs.update-docs.outputs.branch_exists == 'false' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Create or update PR to gh-pages | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| base: gh-pages | |
| title: Docs for ${{ needs.update-docs.outputs.version }} | |
| body: "Automated documentation update for version ${{ needs.update-docs.outputs.version }}." | |
| branch: docs-for-${{ needs.update-docs.outputs.version }} |