diff --git a/.github/workflows/surge-pr-fork-01-build.yml b/.github/workflows/surge-pr-fork-01-build.yml index 9e6432a..28bfad1 100644 --- a/.github/workflows/surge-pr-fork-01-build.yml +++ b/.github/workflows/surge-pr-fork-01-build.yml @@ -2,6 +2,7 @@ name: Surge PR Preview - Build Stage on: pull_request: + # keep in sync with the teardown stage paths: - '.github/workflows/surge-pr-fork-01-build.yml' - '.github/workflows/surge-pr-fork-02-deploy.yml' diff --git a/.github/workflows/surge-pr-fork-02-deploy.yml b/.github/workflows/surge-pr-fork-02-deploy.yml index 060d436..c2dc184 100644 --- a/.github/workflows/surge-pr-fork-02-deploy.yml +++ b/.github/workflows/surge-pr-fork-02-deploy.yml @@ -10,6 +10,8 @@ permissions: pull-requests: write # surge-preview creates or updates PR comments about the deployment status jobs: + # MUST be unique across all surge preview deployments for a repository as the job id is used in the deployment URL + # MUST be kept in sync with the job id of the teardown stage (used in the deployment URL) deploy: runs-on: ubuntu-22.04 if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }} @@ -55,3 +57,4 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} build: echo done dist: site + failOnError: true diff --git a/.github/workflows/surge-pr-fork-03-teardown.yml b/.github/workflows/surge-pr-fork-03-teardown.yml new file mode 100644 index 0000000..ab57c6c --- /dev/null +++ b/.github/workflows/surge-pr-fork-03-teardown.yml @@ -0,0 +1,45 @@ +name: Surge Preview for Pull Request + +on: + pull_request_target: + # To manage 'surge-preview' action teardown on PR closed + types: [closed] + branches: + - master + # keep in sync with the build stage + paths: + - '.github/workflows/surge-pr-fork-01-build.yml' + - '.github/workflows/surge-pr-fork-02-deploy.yml' + - 'public/surge/**/*' + + +permissions: + pull-requests: write # surge-preview creates or updates PR comments about the deployment status + +jobs: + deploy: # MUST be kept in sync with the job id of the deploy stage (used in the deployment URL) + runs-on: ${{ vars.RUNNER_UBUNTU }} + steps: + - uses: bonitasoft/actions/packages/surge-preview-tools@v2 + id: surge-preview-tools + with: + surge-token: ${{ secrets.SURGE_TOKEN }} + + - name: Echo surge preview tools output + run: | + echo "can-run-surge-command: ${{ steps.surge-preview-tools.outputs.can-run-surge-command }}" + echo "domain-exist: ${{ steps.surge-preview-tools.outputs.domain-exist }}" + echo "preview-url: ${{ steps.surge-preview-tools.outputs.preview-url }}" + echo "surge-token-valid: ${{ steps.surge-preview-tools.outputs.surge-token-valid }}" + + - name: Teardown demo preview + if: steps.surge-preview-tools.outputs.can-run-surge-command == 'true' + id: publish_demo_preview + uses: afc163/surge-preview@v1 + with: + surge_token: ${{ secrets.SURGE_TOKEN }} + github_token: ${{ secrets.GITHUB_TOKEN }} + failOnError: true + teardown: true + build: | + echo diff --git a/README.md b/README.md index 7ad1082..6bec342 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,10 @@ GitHub pages environment generated by this repository - [Fill github draft releases](.github/workflows/fill-gh-draft-release.yml): experiment the release-drafter configuration (used in bpmn-visualization) - [Slack send message](.github/workflows/slack-send-message.yml): send a message to a Slack channel or user - [Surge preview for Pull Request](.github/workflows/surge-preview-for-pr.yml): create/teardown a surge deployment when a new Pull Request is created/closed -- Surge preview for PR in 2 stages. This is the only known way to have a preview for PR created from forks. +- Surge preview for PR in several stages. This is the only known way to have a preview for PR created from forks. - [Surge preview for PR - stage 1](.github/workflows/surge-pr-fork-01-build.yml): the first stage is to create an artifact of the site. - [Surge preview for PR - stage 2](.github/workflows/surge-pr-fork-02-deploy.yml): the second stage is to deploy the artifact created in the first stage. + - [Surge preview for PR - stage 3](.github/workflows/surge-pr-fork-03-teardown.yml): the third stage is to teardown the deployment when the PR is closed. - [Workflow concurrency](.github/workflows/workflow-concurrency.yml): avoid to have more than 1 workflow run at a time - [Workflow dispatch](.github/workflows/workflow-dispatch.yml): run job depending on if the workflow is triggered from the `master` branch or another branch