Handle PRE env deployment commands in PR Comments #14
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 Slash 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 | |
| run: | | |
| COMMENT_BODY="${{ github.event.comment.body }}" | |
| echo "Comment received: $COMMENT_BODY" | |
| if [[ "$COMMENT_BODY" == "/deploy-pre" ]]; then | |
| echo "Command `/deploy-pre` found." | |
| echo "valid=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No valid command found." | |
| echo "valid=false" >> $GITHUB_OUTPUT | |
| fi | |
| # Step 2: Debug Command Check | |
| - name: Debug Command Check | |
| run: | | |
| echo "Valid command: ${{ steps.check_command.outputs.valid }}" | |
| # Step 3: Extract PR Branch | |
| - name: Extract PR Branch | |
| if: steps.check_command.outputs.valid == 'true' | |
| id: pr_details | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const issue = context.payload.issue; | |
| // Check if the issue is related to a pull request | |
| if (!issue.pull_request || !issue.pull_request.url) { | |
| throw new Error("This comment is not associated with a pull request."); | |
| } | |
| const pr_url = issue.pull_request.url; | |
| // Fetch pull request details using the GitHub API | |
| const pr = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.issue.number | |
| }); | |
| // Log the PR details for debugging | |
| console.log("PR details:", pr.data); | |
| // Ensure we have the correct branch info | |
| if (!pr.data.head || !pr.data.head.ref) { | |
| throw new Error("Cannot find branch reference in the pull request."); | |
| } | |
| const branch = pr.data.head.ref; // This is the branch name of the PR | |
| console.log("PR branch:", branch); | |
| return { branch: branch }; | |
| result-encoding: json | |
| # Step 4: Debug PR Branch | |
| - name: Debug PR Branch | |
| if: steps.check_command.outputs.valid == 'true' | |
| run: | | |
| echo "Branch: ${{ steps.pr_details.outputs.branch }}" | |
| # Step 5: Trigger Deploy Workflow | |
| - name: Trigger Deploy Workflow | |
| if: steps.check_command.outputs.valid == 'true' | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const branch = '${{ steps.pr_details.outputs.branch }}'; # Access the branch directly as a string | |
| github.rest.actions.createWorkflowDispatch({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| workflow_id: "deploy-pre.yml", # Replace with your workflow file | |
| ref: branch, | |
| }); | |
| console.log("Triggered deploy-pre workflow for branch:", branch); | |
| # Step 6: 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.branch }}\`.` | |
| }); | |
| console.log("Commented back to PR:", issue_number); |