Skip to content

Commit 0d4d7a0

Browse files
committed
handle calling cpflow setup for new app
1 parent 180ee90 commit 0d4d7a0

File tree

1 file changed

+103
-51
lines changed

1 file changed

+103
-51
lines changed

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

+103-51
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:
@@ -37,7 +37,7 @@ jobs:
3737
(github.event_name == 'workflow_dispatch') ||
3838
(github.event_name == 'issue_comment' &&
3939
github.event.issue.pull_request &&
40-
github.event.comment.body == '/deploy-review-app')
40+
contains(github.event.comment.body, '/deploy-review-app'))
4141
runs-on: ubuntu-latest
4242
permissions:
4343
contents: read
@@ -91,51 +91,65 @@ jobs:
9191
env:
9292
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9393
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 }}"
94+
# For push events, try to find associated PR first
95+
if [[ "${{ github.event_name }}" == "push" ]]; then
96+
PR_DATA=$(gh pr list --head "${{ github.ref_name }}" --json number,headRefName,headRefOid --jq '.[0]')
97+
if [[ -n "$PR_DATA" ]]; then
98+
PR_NUMBER=$(echo "$PR_DATA" | jq -r .number)
99+
else
100+
echo "No PR found for branch ${{ github.ref_name }}, skipping deployment"
101+
echo "DO_DEPLOY=false" >> $GITHUB_ENV
102+
exit 0
103+
fi
104+
else
105+
# Get PR number based on event type
106+
case "${{ github.event_name }}" in
107+
"workflow_dispatch")
108+
PR_NUMBER="${{ github.event.inputs.pr_number }}"
109+
;;
110+
"issue_comment")
111+
PR_NUMBER="${{ github.event.issue.number }}"
112+
;;
113+
"pull_request")
114+
PR_NUMBER="${{ github.event.pull_request.number }}"
115+
;;
116+
*)
117+
echo "Error: Unsupported event type ${{ github.event_name }}"
112118
exit 1
113-
fi
114-
;;
115-
*)
116-
echo "Error: Unsupported event type ${{ github.event_name }}"
117-
exit 1
118-
;;
119-
esac
120-
119+
;;
120+
esac
121+
fi
122+
121123
if [[ -z "$PR_NUMBER" ]]; then
122124
echo "Error: Could not determine PR number"
125+
echo "Event type: ${{ github.event_name }}"
126+
echo "Event action: ${{ github.event.action }}"
127+
echo "Ref name: ${{ github.ref_name }}"
128+
echo "Available event data:"
129+
echo "- PR number from inputs: ${{ github.event.inputs.pr_number }}"
130+
echo "- PR number from issue: ${{ github.event.issue.number }}"
131+
echo "- PR number from pull_request: ${{ github.event.pull_request.number }}"
123132
exit 1
124133
fi
125134
126-
# Set environment variables
135+
# Get PR data
136+
if [[ -z "$PR_DATA" ]]; then
137+
PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,headRefOid)
138+
if [[ -z "$PR_DATA" ]]; then
139+
echo "Error: PR DATA for PR #$PR_NUMBER not found"
140+
echo "Event type: ${{ github.event_name }}"
141+
echo "Event action: ${{ github.event.action }}"
142+
echo "Ref name: ${{ github.ref_name }}"
143+
echo "Attempted to fetch PR data with: gh pr view $PR_NUMBER"
144+
exit 1
145+
fi
146+
fi
147+
148+
# Extract and set PR data
127149
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
128150
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
151+
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
152+
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
139153
140154
- name: Checkout PR code
141155
if: github.event_name == 'workflow_dispatch' || github.event_name == 'issue_comment'
@@ -152,7 +166,6 @@ jobs:
152166

