44 issue_comment :
55 types :
66 - created
7-
87jobs :
98 handle-slash-command :
109 runs-on : ubuntu-latest
@@ -13,19 +12,16 @@ jobs:
1312 # Step 1: Verify Command in Comment
1413 - name : Check for `/deploy-pre` Command
1514 id : check_command
16- uses : actions/github-script@v6
17- with :
18- script : |
19- const comment = context.payload.comment.body;
20- console.log("Comment received:", comment);
21- if (comment.trim() === '/deploy-pre') {
22- console.log("Command `/deploy-pre` found.");
23- return { valid: true };
24- }
25- console.log("No valid command found.");
26- return { valid: false };
27- result-encoding : json
28- result : ' steps.check_command.outputs.valid'
15+ run : |
16+ COMMENT_BODY="${{ github.event.comment.body }}"
17+ echo "Comment received: $COMMENT_BODY"
18+ if [[ "$COMMENT_BODY" == "/deploy-pre" ]]; then
19+ echo "Command `/deploy-pre` found."
20+ echo "valid=true" >> $GITHUB_OUTPUT
21+ else
22+ echo "No valid command found."
23+ echo "valid=false" >> $GITHUB_OUTPUT
24+ fi
2925
3026 # Step 2: Debug Command Check
3127 - name : Debug Command Check
@@ -36,36 +32,48 @@ jobs:
3632 - name : Extract PR Branch
3733 if : steps.check_command.outputs.valid == 'true'
3834 id : pr_details
39- uses : actions/github-script@v6
40- with :
41- script : |
42- const pr = context.payload.issue.pull_request;
43- if (!pr) {
44- throw new Error("This command can only be used on a pull request.");
45- }
46- console.log("PR branch:", pr.head.ref);
47- return pr.head.ref;
48- result-encoding : string
35+ run : |
36+ PR_NUMBER="${{ github.event.issue.number }}"
37+ REPO_OWNER="${{ github.repository_owner }}"
38+ REPO_NAME="${{ github.event.repository.name }}"
39+
40+ echo "Fetching PR details for PR #$PR_NUMBER"
41+
42+ # Use GitHub REST API to fetch the PR details
43+ PR_DETAILS=$(curl -s \
44+ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
45+ "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER")
46+
47+ # Extract the branch name (head.ref)
48+ BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')
49+
50+ # Output the branch name for debugging
51+ echo "PR branch: $BRANCH"
52+
53+ # Set the branch name as an output
54+ echo "branch=$BRANCH" >> $GITHUB_OUTPUT
4955
5056 # Step 4: Debug PR Branch
5157 - name : Debug PR Branch
5258 if : steps.check_command.outputs.valid == 'true'
5359 run : |
54- echo "Branch: ${{ steps.pr_details.outputs.result }}"
60+ echo "Branch: ${{ steps.pr_details.outputs.branch }}"
5561
5662 # Step 5: Trigger Deploy Workflow
5763 - name : Trigger Deploy Workflow
5864 if : steps.check_command.outputs.valid == 'true'
5965 uses : actions/github-script@v6
6066 with :
6167 script : |
68+ const branch = '${{ steps.pr_details.outputs.branch }}'; # Access the branch directly as a string
6269 github.rest.actions.createWorkflowDispatch({
6370 owner: context.repo.owner,
6471 repo: context.repo.repo,
65- workflow_id: "deploy-pre.yml", # Replace with your deployment workflow filename
66- ref: "${{ steps.pr_details.outputs.result }}"
67- })
68- console.log("Triggered deploy-pre workflow for branch:", "${{ steps.pr_details.outputs.result }}");
72+ workflow_id: "deploy-pre.yml", # Replace with your workflow file
73+ ref: branch,
74+ });
75+ console.log("Triggered deploy-pre workflow for branch:", branch);
76+
6977
7078 # Step 6: Post Confirmation Comment
7179 - name : Post Comment to PR
7886 owner: context.repo.owner,
7987 repo: context.repo.repo,
8088 issue_number: issue_number,
81- body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${{ steps.pr_details.outputs.result }}\`.`
89+ body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${{ steps.pr_details.outputs.branch }}\`.`
8290 });
8391 console.log("Commented back to PR:", issue_number);
0 commit comments