From 7bf3ed7d1301d00312660af2f5ca7e1fff56050a Mon Sep 17 00:00:00 2001 From: Leo Fang Date: Wed, 15 Jan 2025 23:15:37 -0500 Subject: [PATCH] add custom preview deploy/undeploy steps --- .github/workflows/build-and-test.yml | 1 + .github/workflows/build-docs.yml | 55 ++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index fcff657a..d5ed7df4 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -263,6 +263,7 @@ jobs: permissions: id-token: write contents: write + pull-requests: read needs: - build secrets: inherit diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml index e0d7b861..d5f52307 100644 --- a/.github/workflows/build-docs.yml +++ b/.github/workflows/build-docs.yml @@ -126,6 +126,9 @@ jobs: mkdir -p artifacts/docs mv cuda_python/docs/build/html/* artifacts/docs/ + # create an empty folder for removal use + mkdir -p artifacts/empty_docs + # TODO: remove this step - name: Upload doc artifacts uses: actions/upload-pages-artifact@v3 @@ -133,33 +136,53 @@ jobs: path: artifacts/ retention-days: 3 + - name: Collect PR data + uses: actions/github-script@v7 + id: get_pr_data + with: + script: | + return ( + await github.rest.repos.listPullRequestsAssociatedWithCommit({ + commit_sha: context.sha, + owner: context.repo.owner, + repo: context.repo.repo, + }) + ).data[0]; + + - name: Get PR number + run: | + PR_NUMBER="${{ fromJson(steps.get_pr_data.outputs.result).number }}" + echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV + # Note: the PR previews will be removed once merged to main (see below) - - uses: rossjrw/pr-preview-action@v1 - if: ${{ github.ref_name != 'main' && success() }} + - name: Deploy doc preview + if: ${{ github.ref_name != 'main' }} + uses: JamesIves/github-pages-deploy-action@v4 with: - source-dir: artifacts/docs/ - preview-branch: gh-pages - umbrella-dir: docs/pr-preview - pages-base-path: docs - action: deploy + git-config-name: cuda-python-bot + git-config-email: cuda-python-bot@users.noreply.github.com + folder: artifacts/docs/ + target-folder: docs/pr-preview/pr-${{ env.PR_NUMBER }}/ + commit-message: "Deploy doc preview for PR ${{ env.PR_NUMBER }} (${{ github.sha }})" # The steps below are not executed unless when building on main. - name: Deploy doc update - if: ${{ github.ref_name == 'main' && success() }} + if: ${{ github.ref_name == 'main' }} uses: JamesIves/github-pages-deploy-action@v4 with: - folder: artifacts/docs/ git-config-name: cuda-python-bot git-config-email: cuda-python-bot@users.noreply.github.com + folder: artifacts/docs/ target-folder: docs/ commit-message: "Deploy latest docs: ${{ github.sha }}" clean: false - - uses: rossjrw/pr-preview-action@v1 - if: ${{ github.ref_name == 'main' && success() }} + - name: Remove doc preview + if: ${{ github.ref_name == 'main' }} + uses: JamesIves/github-pages-deploy-action@v4 with: - source-dir: artifacts/docs/ - preview-branch: gh-pages - umbrella-dir: docs/pr-preview - pages-base-path: docs - action: remove + git-config-name: cuda-python-bot + git-config-email: cuda-python-bot@users.noreply.github.com + folder: artifacts/empty_docs/ + target-folder: docs/pr-preview/pr-${{ env.PR_NUMBER }}/ + commit-message: "Deploy doc preview for PR ${{ env.PR_NUMBER }} (${{ github.sha }})"