1
- name : Deploy Review App to Control Plane
1
+ name : Deploy PR Review App to Control Plane
2
2
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 }}
4
4
5
5
on :
6
6
pull_request :
30
30
PR_NUMBER : ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
31
31
32
32
jobs :
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
+
33
38
Process-Deployment-Command :
39
+ needs : debug # Add this to ensure debug runs first
34
40
if : |
35
41
(github.event_name == 'pull_request') ||
36
42
(github.event_name == 'push') ||
37
43
(github.event_name == 'workflow_dispatch') ||
38
44
(github.event_name == 'issue_comment' &&
39
45
github.event.issue.pull_request &&
40
- github.event.comment.body == '/deploy-review-app')
46
+ contains( github.event.comment.body, '/deploy-review-app') )
41
47
runs-on : ubuntu-latest
42
48
permissions :
43
49
contents : read
@@ -91,51 +97,65 @@ jobs:
91
97
env :
92
98
GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
93
99
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 }}"
112
124
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
+
121
129
if [[ -z "$PR_NUMBER" ]]; then
122
130
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 }}"
123
138
exit 1
124
139
fi
125
140
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
127
155
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
128
156
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
139
159
140
160
- name : Checkout PR code
141
161
if : github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
@@ -152,7 +172,6 @@ jobs:
152
172
153
173
- name : Check if Review App Exists
154
174
id : check-app
155
- if : github.event_name == 'pull_request'
156
175
env :
157
176
CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
158
177
run : |
@@ -162,25 +181,64 @@ jobs:
162
181
exit 1
163
182
fi
164
183
165
- # Then check if app exists
184
+ # Check if app exists and save state
166
185
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
169
187
else
170
- echo "DO_DEPLOY =true" >> $GITHUB_ENV
188
+ echo "APP_EXISTS =true" >> $GITHUB_ENV
171
189
fi
172
190
173
191
- name : Validate Deployment Request
174
192
id : validate
175
- if : env.DO_DEPLOY != 'false'
176
193
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
+
177
200
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 }}"
181
205
exit 1
182
206
fi
183
207
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
+
184
242
- name : Create Initial Comment
185
243
if : env.DO_DEPLOY != 'false'
186
244
uses : actions/github-script@v7
@@ -228,7 +286,7 @@ jobs:
228
286
repo: context.repo.repo,
229
287
run_id: runId
230
288
});
231
- return run.html_url;
289
+ return run.html_url + '/job/' + context.job ;
232
290
};
233
291
234
292
const workflowUrl = await getWorkflowUrl(context.runId);
@@ -244,7 +302,7 @@ jobs:
244
302
with :
245
303
script : |
246
304
const buildingMessage = [
247
- '🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + process. env.PR_SHA,
305
+ '🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.PR_SHA }}' ,
248
306
'',
249
307
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
250
308
'',
@@ -262,6 +320,36 @@ jobs:
262
320
if : env.DO_DEPLOY != 'false'
263
321
run : git checkout ${{ steps.getRef.outputs.PR_REF }}
264
322
323
+ - name : Initialize GitHub Deployment
324
+ if : env.DO_DEPLOY != 'false'
325
+ uses : actions/github-script@v7
326
+ id : init-deployment
327
+ with :
328
+ script : |
329
+ const ref = process.env.PR_SHA;
330
+ const environment = process.env.ENVIRONMENT_NAME || 'review-app';
331
+
332
+ const deployment = await github.rest.repos.createDeployment({
333
+ owner: context.repo.owner,
334
+ repo: context.repo.repo,
335
+ ref: ref,
336
+ environment: environment,
337
+ auto_merge: false,
338
+ required_contexts: [],
339
+ description: `Deployment for PR #${process.env.PR_NUMBER}`
340
+ });
341
+
342
+ // Create initial deployment status
343
+ await github.rest.repos.createDeploymentStatus({
344
+ owner: context.repo.owner,
345
+ repo: context.repo.repo,
346
+ deployment_id: deployment.data.id,
347
+ state: 'in_progress',
348
+ description: 'Deployment started'
349
+ });
350
+
351
+ return deployment.data.id;
352
+
265
353
- name : Build Docker Image
266
354
if : env.DO_DEPLOY != 'false'
267
355
uses : ./.github/actions/build-docker-image
@@ -320,7 +408,7 @@ jobs:
320
408
const deploymentStatus = {
321
409
owner: context.repo.owner,
322
410
repo: context.repo.repo,
323
- deployment_id: ${{ fromJSON( steps.init-deployment.outputs.result).deploymentId }},
411
+ deployment_id: ${{ steps.init-deployment.outputs.result }},
324
412
state: isSuccess ? 'success' : 'failure',
325
413
environment_url: isSuccess ? appUrl : undefined,
326
414
log_url: workflowUrl,
0 commit comments