4141 contains(github.event.comment.body, '/deploy-review-app'))
4242 runs-on : ubuntu-latest
4343 steps :
44- # Initial checkout only for pull_request and push events
45- - name : Checkout code
46- uses : actions/checkout@v4
47- with :
48- fetch-depth : 0
49- ref : ${{ github.sha }}
5044
5145 - name : Validate Required Secrets and Variables
5246 shell : bash
7266 exit 1
7367 fi
7468
69+ - name : Get PR HEAD Ref
70+ id : getRef
71+ env :
72+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
73+ run : |
74+ # For push events, try to find associated PR first
75+ if [[ "${{ github.event_name }}" == "push" ]]; then
76+ PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number,headRefName,headRefOid --jq '.[0]')
77+ if [[ -n "$PR_DATA" ]]; then
78+ PR_NUMBER=$(echo "$PR_DATA" | jq -r .number)
79+ else
80+ echo "No PR found for branch ${{ github.ref_name }}, skipping deployment"
81+ echo "DO_DEPLOY=false" >> $GITHUB_ENV
82+ exit 0
83+ fi
84+ else
85+ # Get PR number based on event type
86+ case "${{ github.event_name }}" in
87+ "workflow_dispatch")
88+ PR_NUMBER="${{ github.event.inputs.pr_number }}"
89+ ;;
90+ "issue_comment")
91+ PR_NUMBER="${{ github.event.issue.number }}"
92+ ;;
93+ "pull_request")
94+ PR_NUMBER="${{ github.event.pull_request.number }}"
95+ ;;
96+ *)
97+ echo "Error: Unsupported event type ${{ github.event_name }}"
98+ exit 1
99+ ;;
100+ esac
101+ fi
102+
103+ if [[ -z "$PR_NUMBER" ]]; then
104+ echo "Error: Could not determine PR number"
105+ echo "Event type: ${{ github.event_name }}"
106+ echo "Event action: ${{ github.event.action }}"
107+ echo "Ref name: ${{ github.ref_name }}"
108+ echo "Available event data:"
109+ echo "- PR number from inputs: ${{ github.event.inputs.pr_number }}"
110+ echo "- PR number from issue: ${{ github.event.issue.number }}"
111+ echo "- PR number from pull_request: ${{ github.event.pull_request.number }}"
112+ exit 1
113+ fi
114+
115+ # Get PR data
116+ if [[ -z "$PR_DATA" ]]; then
117+ PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,headRefOid)
118+ if [[ -z "$PR_DATA" ]]; then
119+ echo "Error: PR DATA for PR #$PR_NUMBER not found"
120+ echo "Event type: ${{ github.event_name }}"
121+ echo "Event action: ${{ github.event.action }}"
122+ echo "Ref name: ${{ github.ref_name }}"
123+ echo "Attempted to fetch PR data with: gh pr view $PR_NUMBER"
124+ exit 1
125+ fi
126+ fi
127+
128+ # Extract and set PR data
129+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
130+ echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-$PR_NUMBER" >> $GITHUB_ENV
131+ echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
132+ echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
133+
134+ - name : Checkout code
135+ uses : actions/checkout@v4
136+ with :
137+ fetch-depth : 0
138+ ref : ${{ env.PR_SHA }}
139+
75140 - name : Setup Environment
76141 uses : ./.github/actions/setup-environment
77142 with :
@@ -228,7 +293,7 @@ jobs:
228293 with :
229294 script : |
230295 const buildingMessage = [
231- '🏗️ Building Docker image for PR #${{ env.PR_NUMBER }}, commit ${{ github.sha }}',
296+ '🏗️ Building Docker image for PR #${{ env.PR_NUMBER }}, commit ${{ env.PR_SHA }}',
232297 '',
233298 '📝 [View Build Logs](${{ env.WORKFLOW_URL }})',
234299 '',
@@ -248,7 +313,7 @@ jobs:
248313 with :
249314 app_name : ${{ env.APP_NAME }}
250315 org : ${{ vars.CPLN_ORG_STAGING }}
251- commit : ${{ github.sha }}
316+ commit : ${{ env.PR_SHA }}
252317 PR_NUMBER : ${{ env.PR_NUMBER }}
253318
254319 - name : Update Status - Deploying
@@ -307,7 +372,7 @@ jobs:
307372
308373 // Define messages based on deployment status
309374 const successMessage = [
310- '✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ github.sha }}',
375+ '✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
311376 '',
312377 '🚀 [Review App for PR #' + prNumber + '](' + appUrl + ')',
313378 consoleLink,
@@ -316,7 +381,7 @@ jobs:
316381 ].join('\n');
317382
318383 const failureMessage = [
319- '❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ github.sha }}',
384+ '❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
320385 '',
321386 consoleLink,
322387 '',
0 commit comments