Skip to content

Commit c084dc4

Browse files
committed
Fix deploy
1 parent 9ae70fb commit c084dc4

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

.github/workflows/deploy-to-control-plane.yml

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ run-name: Deploy Review App - ${{ github.ref_name }}
55
on:
66
pull_request:
77
types: [opened, synchronize, reopened]
8+
push:
9+
branches:
10+
- '**' # Any branch
11+
- '!main' # Except main
12+
- '!master' # Except master
813
issue_comment:
914
types: [created]
1015
workflow_dispatch:
@@ -14,7 +19,6 @@ on:
1419
required: true
1520
type: number
1621

17-
# Use concurrency to cancel in-progress runs
1822
concurrency:
1923
group: deploy-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
2024
cancel-in-progress: true
@@ -29,6 +33,7 @@ jobs:
2933
Process-Deployment-Command:
3034
if: |
3135
(github.event_name == 'pull_request') ||
36+
(github.event_name == 'push') ||
3237
(github.event_name == 'workflow_dispatch') ||
3338
(github.event_name == 'issue_comment' &&
3439
github.event.issue.pull_request &&
@@ -42,20 +47,53 @@ jobs:
4247

4348
steps:
4449
- name: Get PR HEAD Ref
45-
if: github.event_name == 'issue_comment' || github.event_name == 'workflow_dispatch'
4650
id: getRef
51+
env:
52+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4753
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
5490
5591
- uses: actions/checkout@v4
5692
with:
5793
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) }}
5997

6098
- name: Setup Environment
6199
uses: ./.github/actions/setup-environment
@@ -75,44 +113,43 @@ jobs:
75113
fi
76114
echo "app_exists=true" >> $GITHUB_OUTPUT
77115
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
78129
79-
- name: Set Workflow URL
80-
id: workflow-url
130+
- name: Set Deployment URLs
131+
id: set-urls
81132
uses: actions/github-script@v7
82133
with:
83134
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({
86138
owner: context.repo.owner,
87139
repo: context.repo.repo,
88140
run_id: runId
89141
});
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+
};
100144
101145
const workflowUrl = await getWorkflowUrl(context.runId);
102146
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+
);
109151
110152
- 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')
116153
id: create-comment
117154
uses: actions/github-script@v7
118155
with:
@@ -121,17 +158,11 @@ jobs:
121158
owner: context.repo.owner,
122159
repo: context.repo.repo,
123160
issue_number: process.env.PR_NUMBER,
124-
body: '🚀 Starting deployment process...'
161+
body: '🚀 Deploying Review App...\n\n' + process.env.CONSOLE_LINK
125162
});
126-
console.log('Created comment:', result.data.id);
127-
return { commentId: result.data.id };
163+
return result.data.id;
128164
129165
- 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')
135166
run: echo "COMMENT_ID=${{ fromJSON(steps.create-comment.outputs.result).commentId }}" >> $GITHUB_ENV
136167

137168
- name: Initialize Deployment
@@ -194,23 +225,16 @@ jobs:
194225
echo "COMMIT_HASH=${FULL_COMMIT:0:7}" >> $GITHUB_ENV
195226
196227
- 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')
202228
uses: actions/github-script@v7
203229
with:
204230
script: |
205-
eval(process.env.GET_CONSOLE_LINK);
206-
207231
const buildingMessage = [
208232
'🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.COMMIT_HASH }}',
209233
'🏗️ Building Docker image...',
210234
'',
211235
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
212236
'',
213-
getConsoleLink(process.env.PR_NUMBER)
237+
process.env.CONSOLE_LINK
214238
].join('\n');
215239
216240
await github.rest.issues.updateComment({
@@ -221,19 +245,9 @@ jobs:
221245
});
222246
223247
- 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')
229248
run: git checkout ${{ steps.getRef.outputs.PR_REF }}
230249

231250
- 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')
237251
uses: ./.github/actions/build-docker-image
238252
with:
239253
app_name: ${{ env.APP_NAME }}
@@ -242,24 +256,17 @@ jobs:
242256
PR_NUMBER: ${{ env.PR_NUMBER }}
243257

244258
- 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')
250259
uses: actions/github-script@v7
251260
with:
252261
script: |
253-
eval(process.env.GET_CONSOLE_LINK);
254-
255262
const deployingMessage = [
256263
'🚀 Deploying to Control Plane...',
257264
'',
258265
'⏳ Waiting for deployment to be ready...',
259266
'',
260267
'📝 [View Deploy Logs](' + process.env.WORKFLOW_URL + ')',
261268
'',
262-
getConsoleLink(process.env.PR_NUMBER)
269+
process.env.CONSOLE_LINK
263270
].join('\n');
264271
265272
await github.rest.issues.updateComment({
@@ -270,11 +277,6 @@ jobs:
270277
});
271278
272279
- 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')
278280
uses: ./.github/actions/deploy-to-control-plane
279281
with:
280282
app_name: ${{ env.APP_NAME }}
@@ -294,8 +296,7 @@ jobs:
294296
const workflowUrl = process.env.WORKFLOW_URL;
295297
const isSuccess = '${{ job.status }}' === 'success';
296298
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;
299300
300301
// Create GitHub deployment status
301302
const deploymentStatus = {

0 commit comments

Comments
 (0)