Skip to content

Commit 3d6bf48

Browse files
committed
handle calling cpflow setup for new app
1 parent 180ee90 commit 3d6bf48

File tree

2 files changed

+176
-52
lines changed

2 files changed

+176
-52
lines changed

.github/workflows/debug-workflow.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Debug Workflow Information
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
debug_enabled:
7+
required: false
8+
type: boolean
9+
default: false
10+
description: 'Enable debug logging (defaults to false)'
11+
12+
jobs:
13+
debug-info:
14+
runs-on: ubuntu-latest
15+
if: inputs.debug_enabled || vars.DEBUG_WORKFLOW == 'true'
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Log Branch Info
22+
run: |
23+
echo "Branch for this run:"
24+
if [ "${{ github.event_name }}" == "pull_request" ]; then
25+
echo "Pull Request Source Branch: ${{ github.head_ref }}"
26+
else
27+
echo "Branch: ${{ github.ref_name }}"
28+
fi
29+
30+
- name: Debug GitHub Context
31+
run: |
32+
echo "Event name: ${{ github.event_name }}"
33+
echo "Event path: ${{ github.event_path }}"
34+
echo "Repository: ${{ github.repository }}"
35+
echo "Full GitHub context:"
36+
echo '${{ toJson(github) }}'

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

+140-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
name: Deploy Review App to Control Plane
1+
name: Deploy PR Review App to Control Plane
22

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 }}
44

55
on:
66
pull_request:
@@ -30,14 +30,20 @@ env:
3030
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
3131

3232
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+
3338
Process-Deployment-Command:
39+
needs: debug # Add this to ensure debug runs first
3440
if: |
3541
(github.event_name == 'pull_request') ||
3642
(github.event_name == 'push') ||
3743
(github.event_name == 'workflow_dispatch') ||
3844
(github.event_name == 'issue_comment' &&
3945
github.event.issue.pull_request &&
40-
github.event.comment.body == '/deploy-review-app')
46+
contains(github.event.comment.body, '/deploy-review-app'))
4147
runs-on: ubuntu-latest
4248
permissions:
4349
contents: read
@@ -91,51 +97,65 @@ jobs:
9197
env:
9298
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9399
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 }}"
112124
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+
121129
if [[ -z "$PR_NUMBER" ]]; then
122130
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 }}"
123138
exit 1
124139
fi
125140
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
127155
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
128156
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
139159
140160
- name: Checkout PR code
141161
if: github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
@@ -152,7 +172,6 @@ jobs:
152172

153173
- name: Check if Review App Exists
154174
id: check-app
155-
if: github.event_name == 'pull_request'
156175
env:
157176
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
158177
run: |
@@ -162,25 +181,64 @@ jobs:
162181
exit 1
163182
fi
164183
165-
# Then check if app exists
184+
# Check if app exists and save state
166185
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
169187
else
170-
echo "DO_DEPLOY=true" >> $GITHUB_ENV
188+
echo "APP_EXISTS=true" >> $GITHUB_ENV
171189
fi
172190
173191
- name: Validate Deployment Request
174192
id: validate
175-
if: env.DO_DEPLOY != 'false'
176193
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+
177200
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 }}"
181205
exit 1
182206
fi
183207
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+
184242
- name: Create Initial Comment
185243
if: env.DO_DEPLOY != 'false'
186244
uses: actions/github-script@v7
@@ -228,7 +286,7 @@ jobs:
228286
repo: context.repo.repo,
229287
run_id: runId
230288
});
231-
return run.html_url;
289+
return run.html_url + '/job/' + context.job;
232290
};
233291
234292
const workflowUrl = await getWorkflowUrl(context.runId);
@@ -244,7 +302,7 @@ jobs:
244302
with:
245303
script: |
246304
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 }}',
248306
'',
249307
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
250308
'',
@@ -262,6 +320,36 @@ jobs:
262320
if: env.DO_DEPLOY != 'false'
263321
run: git checkout ${{ steps.getRef.outputs.PR_REF }}
264322

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+
265353
- name: Build Docker Image
266354
if: env.DO_DEPLOY != 'false'
267355
uses: ./.github/actions/build-docker-image
@@ -320,7 +408,7 @@ jobs:
320408
const deploymentStatus = {
321409
owner: context.repo.owner,
322410
repo: context.repo.repo,
323-
deployment_id: ${{ fromJSON(steps.init-deployment.outputs.result).deploymentId }},
411+
deployment_id: ${{ steps.init-deployment.outputs.result }},
324412
state: isSuccess ? 'success' : 'failure',
325413
environment_url: isSuccess ? appUrl : undefined,
326414
log_url: workflowUrl,

0 commit comments

Comments
 (0)