Handle PRE env deployment commands in PR Comments #4
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: Handle Deployment Commands in PR Comments | |
| on: | |
| issue_comment: | |
| types: | |
| - created | |
| jobs: | |
| handle-slash-command: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Verify Command in Comment | |
| - name: Check for `/deploy-pre` Command | |
| id: check_command | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const comment = context.payload.comment.body; | |
| if (comment.trim() === '/deploy-pre') { | |
| return { valid: true }; | |
| } | |
| return { valid: false }; | |
| result-encoding: json | |
| # Step 2: Get Branch from PR | |
| - name: Extract PR Branch | |
| if: steps.check_command.outputs.valid == 'true' | |
| id: pr_details | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const pr = context.payload.issue.pull_request; | |
| if (!pr) { | |
| throw new Error("This command can only be used on a pull request."); | |
| } | |
| return pr.head.ref; | |
| result-encoding: string | |
| # Step 3: Trigger Deploy Workflow | |
| - name: Trigger Deploy Workflow | |
| if: steps.check_command.outputs.valid == 'true' | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| github.rest.actions.createWorkflowDispatch({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| workflow_id: "deploy-pre.yml", | |
| ref: "${{ steps.pr_details.outputs.result }}" | |
| }) | |
| # Step 4: Post Confirmation Comment | |
| - name: Post Comment to PR | |
| if: steps.check_command.outputs.valid == 'true' | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const issue_number = context.payload.issue.number; | |
| github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issue_number, | |
| body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${{ steps.pr_details.outputs.result }}\`.` | |
| }); |