-
Notifications
You must be signed in to change notification settings - Fork 384
435 lines (390 loc) · 16.6 KB
/
deploy-to-control-plane-review-app.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
name: Deploy PR Review App to Control Plane
run-name: Deploy PR Review App - PR #${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- '**' # Any branch
- '!main' # Except main
- '!master' # Except master
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr_number:
description: 'Pull Request number to deploy'
required: true
type: number
concurrency:
group: deploy-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
cancel-in-progress: true
env:
APP_NAME: ${{ vars.REVIEW_APP_PREFIX }}-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
jobs:
debug:
uses: ./.github/workflows/debug-workflow.yml
with:
debug_enabled: false
process-deployment:
needs: debug
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch') ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/deploy-review-app'))
runs-on: ubuntu-latest
outputs:
pr_number: ${{ env.PR_NUMBER }}
pr_sha: ${{ env.PR_SHA }}
pr_ref: ${{ steps.getRef.outputs.PR_REF }}
do_deploy: ${{ env.DO_DEPLOY }}
comment_id: ${{ steps.create-comment.outputs.comment-id }}
deployment_id: ${{ steps.init-deployment.outputs.result }}
steps:
# Initial checkout only for pull_request and push events
- name: Checkout code
if: github.event_name == 'pull_request' || github.event_name == 'push'
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }}
# Basic checkout for other events (workflow_dispatch, issue_comment)
# We'll do proper checkout after getting PR info
- name: Initial checkout
if: github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate Required Secrets and Variables
uses: ./.github/actions/validate-required-vars
- name: Get PR HEAD Ref
id: getRef
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# For push events, try to find associated PR first
if [[ "${{ github.event_name }}" == "push" ]]; then
PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number,headRefName,headRefOid --jq '.[0]')
if [[ -n "$PR_DATA" ]]; then
PR_NUMBER=$(echo "$PR_DATA" | jq -r .number)
else
echo "No PR found for branch ${{ github.ref_name }}, skipping deployment"
echo "DO_DEPLOY=false" >> $GITHUB_ENV
exit 0
fi
else
# Get PR number based on event type
case "${{ github.event_name }}" in
"workflow_dispatch")
PR_NUMBER="${{ github.event.inputs.pr_number }}"
;;
"issue_comment")
PR_NUMBER="${{ github.event.issue.number }}"
;;
"pull_request")
PR_NUMBER="${{ github.event.pull_request.number }}"
;;
*)
echo "Error: Unsupported event type ${{ github.event_name }}"
exit 1
;;
esac
fi
if [[ -z "$PR_NUMBER" ]]; then
echo "Error: Could not determine PR number"
echo "Event type: ${{ github.event_name }}"
echo "Event action: ${{ github.event.action }}"
echo "Ref name: ${{ github.ref_name }}"
echo "Available event data:"
echo "- PR number from inputs: ${{ github.event.inputs.pr_number }}"
echo "- PR number from issue: ${{ github.event.issue.number }}"
echo "- PR number from pull_request: ${{ github.event.pull_request.number }}"
exit 1
fi
# Get PR data
if [[ -z "$PR_DATA" ]]; then
PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,headRefOid)
if [[ -z "$PR_DATA" ]]; then
echo "Error: PR DATA for PR #$PR_NUMBER not found"
echo "Event type: ${{ github.event_name }}"
echo "Event action: ${{ github.event.action }}"
echo "Ref name: ${{ github.ref_name }}"
echo "Attempted to fetch PR data with: gh pr view $PR_NUMBER"
exit 1
fi
fi
# Extract and set PR data
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-$PR_NUMBER" >> $GITHUB_ENV
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
- name: Setup Environment
uses: ./.github/actions/setup-environment
with:
token: ${{ secrets.CPLN_TOKEN_STAGING }}
org: ${{ vars.CPLN_ORG_STAGING }}
- name: Check if Review App Exists
id: check-app
env:
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
run: |
# First check if cpflow exists
if ! command -v cpflow &> /dev/null; then
echo "Error: cpflow command not found"
exit 1
fi
# Check if app exists and save state
if ! cpflow exists -a ${{ env.APP_NAME }}; then
echo "APP_EXISTS=false" >> $GITHUB_ENV
else
echo "APP_EXISTS=true" >> $GITHUB_ENV
fi
- name: Validate Deployment Request
id: validate
run: |
# Skip validation if deployment is already disabled
if [[ "${{ env.DO_DEPLOY }}" == "false" ]]; then
echo "Skipping validation - deployment already disabled"
exit 0
fi
if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \
"${{ github.event_name }}" == "issue_comment" || \
"${{ github.event_name }}" == "pull_request" || \
"${{ github.event_name }}" == "push" ]]; then
echo "Error: Unsupported event type ${{ github.event_name }}"
exit 1
fi
# Set DO_DEPLOY based on event type and conditions
if [[ "${{ github.event_name }}" == "pull_request" && \
("${{ github.event.action }}" == "opened" || \
"${{ github.event.action }}" == "synchronize" || \
"${{ github.event.action }}" == "reopened") ]]; then
echo "DO_DEPLOY=true" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == "push" ]]; then
echo "DO_DEPLOY=true" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "DO_DEPLOY=true" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
if [[ "${{ github.event.issue.pull_request }}" ]]; then
# Trim spaces and check for exact command
COMMENT_BODY=$(echo "${{ github.event.comment.body }}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [[ "$COMMENT_BODY" == "/deploy-review-app" ]]; then
echo "DO_DEPLOY=true" >> $GITHUB_ENV
else
echo "DO_DEPLOY=false" >> $GITHUB_ENV
echo "Skipping deployment - comment '$COMMENT_BODY' does not match '/deploy-review-app'"
fi
else
echo "DO_DEPLOY=false" >> $GITHUB_ENV
echo "Skipping deployment for non-PR comment"
fi
fi
- name: Setup Control Plane App if Not Existing
if: env.DO_DEPLOY == 'true' && env.APP_EXISTS == 'false'
env:
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
run: |
echo "🔧 Setting up new Control Plane app..."
cpflow setup-app -a ${{ env.APP_NAME }} --org ${{ vars.CPLN_ORG_STAGING }}
- name: Create Initial Comment
if: env.DO_DEPLOY != 'false'
uses: actions/github-script@v7
id: create-comment
with:
script: |
const result = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
body: '🚀 Starting deployment process...\n\n' + process.env.CONSOLE_LINK
});
core.setOutput('comment-id', result.data.id);
- name: Set Deployment URLs
id: set-urls
if: env.DO_DEPLOY != 'false'
uses: actions/github-script@v7
with:
script: |
// Set workflow URL for logs
const getWorkflowUrl = async (runId) => {
const { data: run } = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
// Get the job ID for this specific job
const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
const currentJob = jobs.jobs.find(job => job.name === context.job);
return `${run.html_url}/job/${currentJob.id}`;
};
const workflowUrl = await getWorkflowUrl(context.runId);
core.exportVariable('WORKFLOW_URL', workflowUrl);
core.exportVariable('CONSOLE_LINK',
'🎮 [Control Plane Console](' +
'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
);
- name: Initialize GitHub Deployment
if: env.DO_DEPLOY != 'false'
uses: actions/github-script@v7
id: init-deployment
with:
script: |
const ref = process.env.PR_SHA;
const environment = process.env.ENVIRONMENT_NAME || 'review-app';
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: ref,
environment: environment,
auto_merge: false,
required_contexts: [],
description: `Deployment for PR #${process.env.PR_NUMBER}`
});
// Create initial deployment status
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.data.id,
state: 'in_progress',
description: 'Deployment started'
});
return deployment.data.id;
build:
needs: process-deployment
if: needs.process-deployment.outputs.do_deploy != 'false'
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.build.outputs.image_tag }}
comment_id: ${{ needs.process-deployment.outputs.comment_id }}
pr_number: ${{ needs.process-deployment.outputs.pr_number }}
do_deploy: ${{ needs.process-deployment.outputs.do_deploy }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.process-deployment.outputs.pr_ref }}
- name: Setup Environment
uses: ./.github/actions/setup-environment
with:
token: ${{ secrets.CPLN_TOKEN_STAGING }}
org: ${{ vars.CPLN_ORG_STAGING }}
- name: Update Status - Building
uses: actions/github-script@v7
with:
script: |
const buildingMessage = [
'🏗️ Building Docker image for PR #${{ needs.process-deployment.outputs.pr_number }}, commit ${{ needs.process-deployment.outputs.pr_sha }}',
'',
'📝 [View Build Logs](${{ env.WORKFLOW_URL }})',
'',
process.env.CONSOLE_LINK
].join('\n');
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ needs.process-deployment.outputs.comment_id }},
body: buildingMessage
});
- name: Build Docker Image
id: build
uses: ./.github/actions/build-docker-image
with:
app_name: ${{ env.APP_NAME }}
org: ${{ vars.CPLN_ORG_STAGING }}
commit: ${{ needs.process-deployment.outputs.pr_sha }}
PR_NUMBER: ${{ needs.process-deployment.outputs.pr_number }}
deploy:
needs: build
if: needs.build.outputs.do_deploy != 'false'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
with:
token: ${{ secrets.CPLN_TOKEN_STAGING }}
org: ${{ vars.CPLN_ORG_STAGING }}
- name: Update Status - Deploying
uses: actions/github-script@v7
with:
script: |
const deployingMessage = [
'🚀 Deploying to Control Plane...',
'',
'⏳ Waiting for deployment to be ready...',
'',
'📝 [View Deploy Logs](${{ env.WORKFLOW_URL }})',
'',
process.env.CONSOLE_LINK
].join('\n');
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.create-comment.outputs.comment-id }},
body: deployingMessage
});
- name: Deploy to Control Plane
if: env.DO_DEPLOY != 'false'
uses: ./.github/actions/deploy-to-control-plane
with:
app_name: ${{ env.APP_NAME }}
org: ${{ vars.CPLN_ORG_STAGING }}
github_token: ${{ secrets.GITHUB_TOKEN }}
wait_timeout: ${{ vars.WAIT_TIMEOUT || 900 }}
cpln_token: ${{ secrets.CPLN_TOKEN_STAGING }}
pr_number: ${{ env.PR_NUMBER }}
- name: Update Status - Deployment Complete
if: env.DO_DEPLOY != 'false'
uses: actions/github-script@v7
with:
script: |
const prNumber = process.env.PR_NUMBER;
const appUrl = process.env.APP_URL;
const workflowUrl = process.env.WORKFLOW_URL;
const isSuccess = '${{ job.status }}' === 'success';
const consoleLink = process.env.CONSOLE_LINK;
// Create GitHub deployment status
const deploymentStatus = {
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.init-deployment.outputs.result }},
state: isSuccess ? 'success' : 'failure',
environment_url: isSuccess ? appUrl : undefined,
log_url: workflowUrl,
environment: 'review'
};
await github.rest.repos.createDeploymentStatus(deploymentStatus);
// Define messages based on deployment status
const successMessage = [
'✅ Deployment complete for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
'',
'🚀 [Review App for PR #' + prNumber + '](' + appUrl + ')',
consoleLink,
'',
'📋 [View Completed Action Build and Deploy Logs](' + workflowUrl + ')'
].join('\n');
const failureMessage = [
'❌ Deployment failed for PR #' + prNumber + ', commit ' + '${{ env.PR_SHA }}',
'',
consoleLink,
'',
'📋 [View Deployment Logs with Errors](' + workflowUrl + ')'
].join('\n');
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.create-comment.outputs.comment-id }},
body: isSuccess ? successMessage : failureMessage
});