From 6e31dc00059cbe32292f7dbd937559debbddc3f5 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 12 Apr 2022 16:10:01 +0200 Subject: [PATCH] ci: setup command dispatcher (#1759) --- .github/workflows/command.yml | 21 +++++++ ...review.yml => website-preview-command.yml} | 58 +++++++------------ 2 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/command.yml rename .github/workflows/{website-preview.yml => website-preview-command.yml} (61%) diff --git a/.github/workflows/command.yml b/.github/workflows/command.yml new file mode 100644 index 000000000..30ce3e339 --- /dev/null +++ b/.github/workflows/command.yml @@ -0,0 +1,21 @@ +name: Command + +on: + issue_comment: + types: [created] + +jobs: + dispatch: + name: Dispatch + runs-on: ubuntu-latest + + steps: + - name: Dispatch website preview + uses: peter-evans/slash-command-dispatch@v3 + with: + token: ${{ secrets.COMMAND_DISPATCH_TOKEN }} + issue-type: pull-request + dispatch-type: workflow + commands: website-preview + static-args: | + pull-request-number=${{ github.event.issue.number }} diff --git a/.github/workflows/website-preview.yml b/.github/workflows/website-preview-command.yml similarity index 61% rename from .github/workflows/website-preview.yml rename to .github/workflows/website-preview-command.yml index 9b5446bb6..e70c8e2a3 100644 --- a/.github/workflows/website-preview.yml +++ b/.github/workflows/website-preview-command.yml @@ -1,8 +1,13 @@ name: Website Preview on: - issue_comment: - types: [created] + workflow_dispatch: + inputs: + pull-request-number: + description: "Pull request number from where the command was triggered" + required: true + type: string + pull_request_target: types: [closed] branches: [main] @@ -18,28 +23,15 @@ env: jobs: deploy: - if: > - github.event.issue.pull_request - && ( - github.event.comment.author_association == 'OWNER' - || github.event.comment.author_association == 'MEMBER' - ) - && startsWith(github.event.comment.body, '/deploy-website') + if: github.event_name == 'workflow_dispatch' name: Deploy runs-on: ubuntu-latest steps: - - name: Notify deployment triggered - uses: peter-evans/create-or-update-comment@v2 - with: - issue-number: ${{ github.event.issue.number }} - comment-id: ${{ github.event.comment.id }} - reactions: "+1" - - uses: actions/checkout@v3 - name: Checkout pull request - run: hub pr checkout ${{ github.event.issue.number }} + run: hub pr checkout ${{ github.event.inputs.pull-request-number }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -53,7 +45,7 @@ jobs: - uses: actions/cache@v3 with: path: website/.docusaurus - key: docusaurus-main-pr-${{ github.event.issue.number }} + key: docusaurus-main-pr-${{ github.event.inputs.pull-request-number }} restore-keys: | docusaurus-main- @@ -66,7 +58,7 @@ jobs: run: yarn build env: URL: ${{ env.PREVIEW_URL }} - BASE_URL: ${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.issue.number }}/ + BASE_URL: ${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.inputs.pull-request-number }}/ - name: Deploy uses: peaceiris/actions-gh-pages@v3 @@ -74,33 +66,33 @@ jobs: external_repository: ${{ env.PREVIEW_EXTERNAL_REPOSITORY }} deploy_key: ${{ secrets.PREVIEW_DEPLOY_KEY }} publish_dir: website/build - destination_dir: pr-${{ github.event.issue.number }} - full_commit_message: "deploy: pr-${{ github.event.issue.number }}" + destination_dir: pr-${{ github.event.inputs.pull-request-number }} + full_commit_message: "deploy pr-${{ github.event.inputs.pull-request-number }}" keep_files: true - name: Notify deployment succeeded if: ${{ success() }} uses: peter-evans/create-or-update-comment@v2 with: - issue-number: ${{ github.event.issue.number }} + issue-number: ${{ github.event.inputs.pull-request-number }} body: | **:rocket: Preview deployment succeeded!** - Preview: ${{ env.PREVIEW_URL }}${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.issue.number }}/ + Preview: ${{ env.PREVIEW_URL }}${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.inputs.pull-request-number }}/ Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - name: Notify deployment failed if: ${{ failure() }} uses: peter-evans/create-or-update-comment@v2 with: - issue-number: ${{ github.event.issue.number }} + issue-number: ${{ github.event.inputs.pull-request-number }} body: | **:boom: Preview deployment failed!** Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} clean: - if: github.event.action == 'closed' + if: github.event_name == 'pull_request_target' && github.event.action == 'closed' name: Clean runs-on: ubuntu-latest @@ -110,15 +102,9 @@ jobs: repository: ${{ env.PREVIEW_EXTERNAL_REPOSITORY }} ssh-key: ${{ secrets.PREVIEW_DEPLOY_KEY }} - - name: Clean preview - run: | - git config --local user.name 'github-actions[bot]' - git config --local user.email 'github-actions[bot]@users.noreply.github.com' + - name: Remove files + run: rm -fR pr-${{ github.event.pull_request.number }} - rm -fR pr-${{ github.event.pull_request.number }} - - if ! git diff-index --quiet HEAD --; then - git add . - git commit -m "clean: pr-${{ github.event.pull_request.number }}" - git push - fi + - uses: endbug/add-and-commit@v9 + with: + message: "clean pr-${{ github.event.pull_request.number }}"