Skip to content

Commit f548d2a

Browse files
committed
fixes
1 parent 45b512c commit f548d2a

File tree

2 files changed

+56
-98
lines changed

2 files changed

+56
-98
lines changed

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

+55-97
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,29 @@ jobs:
4646
issues: write
4747

4848
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+
4965
- name: Get PR HEAD Ref
5066
id: getRef
5167
env:
5268
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5369
run: |
54-
# Set PR number based on event type
5570
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
56-
PR_NUMBER="${{ github.event.inputs.pr_number }}"
71+
PR_NUMBER="${{ github.event.inputs.pr }}"
5772
elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
5873
PR_NUMBER="${{ github.event.issue.number }}"
5974
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
@@ -68,18 +83,18 @@ jobs:
6883
exit 1
6984
fi
7085
fi
71-
86+
7287
if [[ -z "$PR_NUMBER" ]]; then
7388
echo "Error: Could not determine PR number"
7489
exit 1
7590
fi
76-
91+
7792
# Set environment variables
7893
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
7994
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-$PR_NUMBER" >> $GITHUB_ENV
8095
8196
# 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)
8398
if [[ $? -eq 0 ]]; then
8499
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
85100
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
@@ -88,42 +103,60 @@ jobs:
88103
exit 1
89104
fi
90105
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
92109
with:
93110
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 }}
97112

98113
- name: Setup Environment
99114
uses: ./.github/actions/setup-environment
100115
with:
101-
token: ${{ env.CPLN_TOKEN }}
102-
org: ${{ env.CPLN_ORG }}
116+
token: ${{ secrets.CPLN_TOKEN_STAGING }}
117+
org: ${{ vars.CPLN_ORG }}
103118

104119
- name: Check if Review App Exists
105120
id: check-app
121+
if: github.event_name == 'pull_request'
122+
env:
123+
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
106124
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
107132
if ! cpflow exists -a ${{ env.APP_NAME }}; then
108133
echo "No review app exists for this PR"
109134
exit 0
110135
fi
111-
echo "app_exists=true" >> $GITHUB_OUTPUT
112136
113137
- name: Validate Deployment Request
114138
id: validate
115139
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
140+
if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \
141+
("${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app") || \
142+
"${{ github.event_name }}" == "pull_request" ]]; then
123143
echo "Skipping deployment - not a valid trigger (event: ${{ github.event_name }})"
124144
exit 0
125145
fi
126146
147+
- name: Create Initial Comment
148+
id: create-comment
149+
uses: actions/github-script@v7
150+
with:
151+
script: |
152+
const result = await github.rest.issues.createComment({
153+
owner: context.repo.owner,
154+
repo: context.repo.repo,
155+
issue_number: process.env.PR_NUMBER,
156+
body: '🚀 Starting deployment process...\n\n' + process.env.CONSOLE_LINK
157+
});
158+
core.setOutput('comment-id', result.data.id);
159+
127160
- name: Set Deployment URLs
128161
id: set-urls
129162
uses: actions/github-script@v7
@@ -146,81 +179,6 @@ jobs:
146179
'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
147180
);
148181
149-
- name: Create Initial Comment
150-
id: create-comment
151-
uses: actions/github-script@v7
152-
with:
153-
script: |
154-
const result = await github.rest.issues.createComment({
155-
owner: context.repo.owner,
156-
repo: context.repo.repo,
157-
issue_number: process.env.PR_NUMBER,
158-
body: '🚀 Deploying Review App...\n\n' + process.env.CONSOLE_LINK
159-
});
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
164-
165-
- name: Initialize Deployment
166-
id: init-deployment
167-
uses: actions/github-script@v7
168-
with:
169-
script: |
170-
async function getWorkflowUrl(runId) {
171-
const jobs = await github.rest.actions.listJobsForWorkflowRun({
172-
owner: context.repo.owner,
173-
repo: context.repo.repo,
174-
run_id: runId
175-
});
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-
});
195-
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-
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
223-
224182
- name: Update Status - Building
225183
uses: actions/github-script@v7
226184
with:
@@ -237,7 +195,7 @@ jobs:
237195
await github.rest.issues.updateComment({
238196
owner: context.repo.owner,
239197
repo: context.repo.repo,
240-
comment_id: process.env.COMMENT_ID,
198+
comment_id: ${{ steps.create-comment.outputs.comment-id }},
241199
body: buildingMessage
242200
});
243201
@@ -269,7 +227,7 @@ jobs:
269227
await github.rest.issues.updateComment({
270228
owner: context.repo.owner,
271229
repo: context.repo.repo,
272-
comment_id: process.env.COMMENT_ID,
230+
comment_id: ${{ steps.create-comment.outputs.comment-id }},
273231
body: deployingMessage
274232
});
275233
@@ -329,6 +287,6 @@ jobs:
329287
await github.rest.issues.updateComment({
330288
owner: context.repo.owner,
331289
repo: context.repo.repo,
332-
comment_id: process.env.COMMENT_ID,
290+
comment_id: ${{ steps.create-comment.outputs.comment-id }},
333291
body: isSuccess ? successMessage : failureMessage
334292
});

.github/workflows/help-command.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
uses: actions/checkout
3232

3333
- name: Process Help Command
34-
uses: shakacode/react-webpack-rails-tutorial/.github/actions/help-command@justin808-more-work-on-review-apps-2
34+
uses: ./.github/actions/help-command
3535
with:
3636
github-token: ${{ secrets.GITHUB_TOKEN }}
3737
issue-number: ${{ github.event.inputs.issue-number }}

0 commit comments

Comments
 (0)