1- name : Handle Deploy Commands in PR Comments
1+ name : Handle PRE env deployment commands in PR Comments
22
33on :
44 issue_comment :
@@ -10,37 +10,68 @@ jobs:
1010 runs-on : ubuntu-latest
1111
1212 steps :
13- # Step 1: Check if the command is `/deploy-pre`
13+ # Step 1: Verify Command in Comment
1414 - name : Check for `/deploy-pre` Command
1515 id : check_command
16- uses : peter-evans/slash-command-action@v4
17- with :
18- token : ${{ secrets.GITHUB_TOKEN }}
19- command : /deploy-pre
16+ run : |
17+ COMMENT_BODY="${{ github.event.comment.body }}"
18+ echo "Comment received: $COMMENT_BODY"
19+ if [[ "$COMMENT_BODY" == "/deploy-pre" ]]; then
20+ echo "Command `/deploy-pre` found."
21+ echo "valid=true" >> $GITHUB_OUTPUT
22+ else
23+ echo "No valid command found."
24+ echo "valid=false" >> $GITHUB_OUTPUT
25+ fi
2026
21- # Step 2: Extract Branch Reference from PR
22- - name : Get Pull Request Details
23- if : steps.check_command.outputs.command == 'true'
27+ # Step 2: Extract PR Branch
28+ - name : Extract PR Branch
29+ if : steps.check_command.outputs.valid == 'true'
2430 id : pr_details
31+ run : |
32+ PR_NUMBER="${{ github.event.issue.number }}"
33+ REPO_OWNER="${{ github.repository_owner }}"
34+ REPO_NAME="${{ github.event.repository.name }}"
35+
36+ echo "Fetching PR details for PR #$PR_NUMBER"
37+
38+ # Use GitHub REST API to fetch the PR details
39+ PR_DETAILS=$(curl -s \
40+ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
41+ "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER")
42+ # Extract the branch name (head.ref)
43+ BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')
44+
45+ # Output the branch name
46+ echo "branch=$BRANCH" >> $GITHUB_OUTPUT
47+
48+ # Step 3: Trigger Deploy Workflow
49+ - name : Trigger Deploy Workflow
50+ if : steps.check_command.outputs.valid == 'true'
2551 uses : actions/github-script@v6
2652 with :
2753 script : |
28- const pr = context.payload.issue.pull_request;
29- if (!pr) {
30- throw new Error("The /deploy-pre command must be used in a pull request comment.");
31- }
32- return pr.head.ref;
33- result-encoding : string
54+ const branch = `${{ steps.pr_details.outputs.branch }}`;
55+ console.log("Triggering deploy-pre.yml for branch:", branch);
56+ github.rest.actions.createWorkflowDispatch({
57+ owner: context.repo.owner,
58+ repo: context.repo.repo,
59+ workflow_id: "deploy-pre.yml",
60+ ref: branch,
61+ });
3462
35- # Step 3: Trigger Deploy Workflow for the PR Branch
36- - name : Trigger Deploy Workflow
37- if : steps.check_command.outputs.command == 'true'
63+ # Step 4: Post Confirmation Comment
64+ - name : Post Comment to PR
65+ if : steps.check_command.outputs.valid == 'true'
3866 uses : actions/github-script@v6
3967 with :
4068 script : |
41- github.rest.actions.createWorkflowDispatch({
69+ const branch = `${{ steps.pr_details.outputs.branch }}`;
70+ const prNumber = context.payload.issue.number;
71+ console.log(`Commenting back on PR #${prNumber}`);
72+ github.rest.issues.createComment({
4273 owner: context.repo.owner,
4374 repo: context.repo.repo,
44- workflow_id: "deploy-pre.yml", # Replace with your workflow filename
45- ref: "${{ steps.pr_details.outputs.result }}"
46- })
75+ issue_number: prNumber,
76+ body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${branch}\`.`,
77+ });
0 commit comments