Generate CLI Docs and PR to genlayer-docs #6
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: Generate CLI Docs and PR to genlayer-docs | |
| on: | |
| workflow_dispatch: | |
| release: | |
| types: [published] | |
| jobs: | |
| generate-and-sync: | |
| # Skip for pre-releases (tags containing '-') unless manually dispatched | |
| if: github.event_name != 'release' || !contains(github.event.release.tag_name, '-') | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # needed to commit back to THIS repo | |
| pull-requests: write | |
| steps: | |
| - name: Checkout CLI repo | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| - name: Install native deps for keytar | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y libsecret-1-0 | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Determine version for docs | |
| id: version | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [ "${{ github.event_name }}" = "release" ]; then | |
| echo "value=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT" | |
| else | |
| VERSION=$(node -p 'require("./package.json").version') | |
| echo "value=$VERSION" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Generate CLI docs (MDX) | |
| env: | |
| DOCS_CLEAN: 'true' | |
| DOCS_VERSION: ${{ steps.version.outputs.value }} | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| mkdir -p docs/api-references | |
| node scripts/generate-cli-docs.mjs | |
| (ls -la docs/api-references || true) | cat | |
| - name: Set up Git (for committing to CLI repo) | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Commit and push docs back to CLI repo (non-beta releases) | |
| if: github.event_name == 'release' && !contains(github.event.release.tag_name, '-') | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [ -n "$(git status --porcelain docs/api-references || true)" ]; then | |
| git add docs/api-references | |
| VERSION=${{ steps.version.outputs.value }} | |
| git commit -m "docs(cli): update API reference for ${VERSION}" | |
| git push | |
| else | |
| echo "No docs changes to commit" | |
| fi | |
| - name: Checkout docs repo | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: genlayerlabs/genlayer-docs | |
| # must be a PAT with write access to genlayer-docs | |
| token: ${{ secrets.DOCS_REPO_TOKEN }} | |
| path: docs-repo | |
| fetch-depth: 0 | |
| - name: Prepare branch | |
| working-directory: docs-repo | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| BRANCH="docs/cli/${{ github.repository }}-${{ github.ref_name }}-${{ github.run_id }}" | |
| git switch -c "$BRANCH" || git switch "$BRANCH" | |
| echo "BRANCH=$BRANCH" >> $GITHUB_ENV | |
| - name: Sync CLI docs into docs repo | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| SRC="${{ github.workspace }}/docs/api-references" | |
| DEST="docs-repo/pages/api-references/genlayer-cli" | |
| if [ -d "$SRC" ] && [ -n "$(ls -A "$SRC" 2>/dev/null)" ]; then | |
| mkdir -p "$DEST" | |
| rsync -a --delete "$SRC"/ "$DEST"/ | |
| echo "Synced files:" && ls -la "$DEST" | cat | |
| echo "HAS_DOCS=true" >> $GITHUB_ENV | |
| else | |
| echo "No docs generated; skipping sync." | |
| echo "HAS_DOCS=false" >> $GITHUB_ENV | |
| fi | |
| - name: Commit changes (docs repo) | |
| if: env.HAS_DOCS == 'true' | |
| working-directory: docs-repo | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [ -n "$(git status --porcelain)" ]; then | |
| git add pages/api-references/genlayer-cli | |
| git commit -m "docs(cli): sync API reference ${{ steps.version.outputs.value }}" | |
| # ensure remote uses the PAT for pushing cross-repo | |
| git remote set-url origin "https://x-access-token:${{ secrets.DOCS_REPO_TOKEN }}@github.com/genlayerlabs/genlayer-docs.git" | |
| git push --set-upstream origin "$BRANCH" | |
| echo "HAS_CHANGES=true" >> $GITHUB_ENV | |
| else | |
| echo "No changes to commit" | |
| echo "HAS_CHANGES=false" >> $GITHUB_ENV | |
| fi | |
| - name: Create PR in docs repo | |
| if: env.HAS_DOCS == 'true' && env.HAS_CHANGES == 'true' | |
| uses: peter-evans/create-pull-request@v6 | |
| with: | |
| token: ${{ secrets.DOCS_REPO_TOKEN }} # use PAT, not GITHUB_TOKEN | |
| path: docs-repo | |
| commit-message: "docs(cli): sync API reference ${{ steps.version.outputs.value }}" | |
| branch: ${{ env.BRANCH }} | |
| title: "docs(cli): sync CLI API reference ${{ steps.version.outputs.value }}" | |
| body: | | |
| This PR updates the GenlayerCLI API Reference generated automatically from `${{ github.repository }}`. | |
| - Version: `${{ steps.version.outputs.value }}` | |
| - Source commit: `${{ github.sha }}` | |
| - Trigger: `${{ github.event_name }}` | |
| labels: documentation, cli |