@@ -43,10 +43,19 @@ jobs:
4343 steps :
4444 # Initial checkout only for pull_request and push events
4545 - name : Checkout code
46+ if : github.event_name == 'pull_request' || github.event_name == 'push'
47+ uses : actions/checkout@v4
48+ with :
49+ fetch-depth : 0
50+ ref : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }}
51+
52+ # Basic checkout for other events (workflow_dispatch, issue_comment)
53+ # We'll do proper checkout after getting PR info
54+ - name : Initial checkout
55+ if : github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
4656 uses : actions/checkout@v4
4757 with :
4858 fetch-depth : 0
49- ref : ${{ github.sha }}
5059
5160 - name : Validate Required Secrets and Variables
5261 shell : bash
7281 exit 1
7382 fi
7483
84+ - name : Get PR HEAD Ref
85+ id : getRef
86+ env :
87+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
88+ run : |
89+ # For push events, try to find associated PR first
90+ if [[ "${{ github.event_name }}" == "push" ]]; then
91+ PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number,headRefName,headRefOid --jq '.[0]')
92+ if [[ -n "$PR_DATA" ]]; then
93+ PR_NUMBER=$(echo "$PR_DATA" | jq -r .number)
94+ else
95+ echo "No PR found for branch ${{ github.ref_name }}, skipping deployment"
96+ echo "DO_DEPLOY=false" >> $GITHUB_ENV
97+ exit 0
98+ fi
99+ else
100+ # Get PR number based on event type
101+ case "${{ github.event_name }}" in
102+ "workflow_dispatch")
103+ PR_NUMBER="${{ github.event.inputs.pr_number }}"
104+ ;;
105+ "issue_comment")
106+ PR_NUMBER="${{ github.event.issue.number }}"
107+ ;;
108+ "pull_request")
109+ PR_NUMBER="${{ github.event.pull_request.number }}"
110+ ;;
111+ *)
112+ echo "Error: Unsupported event type ${{ github.event_name }}"
113+ exit 1
114+ ;;
115+ esac
116+ fi
117+
118+ if [[ -z "$PR_NUMBER" ]]; then
119+ echo "Error: Could not determine PR number"
120+ echo "Event type: ${{ github.event_name }}"
121+ echo "Event action: ${{ github.event.action }}"
122+ echo "Ref name: ${{ github.ref_name }}"
123+ echo "Available event data:"
124+ echo "- PR number from inputs: ${{ github.event.inputs.pr_number }}"
125+ echo "- PR number from issue: ${{ github.event.issue.number }}"
126+ echo "- PR number from pull_request: ${{ github.event.pull_request.number }}"
127+ exit 1
128+ fi
129+
130+ # Get PR data
131+ if [[ -z "$PR_DATA" ]]; then
132+ PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,headRefOid)
133+ if [[ -z "$PR_DATA" ]]; then
134+ echo "Error: PR DATA for PR #$PR_NUMBER not found"
135+ echo "Event type: ${{ github.event_name }}"
136+ echo "Event action: ${{ github.event.action }}"
137+ echo "Ref name: ${{ github.ref_name }}"
138+ echo "Attempted to fetch PR data with: gh pr view $PR_NUMBER"
139+ exit 1
140+ fi
141+ fi
142+
143+ # Extract and set PR data
144+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
145+ echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-$PR_NUMBER" >> $GITHUB_ENV
146+ echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
147+ echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
148+
75149 - name : Setup Environment
76150 uses : ./.github/actions/setup-environment
77151 with :
@@ -228,7 +302,7 @@ jobs:
228302 with :
229303 script : |
230304 const buildingMessage = [
231- '🏗️ Building Docker image for PR #${{ env.PR_NUMBER }}, commit ${{ github.sha }}',
305+ '🏗️ Building Docker image for PR #${{ env.PR_NUMBER }}, commit ${{ env.PR_SHA }}',
232306 '',
233307 '📝 [View Build Logs](${{ env.WORKFLOW_URL }})',
234308 '',
@@ -248,7 +322,7 @@ jobs:
248322 with :
249323 app_name : ${{ env.APP_NAME }}
250324 org : ${{ vars.CPLN_ORG_STAGING }}
251- commit : ${{ github.sha }}
325+ commit : ${{ env.PR_SHA }}
252326 PR_NUMBER : ${{ env.PR_NUMBER }}
253327
254328 - name : Update Status - Deploying
@@ -307,7 +381,7 @@ jobs:
307381
308382 // Define messages based on deployment status
309383 const successMessage = [
310- '✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ github.sha }}',
384+ '✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
311385 '',
312386 '🚀 [Review App for PR #' + prNumber + '](' + appUrl + ')',
313387 consoleLink,
@@ -316,7 +390,7 @@ jobs:
316390 ].join('\n');
317391
318392 const failureMessage = [
319- '❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ github.sha }}',
393+ '❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
320394 '',
321395 consoleLink,
322396 '',
0 commit comments