@@ -46,14 +46,29 @@ jobs:
46
46
issues : write
47
47
48
48
steps :
49
+ # Initial checkout only for pull_request and push events
50
+ - name : Checkout code
51
+ if : github.event_name == 'pull_request' || github.event_name == 'push'
52
+ uses : actions/checkout@v4
53
+ with :
54
+ fetch-depth : 0
55
+ ref : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }}
56
+
57
+ # Basic checkout for other events (workflow_dispatch, issue_comment)
58
+ # We'll do proper checkout after getting PR info
59
+ - name : Initial checkout
60
+ if : github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
61
+ uses : actions/checkout@v4
62
+ with :
63
+ fetch-depth : 0
64
+
49
65
- name : Get PR HEAD Ref
50
66
id : getRef
51
67
env :
52
68
GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
53
69
run : |
54
- # Set PR number based on event type
55
70
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
56
- PR_NUMBER="${{ github.event.inputs.pr_number }}"
71
+ PR_NUMBER="${{ github.event.inputs.pr }}"
57
72
elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
58
73
PR_NUMBER="${{ github.event.issue.number }}"
59
74
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
@@ -68,18 +83,18 @@ jobs:
68
83
exit 1
69
84
fi
70
85
fi
71
-
86
+
72
87
if [[ -z "$PR_NUMBER" ]]; then
73
88
echo "Error: Could not determine PR number"
74
89
exit 1
75
90
fi
76
-
91
+
77
92
# Set environment variables
78
93
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
79
94
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-$PR_NUMBER" >> $GITHUB_ENV
80
95
81
96
# Get PR data using GitHub CLI
82
- PR_DATA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName,headRefOid)
97
+ PR_DATA=$(gh pr view $PR_NUMBER --repo shakacode/react-webpack-rails-tutorial --json headRefName,headRefOid)
83
98
if [[ $? -eq 0 ]]; then
84
99
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
85
100
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
@@ -88,64 +103,57 @@ jobs:
88
103
exit 1
89
104
fi
90
105
91
- - uses : actions/checkout@v4
106
+ - name : Checkout PR code
107
+ if : github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
108
+ uses : actions/checkout@v4
92
109
with :
93
110
fetch-depth : 0
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) }}
111
+ ref : ${{ steps.getRef.outputs.PR_SHA }}
97
112
98
113
- name : Setup Environment
99
114
uses : ./.github/actions/setup-environment
100
115
with :
101
- token : ${{ env.CPLN_TOKEN }}
102
- org : ${{ env.CPLN_ORG }}
116
+ token : ${{ secrets.CPLN_TOKEN_STAGING }}
117
+ org : ${{ vars.CPLN_ORG_STAGING }}
103
118
104
119
- name : Check if Review App Exists
105
120
id : check-app
121
+ if : github.event_name == 'pull_request'
122
+ env :
123
+ CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
106
124
run : |
125
+ # First check if cpflow exists
126
+ if ! command -v cpflow &> /dev/null; then
127
+ echo "Error: cpflow command not found"
128
+ exit 1
129
+ fi
130
+
131
+ # Then check if app exists
107
132
if ! cpflow exists -a ${{ env.APP_NAME }}; then
108
133
echo "No review app exists for this PR"
134
+ echo "exists=false" >> $GITHUB_OUTPUT
109
135
exit 0
110
136
fi
111
- echo "app_exists=true" >> $GITHUB_OUTPUT
137
+ echo "exists=true" >> $GITHUB_OUTPUT
138
+
139
+ - name : Exit if no app exists
140
+ if : |
141
+ github.event_name == 'pull_request' &&
142
+ steps.check-app.outputs.exists == 'false'
143
+ run : |
144
+ echo "Exiting workflow - no review app exists for this PR"
145
+ exit 1
112
146
113
147
- name : Validate Deployment Request
114
148
id : validate
115
149
run : |
116
- if [[ "${{ github.event_name }}" == "pull_request" && "${{ steps.check-app.outputs.app_exists }}" == "true" ]] || \
117
- [[ "${{ github.event_name }}" == "workflow_dispatch" ]] || \
118
- [[ "${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app" ]] || \
119
- [[ "${{ github.event_name }}" == "push" ]]; then
120
- echo "SHOULD_DEPLOY=true" >> $GITHUB_ENV
121
- else
122
- echo "SHOULD_DEPLOY=false" >> $GITHUB_ENV
150
+ if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \
151
+ ("${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app") || \
152
+ "${{ github.event_name }}" == "pull_request" ]]; then
123
153
echo "Skipping deployment - not a valid trigger (event: ${{ github.event_name }})"
124
- exit 0
154
+ exit 1
125
155
fi
126
156
127
- - name : Set Deployment URLs
128
- id : set-urls
129
- uses : actions/github-script@v7
130
- with :
131
- script : |
132
- // Set workflow URL for logs
133
- const getWorkflowUrl = async (runId) => {
134
- const { data: run } = await github.rest.actions.getWorkflowRun({
135
- owner: context.repo.owner,
136
- repo: context.repo.repo,
137
- run_id: runId
138
- });
139
- return run.html_url;
140
- };
141
-
142
- const workflowUrl = await getWorkflowUrl(context.runId);
143
- core.exportVariable('WORKFLOW_URL', workflowUrl);
144
- core.exportVariable('CONSOLE_LINK',
145
- '🎮 [Control Plane Console](' +
146
- 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
147
- );
148
-
149
157
- name : Create Initial Comment
150
158
id : create-comment
151
159
uses : actions/github-script@v7
@@ -155,71 +163,31 @@ jobs:
155
163
owner: context.repo.owner,
156
164
repo: context.repo.repo,
157
165
issue_number: process.env.PR_NUMBER,
158
- body: '🚀 Deploying Review App ...\n\n' + process.env.CONSOLE_LINK
166
+ body: '🚀 Starting deployment process ...\n\n' + process.env.CONSOLE_LINK
159
167
});
160
- return result.data.id;
161
-
162
- - name : Set Comment ID
163
- run : echo "COMMENT_ID=${{ fromJSON(steps.create-comment.outputs.result).commentId }}" >> $GITHUB_ENV
168
+ core.setOutput('comment-id', result.data.id);
164
169
165
- - name : Initialize Deployment
166
- id : init-deployment
170
+ - name : Set Deployment URLs
171
+ id : set-urls
167
172
uses : actions/github-script@v7
168
173
with :
169
174
script : |
170
- async function getWorkflowUrl(runId) {
171
- const jobs = await github.rest.actions.listJobsForWorkflowRun({
175
+ // Set workflow URL for logs
176
+ const getWorkflowUrl = async (runId) => {
177
+ const { data: run } = await github.rest.actions.getWorkflowRun({
172
178
owner: context.repo.owner,
173
179
repo: context.repo.repo,
174
180
run_id: runId
175
181
});
176
-
177
- const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress');
178
- const jobId = currentJob?.id;
179
-
180
- if (!jobId) {
181
- console.log('Warning: Could not find current job ID');
182
- return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
183
- }
184
-
185
- return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`;
186
- }
187
-
188
- // Create initial deployment comment
189
- const comment = await github.rest.issues.createComment({
190
- owner: context.repo.owner,
191
- repo: context.repo.repo,
192
- issue_number: process.env.PR_NUMBER,
193
- body: '⏳ Initializing deployment...'
194
- });
182
+ return run.html_url;
183
+ };
195
184
196
- // Create GitHub deployment
197
- const deployment = await github.rest.repos.createDeployment({
198
- owner: context.repo.owner,
199
- repo: context.repo.repo,
200
- ref: context.sha,
201
- environment: 'review',
202
- auto_merge: false,
203
- required_contexts: []
204
- });
205
-
206
185
const workflowUrl = await getWorkflowUrl(context.runId);
207
-
208
- return {
209
- deploymentId: deployment.data.id,
210
- commentId: comment.data.id,
211
- workflowUrl
212
- };
213
-
214
- - name : Set comment ID and workflow URL
215
- run : |
216
- echo "COMMENT_ID=${{ fromJSON(steps.init-deployment.outputs.result).commentId }}" >> $GITHUB_ENV
217
- echo "WORKFLOW_URL=${{ fromJSON(steps.init-deployment.outputs.result).workflowUrl }}" >> $GITHUB_ENV
218
-
219
- - name : Set commit hash
220
- run : |
221
- FULL_COMMIT="${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || steps.getRef.outputs.PR_SHA || github.sha }}"
222
- echo "COMMIT_HASH=${FULL_COMMIT:0:7}" >> $GITHUB_ENV
186
+ core.exportVariable('WORKFLOW_URL', workflowUrl);
187
+ core.exportVariable('CONSOLE_LINK',
188
+ '🎮 [Control Plane Console](' +
189
+ 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG_STAGING + '/gvc/' + process.env.APP_NAME + '/-info)'
190
+ );
223
191
224
192
- name : Update Status - Building
225
193
uses : actions/github-script@v7
@@ -237,7 +205,7 @@ jobs:
237
205
await github.rest.issues.updateComment({
238
206
owner: context.repo.owner,
239
207
repo: context.repo.repo,
240
- comment_id: process.env.COMMENT_ID ,
208
+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
241
209
body: buildingMessage
242
210
});
243
211
@@ -248,7 +216,7 @@ jobs:
248
216
uses : ./.github/actions/build-docker-image
249
217
with :
250
218
app_name : ${{ env.APP_NAME }}
251
- org : ${{ env.CPLN_ORG }}
219
+ org : ${{ env.CPLN_ORG_STAGING }}
252
220
commit : ${{ env.COMMIT_HASH }}
253
221
PR_NUMBER : ${{ env.PR_NUMBER }}
254
222
@@ -269,15 +237,15 @@ jobs:
269
237
await github.rest.issues.updateComment({
270
238
owner: context.repo.owner,
271
239
repo: context.repo.repo,
272
- comment_id: process.env.COMMENT_ID ,
240
+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
273
241
body: deployingMessage
274
242
});
275
243
276
244
- name : Deploy to Control Plane
277
245
uses : ./.github/actions/deploy-to-control-plane
278
246
with :
279
247
app_name : ${{ env.APP_NAME }}
280
- org : ${{ env.CPLN_ORG }}
248
+ org : ${{ env.CPLN_ORG_STAGING }}
281
249
github_token : ${{ secrets.GITHUB_TOKEN }}
282
250
wait_timeout : ${{ vars.WAIT_TIMEOUT || 900 }}
283
251
cpln_token : ${{ secrets.CPLN_TOKEN_STAGING }}
@@ -329,6 +297,6 @@ jobs:
329
297
await github.rest.issues.updateComment({
330
298
owner: context.repo.owner,
331
299
repo: context.repo.repo,
332
- comment_id: process.env.COMMENT_ID ,
300
+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
333
301
body: isSuccess ? successMessage : failureMessage
334
302
});
0 commit comments