Deploy documentation preview #7085
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: Deploy documentation preview | |
| on: | |
| workflow_run: | |
| workflows: [Tests And Linting] | |
| types: [completed] | |
| jobs: | |
| deploy: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| contents: write | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v6 | |
| with: | |
| persist-credentials: false | |
| - name: Download artifact | |
| uses: dawidd6/action-download-artifact@v19 | |
| with: | |
| workflow_conclusion: success | |
| run_id: ${{ github.event.workflow_run.id }} | |
| path: docs-preview | |
| name: docs-preview | |
| - name: Validate and set PR number | |
| run: | | |
| PR_NUMBER=$(cat docs-preview/.pr_number) | |
| if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then | |
| echo "Invalid PR number: $PR_NUMBER" | |
| exit 1 | |
| fi | |
| echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
| - name: Generate GitHub App Token | |
| id: generate-token | |
| uses: actions/create-github-app-token@v3 | |
| with: | |
| app-id: ${{ secrets.APP_ID }} | |
| private-key: ${{ secrets.APP_PRIVATE_KEY }} | |
| owner: litestar-org | |
| repositories: litestar-docs-preview | |
| - name: Deploy docs preview | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| folder: docs-preview/docs/_build/html | |
| token: ${{ steps.generate-token.outputs.token }} | |
| repository-name: litestar-org/litestar-docs-preview | |
| clean: false | |
| target-folder: ${{ env.PR_NUMBER }} | |
| branch: gh-pages | |
| - uses: actions/github-script@v8 | |
| env: | |
| PR_NUMBER: ${{ env.PR_NUMBER }} | |
| with: | |
| script: | | |
| const issue_number = parseInt(process.env.PR_NUMBER, 10) | |
| const previewUrl = "https://litestar-org.github.io/litestar-docs-preview/" + issue_number | |
| const marker = "<!-- docs-preview -->" | |
| const section = marker + "\n\n<hr>\n📚 Documentation preview 📚: " + previewUrl + "\n" | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: issue_number, | |
| }) | |
| let body = pr.body || "" | |
| if (body.includes(marker)) { | |
| // Update existing section | |
| body = body.replace( | |
| new RegExp(marker + "[\\s\\S]*$"), | |
| section, | |
| ) | |
| } else { | |
| // Append section | |
| body = body + "\n\n" + section | |
| } | |
| await github.rest.pulls.update({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: issue_number, | |
| body: body, | |
| }) |