@@ -5,6 +5,11 @@ run-name: Deploy Review App - ${{ github.ref_name }}
5
5
on :
6
6
pull_request :
7
7
types : [opened, synchronize, reopened]
8
+ push :
9
+ branches :
10
+ - ' **' # Any branch
11
+ - ' !main' # Except main
12
+ - ' !master' # Except master
8
13
issue_comment :
9
14
types : [created]
10
15
workflow_dispatch :
14
19
required : true
15
20
type : number
16
21
17
- # Use concurrency to cancel in-progress runs
18
22
concurrency :
19
23
group : deploy-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
20
24
cancel-in-progress : true
29
33
Process-Deployment-Command :
30
34
if : |
31
35
(github.event_name == 'pull_request') ||
36
+ (github.event_name == 'push') ||
32
37
(github.event_name == 'workflow_dispatch') ||
33
38
(github.event_name == 'issue_comment' &&
34
39
github.event.issue.pull_request &&
@@ -42,20 +47,53 @@ jobs:
42
47
43
48
steps :
44
49
- name : Get PR HEAD Ref
45
- if : github.event_name == 'issue_comment' || github.event_name == 'workflow_dispatch'
46
50
id : getRef
51
+ env :
52
+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
47
53
run : |
48
- echo "PR_NUMBER=${{ github.event.issue.number || github.event.inputs.pr_number }}" >> $GITHUB_ENV
49
- echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ github.event.issue.number || github.event.inputs.pr_number }}" >> $GITHUB_ENV
50
- # Get the PR head commit
51
- PR_DATA=$(gh pr view ${{ github.event.issue.number || github.event.inputs.pr_number }} --repo ${{ github.repository }} --json headRefName,headRefOid)
52
- echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
53
- echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
54
+ # Set PR number based on event type
55
+ if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
56
+ PR_NUMBER="${{ github.event.inputs.pr_number }}"
57
+ elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
58
+ PR_NUMBER="${{ github.event.issue.number }}"
59
+ elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
60
+ PR_NUMBER="${{ github.event.pull_request.number }}"
61
+ elif [[ "${{ github.event_name }}" == "push" ]]; then
62
+ # For push events, find associated PR
63
+ PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number --jq '.[0].number')
64
+ if [[ -n "$PR_DATA" ]]; then
65
+ PR_NUMBER="$PR_DATA"
66
+ else
67
+ echo "Error: No PR found for branch ${{ github.ref_name }}"
68
+ exit 1
69
+ fi
70
+ fi
71
+
72
+ if [[ -z "$PR_NUMBER" ]]; then
73
+ echo "Error: Could not determine PR number"
74
+ exit 1
75
+ fi
76
+
77
+ # Set environment variables
78
+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
79
+ echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-$PR_NUMBER" >> $GITHUB_ENV
80
+
81
+ # Get PR data using GitHub CLI
82
+ PR_DATA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName,headRefOid)
83
+ if [[ $? -eq 0 ]]; then
84
+ echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
85
+ echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
86
+ else
87
+ echo "Error: Could not fetch PR data for PR #$PR_NUMBER"
88
+ exit 1
89
+ fi
54
90
55
91
- uses : actions/checkout@v4
56
92
with :
57
93
fetch-depth : 0
58
- ref : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || steps.getRef.outputs.PR_REF || github.ref }}
94
+ # 1. For comment/manual: use branch from PR number lookup
95
+ # 2. For PR events: use the PR's branch
96
+ ref : ${{ steps.getRef.outputs.PR_REF || (github.event_name == 'pull_request' && github.event.pull_request.head.ref) }}
59
97
60
98
- name : Setup Environment
61
99
uses : ./.github/actions/setup-environment
@@ -75,44 +113,43 @@ jobs:
75
113
fi
76
114
echo "app_exists=true" >> $GITHUB_OUTPUT
77
115
116
+ - name : Validate Deployment Request
117
+ id : validate
118
+ run : |
119
+ if [[ "${{ github.event_name }}" == "pull_request" && "${{ steps.check-app.outputs.app_exists }}" == "true" ]] || \
120
+ [[ "${{ github.event_name }}" == "workflow_dispatch" ]] || \
121
+ [[ "${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app" ]] || \
122
+ [[ "${{ github.event_name }}" == "push" ]]; then
123
+ echo "SHOULD_DEPLOY=true" >> $GITHUB_ENV
124
+ else
125
+ echo "SHOULD_DEPLOY=false" >> $GITHUB_ENV
126
+ echo "Skipping deployment - not a valid trigger (event: ${{ github.event_name }})"
127
+ exit 0
128
+ fi
78
129
79
- - name : Set Workflow URL
80
- id : workflow-url
130
+ - name : Set Deployment URLs
131
+ id : set-urls
81
132
uses : actions/github-script@v7
82
133
with :
83
134
script : |
84
- async function getWorkflowUrl(runId) {
85
- const jobs = await github.rest.actions.listJobsForWorkflowRun({
135
+ // Set workflow URL for logs
136
+ const getWorkflowUrl = async (runId) => {
137
+ const { data: run } = await github.rest.actions.getWorkflowRun({
86
138
owner: context.repo.owner,
87
139
repo: context.repo.repo,
88
140
run_id: runId
89
141
});
90
-
91
- const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress');
92
- const jobId = currentJob?.id;
93
-
94
- if (!jobId) {
95
- return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
96
- }
97
-
98
- return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`;
99
- }
142
+ return run.html_url;
143
+ };
100
144
101
145
const workflowUrl = await getWorkflowUrl(context.runId);
102
146
core.exportVariable('WORKFLOW_URL', workflowUrl);
103
- core.exportVariable('GET_CONSOLE_LINK', `
104
- function getConsoleLink(prNumber) {
105
- return '🎮 [Control Plane Console](' +
106
- 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)';
107
- }
108
- `);
147
+ core.exportVariable('CONSOLE_LINK',
148
+ '🎮 [Control Plane Console](' +
149
+ 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
150
+ );
109
151
110
152
- name : Create Initial Comment
111
- if : |
112
- (github.event_name == 'issue_comment' &&
113
- github.event.issue.pull_request &&
114
- github.event.comment.body == '/deploy-review-app') ||
115
- ( steps.check-app.outputs.app_exists == 'true')
116
153
id : create-comment
117
154
uses : actions/github-script@v7
118
155
with :
@@ -121,17 +158,11 @@ jobs:
121
158
owner: context.repo.owner,
122
159
repo: context.repo.repo,
123
160
issue_number: process.env.PR_NUMBER,
124
- body: '🚀 Starting deployment process ...'
161
+ body: '🚀 Deploying Review App ...\n\n' + process.env.CONSOLE_LINK
125
162
});
126
- console.log('Created comment:', result.data.id);
127
- return { commentId: result.data.id };
163
+ return result.data.id;
128
164
129
165
- name : Set Comment ID
130
- if : |
131
- (github.event_name == 'issue_comment' &&
132
- github.event.issue.pull_request &&
133
- github.event.comment.body == '/deploy-review-app') ||
134
- (steps.check-app.outputs.app_exists == 'true')
135
166
run : echo "COMMENT_ID=${{ fromJSON(steps.create-comment.outputs.result).commentId }}" >> $GITHUB_ENV
136
167
137
168
- name : Initialize Deployment
@@ -194,23 +225,16 @@ jobs:
194
225
echo "COMMIT_HASH=${FULL_COMMIT:0:7}" >> $GITHUB_ENV
195
226
196
227
- name : Update Status - Building
197
- if : |
198
- (github.event_name == 'issue_comment' &&
199
- github.event.issue.pull_request &&
200
- github.event.comment.body == '/deploy-review-app') ||
201
- (steps.check-app.outputs.app_exists == 'true')
202
228
uses : actions/github-script@v7
203
229
with :
204
230
script : |
205
- eval(process.env.GET_CONSOLE_LINK);
206
-
207
231
const buildingMessage = [
208
232
'🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.COMMIT_HASH }}',
209
233
'🏗️ Building Docker image...',
210
234
'',
211
235
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
212
236
'',
213
- getConsoleLink( process.env.PR_NUMBER)
237
+ process.env.CONSOLE_LINK
214
238
].join('\n');
215
239
216
240
await github.rest.issues.updateComment({
@@ -221,19 +245,9 @@ jobs:
221
245
});
222
246
223
247
- name : Checkout PR Branch
224
- if : |
225
- (github.event_name == 'issue_comment' &&
226
- github.event.issue.pull_request &&
227
- github.event.comment.body == '/deploy-review-app') ||
228
- (steps.check-app.outputs.app_exists == 'true')
229
248
run : git checkout ${{ steps.getRef.outputs.PR_REF }}
230
249
231
250
- name : Build Docker Image
232
- if : |
233
- (github.event_name == 'issue_comment' &&
234
- github.event.issue.pull_request &&
235
- github.event.comment.body == '/deploy-review-app') ||
236
- (steps.check-app.outputs.app_exists == 'true')
237
251
uses : ./.github/actions/build-docker-image
238
252
with :
239
253
app_name : ${{ env.APP_NAME }}
@@ -242,24 +256,17 @@ jobs:
242
256
PR_NUMBER : ${{ env.PR_NUMBER }}
243
257
244
258
- name : Update Status - Deploying
245
- if : |
246
- (github.event_name == 'issue_comment' &&
247
- github.event.issue.pull_request &&
248
- github.event.comment.body == '/deploy-review-app') ||
249
- (steps.check-app.outputs.app_exists == 'true')
250
259
uses : actions/github-script@v7
251
260
with :
252
261
script : |
253
- eval(process.env.GET_CONSOLE_LINK);
254
-
255
262
const deployingMessage = [
256
263
'🚀 Deploying to Control Plane...',
257
264
'',
258
265
'⏳ Waiting for deployment to be ready...',
259
266
'',
260
267
'📝 [View Deploy Logs](' + process.env.WORKFLOW_URL + ')',
261
268
'',
262
- getConsoleLink( process.env.PR_NUMBER)
269
+ process.env.CONSOLE_LINK
263
270
].join('\n');
264
271
265
272
await github.rest.issues.updateComment({
@@ -270,11 +277,6 @@ jobs:
270
277
});
271
278
272
279
- name : Deploy to Control Plane
273
- if : |
274
- (github.event_name == 'issue_comment' &&
275
- github.event.issue.pull_request &&
276
- github.event.comment.body == '/deploy-review-app') ||
277
- (steps.check-app.outputs.app_exists == 'true')
278
280
uses : ./.github/actions/deploy-to-control-plane
279
281
with :
280
282
app_name : ${{ env.APP_NAME }}
@@ -294,8 +296,7 @@ jobs:
294
296
const workflowUrl = process.env.WORKFLOW_URL;
295
297
const isSuccess = '${{ job.status }}' === 'success';
296
298
297
- const consoleLink = '🎮 [Control Plane Console](https://console.cpln.io/console/org/' +
298
- process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)';
299
+ const consoleLink = process.env.CONSOLE_LINK;
299
300
300
301
// Create GitHub deployment status
301
302
const deploymentStatus = {
0 commit comments