Skip to content

First deployment on PRE via GitHub actions triggered by a chat command on PR #29

First deployment on PRE via GitHub actions triggered by a chat command on PR

First deployment on PRE via GitHub actions triggered by a chat command on PR #29

name: Handle PRE env 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
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: Extract PR Branch
- name: Extract PR Branch
if: steps.check_command.outputs.valid == 'true'
id: pr_details
run: |
PR_NUMBER="${{ github.event.issue.number }}"
REPO_OWNER="${{ github.repository_owner }}"
REPO_NAME="${{ github.event.repository.name }}"
echo "Fetching PR details for PR #$PR_NUMBER"
# Use GitHub REST API to fetch the PR details
PR_DETAILS=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER")
# Extract the branch name (head.ref)
BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')
# Output the branch name
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
# Step 3: 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 }}`;
console.log("Triggering deploy-pre.yml for branch:", branch);
github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "deploy-pre.yml",
ref: branch,
});
# 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 branch = `${{ steps.pr_details.outputs.branch }}`;
const prNumber = context.payload.issue.number;
console.log(`Commenting back on PR #${prNumber}`);
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${branch}\`.`,
});