153167
- name: Check if Review App Exists
154168
id: check-app
155-
if: github.event_name == 'pull_request'
156169
env:
157170
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
158171
run: |
@@ -162,25 +175,64 @@ jobs:
162175
exit 1
163176
fi
164177
165-
# Then check if app exists
178+
# Check if app exists and save state
166179
if ! cpflow exists -a ${{ env.APP_NAME }}; then
167-
echo "No review app exists for this PR"
168-
echo "DO_DEPLOY=false" >> $GITHUB_ENV
180+
echo "APP_EXISTS=false" >> $GITHUB_ENV
169181
else
170-
echo "DO_DEPLOY=true" >> $GITHUB_ENV
182+
echo "APP_EXISTS=true" >> $GITHUB_ENV
171183
fi
172184
173185
- name: Validate Deployment Request
174186
id: validate
175-
if: env.DO_DEPLOY != 'false'
176187
run: |
188+
# Skip validation if deployment is already disabled
189+
if [[ "${{ env.DO_DEPLOY }}" == "false" ]]; then
190+
echo "Skipping validation - deployment already disabled"
191+
exit 0
192+
fi
193+
177194
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 }})"
195+
"${{ github.event_name }}" == "issue_comment" || \
196+
"${{ github.event_name }}" == "pull_request" || \
197+
"${{ github.event_name }}" == "push" ]]; then
198+
echo "Error: Unsupported event type ${{ github.event_name }}"
181199
exit 1
182200
fi
183201
202+
# Set DO_DEPLOY based on event type and conditions
203+
if [[ "${{ github.event_name }}" == "pull_request" && \
204+
("${{ github.event.action }}" == "opened" || \
205+
"${{ github.event.action }}" == "synchronize" || \
206+
"${{ github.event.action }}" == "reopened") ]]; then
207+
echo "DO_DEPLOY=true" >> $GITHUB_ENV
208+
elif [[ "${{ github.event_name }}" == "push" ]]; then
209+
echo "DO_DEPLOY=true" >> $GITHUB_ENV
210+
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
211+
echo "DO_DEPLOY=true" >> $GITHUB_ENV
212+
elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
213+
if [[ "${{ github.event.issue.pull_request }}" ]]; then
214+
# Trim spaces and check for exact command
215+
COMMENT_BODY=$(echo "${{ github.event.comment.body }}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
216+
if [[ "$COMMENT_BODY" == "/deploy-review-app" ]]; then
217+
echo "DO_DEPLOY=true" >> $GITHUB_ENV
218+
else
219+
echo "DO_DEPLOY=false" >> $GITHUB_ENV
220+
echo "Skipping deployment - comment '$COMMENT_BODY' does not match '/deploy-review-app'"
221+
fi
222+
else
223+
echo "DO_DEPLOY=false" >> $GITHUB_ENV
224+
echo "Skipping deployment for non-PR comment"
225+
fi
226+
fi
227+
228+
- name: Setup Control Plane App if Not Existing
229+
if: env.DO_DEPLOY == 'true' && env.APP_EXISTS == 'false'
230+
env:
231+
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
232+
run: |
233+
echo "🔧 Setting up new Control Plane app..."
234+
cpflow setup-app -a ${{ env.APP_NAME }} --org ${{ vars.CPLN_ORG_STAGING }}
235+
184236
- name: Create Initial Comment
185237
if: env.DO_DEPLOY != 'false'
186238
uses: actions/github-script@v7
@@ -228,7 +280,7 @@ jobs:
228280
repo: context.repo.repo,
229281
run_id: runId
230282
});
231-
return run.html_url;
283+
return run.html_url + '/job/' + context.job;
232284
};
233285
234286
const workflowUrl = await getWorkflowUrl(context.runId);
@@ -244,7 +296,7 @@ jobs:
244296
with:
245297
script: |
246298
const buildingMessage = [
247-
'🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + process.env.PR_SHA,
299+
'🏗️ Building Docker image for PR #' + process.env.PR_NUMBER + ', commit ' + '${{ env.PR_SHA }}',
248300
'',
249301
'📝 [View Build Logs](' + process.env.WORKFLOW_URL + ')',
250302
'',

0 commit comments

Comments
 (0)