1- name : Deploy Review App to Control Plane
1+ name : Deploy PR Review App to Control Plane
22
3- run-name : Deploy Review App - ${{ github.ref_name }}
3+ run-name : Deploy PR Review App - PR # ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
44
55on :
66 pull_request :
3030 PR_NUMBER : ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
3131
3232jobs :
33+ debug :
34+ uses : ./.github/workflows/debug-workflow.yml
35+ with :
36+ debug_enabled : false # Will still run if vars.DEBUG_WORKFLOW is true
37+
3338 Process-Deployment-Command :
39+ needs : debug # Add this to ensure debug runs first
3440 if : |
3541 (github.event_name == 'pull_request') ||
3642 (github.event_name == 'push') ||
3743 (github.event_name == 'workflow_dispatch') ||
3844 (github.event_name == 'issue_comment' &&
3945 github.event.issue.pull_request &&
40- github.event.comment.body == '/deploy-review-app')
46+ contains( github.event.comment.body, '/deploy-review-app') )
4147 runs-on : ubuntu-latest
4248 permissions :
4349 contents : read
@@ -91,51 +97,65 @@ jobs:
9197 env :
9298 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
9399 run : |
94- # Get PR number based on event type
95- case "${{ github.event_name }}" in
96- "workflow_dispatch")
97- PR_NUMBER="${{ github.event.inputs.pr_number }}"
98- ;;
99- "issue_comment")
100- PR_NUMBER="${{ github.event.issue.number }}"
101- ;;
102- "pull_request")
103- PR_NUMBER="${{ github.event.pull_request.number }}"
104- ;;
105- "push")
106- # For push events, find associated PR
107- PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number --jq '.[0].number')
108- if [[ -n "$PR_DATA" ]]; then
109- PR_NUMBER="$PR_DATA"
110- else
111- echo "Error: No PR found for branch ${{ github.ref_name }}"
100+ # For push events, try to find associated PR first
101+ if [[ "${{ github.event_name }}" == "push" ]]; then
102+ PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number,headRefName,headRefOid --jq '.[0]')
103+ if [[ -n "$PR_DATA" ]]; then
104+ PR_NUMBER=$(echo "$PR_DATA" | jq -r .number)
105+ else
106+ echo "No PR found for branch ${{ github.ref_name }}, skipping deployment"
107+ echo "DO_DEPLOY=false" >> $GITHUB_ENV
108+ exit 0
109+ fi
110+ else
111+ # Get PR number based on event type
112+ case "${{ github.event_name }}" in
113+ "workflow_dispatch")
114+ PR_NUMBER="${{ github.event.inputs.pr_number }}"
115+ ;;
116+ "issue_comment")
117+ PR_NUMBER="${{ github.event.issue.number }}"
118+ ;;
119+ "pull_request")
120+ PR_NUMBER="${{ github.event.pull_request.number }}"
121+ ;;
122+ *)
123+ echo "Error: Unsupported event type ${{ github.event_name }}"
112124 exit 1
113- fi
114- ;;
115- *)
116- echo "Error: Unsupported event type ${{ github.event_name }}"
117- exit 1
118- ;;
119- esac
120-
125+ ;;
126+ esac
127+ fi
128+
121129 if [[ -z "$PR_NUMBER" ]]; then
122130 echo "Error: Could not determine PR number"
131+ echo "Event type: ${{ github.event_name }}"
132+ echo "Event action: ${{ github.event.action }}"
133+ echo "Ref name: ${{ github.ref_name }}"
134+ echo "Available event data:"
135+ echo "- PR number from inputs: ${{ github.event.inputs.pr_number }}"
136+ echo "- PR number from issue: ${{ github.event.issue.number }}"
137+ echo "- PR number from pull_request: ${{ github.event.pull_request.number }}"
123138 exit 1
124139 fi
125140
126- # Set environment variables
141+ # Get PR data
142+ if [[ -z "$PR_DATA" ]]; then
143+ PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,headRefOid)
144+ if [[ -z "$PR_DATA" ]]; then
145+ echo "Error: PR DATA for PR #$PR_NUMBER not found"
146+ echo "Event type: ${{ github.event_name }}"
147+ echo "Event action: ${{ github.event.action }}"
148+ echo "Ref name: ${{ github.ref_name }}"
149+ echo "Attempted to fetch PR data with: gh pr view $PR_NUMBER"
150+ exit 1
151+ fi
152+ fi
153+
154+ # Extract and set PR data
127155 echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
128156 echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-$PR_NUMBER" >> $GITHUB_ENV
129-
130- # Get PR data using GitHub CLI
131- PR_DATA=$(gh pr view $PR_NUMBER --repo shakacode/react-webpack-rails-tutorial --json headRefName,headRefOid)
132- if [[ $? -eq 0 ]]; then
133- echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
134- echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
135- else
136- echo "Error: Could not fetch PR data for PR #$PR_NUMBER"
137- exit 1
138- fi
157+ echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
158+ echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
139159
140160 - name : Checkout PR code
141161 if : github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
@@ -152,7 +172,6 @@ jobs:
152172
153173 - name : Check if Review App Exists
154174 id : check-app
155- if : github.event_name == 'pull_request'
156175 env :
157176 CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
158177 run : |
@@ -162,25 +181,64 @@ jobs:
162181 exit 1
163182 fi
164183
165- # Then check if app exists
184+ # Check if app exists and save state
166185 if ! cpflow exists -a ${{ env.APP_NAME }}; then
167- echo "No review app exists for this PR"
168- echo "DO_DEPLOY=false" >> $GITHUB_ENV
186+ echo "APP_EXISTS=false" >> $GITHUB_ENV
169187 else
170- echo "DO_DEPLOY =true" >> $GITHUB_ENV
188+ echo "APP_EXISTS =true" >> $GITHUB_ENV
171189 fi
172190
173191 - name : Validate Deployment Request
174192 id : validate
175- if : env.DO_DEPLOY != 'false'
176193 run : |
194+ # Skip validation if deployment is already disabled
195+ if [[ "${{ env.DO_DEPLOY }}" == "false" ]]; then
196+ echo "Skipping validation - deployment already disabled"
197+ exit 0
198+ fi
199+
177200 if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \
178- ("${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app") || \
179- "${{ github.event_name }}" == "pull_request" ]]; then
180- echo "Skipping deployment - not a valid trigger (event: ${{ github.event_name }})"
201+ "${{ github.event_name }}" == "issue_comment" || \
202+ "${{ github.event_name }}" == "pull_request" || \
203+ "${{ github.event_name }}" == "push" ]]; then
204+ echo "Error: Unsupported event type ${{ github.event_name }}"
181205 exit 1
182206 fi
183207
208+ # Set DO_DEPLOY based on event type and conditions
209+ if [[ "${{ github.event_name }}" == "pull_request" && \
210+ ("${{ github.event.action }}" == "opened" || \
211+ "${{ github.event.action }}" == "synchronize" || \
212+ "${{ github.event.action }}" == "reopened") ]]; then
213+ echo "DO_DEPLOY=true" >> $GITHUB_ENV
214+ elif [[ "${{ github.event_name }}" == "push" ]]; then
215+ echo "DO_DEPLOY=true" >> $GITHUB_ENV
216+ elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
217+ echo "DO_DEPLOY=true" >> $GITHUB_ENV
218+ elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
219+ if [[ "${{ github.event.issue.pull_request }}" ]]; then
220+ # Trim spaces and check for exact command
221+ COMMENT_BODY=$(echo "${{ github.event.comment.body }}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
222+ if [[ "$COMMENT_BODY" == "/deploy-review-app" ]]; then
223+ echo "DO_DEPLOY=true" >> $GITHUB_ENV
224+ else
225+ echo "DO_DEPLOY=false" >> $GITHUB_ENV
226+ echo "Skipping deployment - comment '$COMMENT_BODY' does not match '/deploy-review-app'"
227+ fi
228+ else
229+ echo "DO_DEPLOY=false" >> $GITHUB_ENV
230+ echo "Skipping deployment for non-PR comment"
231+ fi
232+ fi
233+
234+ - name : Setup Control Plane App if Not Existing
235+ if : env.DO_DEPLOY == 'true' && env.APP_EXISTS == 'false'
236+ env :
237+ CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
238+ run : |
239+ echo "🔧 Setting up new Control Plane app..."
240+ cpflow setup-app -a ${{ env.APP_NAME }} --org ${{ vars.CPLN_ORG_STAGING }}
241+
184242 - name : Create Initial Comment
185243 if : env.DO_DEPLOY != 'false'
186244 uses : actions/github-script@v7
@@ -228,7 +286,16 @@ jobs:
228286 repo: context.repo.repo,
229287 run_id: runId
230288 });
231- return run.html_url;
289+
290+ // Get the job ID for this specific job
291+ const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({
292+ owner: context.repo.owner,
293+ repo: context.repo.repo,
294+ run_id: runId
295+ });
296+
297+ const currentJob = jobs.jobs.find(job => job.name === context.job);
298+ return `${run.html_url}/job/${currentJob.id}`;
232299 };
233300
234301 const workflowUrl = await getWorkflowUrl(context.runId);
@@ -244,7 +311,7 @@ jobs:
244311 with :
245312 script : |
246313 const buildingMessage = [
247- '🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + process. env.PR_SHA,
314+ '🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.PR_SHA }}' ,
248315 '',
249316 '📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
250317 '',
@@ -262,6 +329,36 @@ jobs:
262329 if : env.DO_DEPLOY != 'false'
263330 run : git checkout ${{ steps.getRef.outputs.PR_REF }}
264331
332+ - name : Initialize GitHub Deployment
333+ if : env.DO_DEPLOY != 'false'
334+ uses : actions/github-script@v7
335+ id : init-deployment
336+ with :
337+ script : |
338+ const ref = process.env.PR_SHA;
339+ const environment = process.env.ENVIRONMENT_NAME || 'review-app';
340+
341+ const deployment = await github.rest.repos.createDeployment({
342+ owner: context.repo.owner,
343+ repo: context.repo.repo,
344+ ref: ref,
345+ environment: environment,
346+ auto_merge: false,
347+ required_contexts: [],
348+ description: `Deployment for PR #${process.env.PR_NUMBER}`
349+ });
350+
351+ // Create initial deployment status
352+ await github.rest.repos.createDeploymentStatus({
353+ owner: context.repo.owner,
354+ repo: context.repo.repo,
355+ deployment_id: deployment.data.id,
356+ state: 'in_progress',
357+ description: 'Deployment started'
358+ });
359+
360+ return deployment.data.id;
361+
265362 - name : Build Docker Image
266363 if : env.DO_DEPLOY != 'false'
267364 uses : ./.github/actions/build-docker-image
@@ -320,7 +417,7 @@ jobs:
320417 const deploymentStatus = {
321418 owner: context.repo.owner,
322419 repo: context.repo.repo,
323- deployment_id: ${{ fromJSON( steps.init-deployment.outputs.result).deploymentId }},
420+ deployment_id: ${{ steps.init-deployment.outputs.result }},
324421 state: isSuccess ? 'success' : 'failure',
325422 environment_url: isSuccess ? appUrl : undefined,
326423 log_url: workflowUrl,
0 commit comments