@@ -46,14 +46,21 @@ jobs:
4646 issues : write
4747
4848 steps :
49+ - name : Checkout code
50+ uses : actions/checkout@v4
51+ with :
52+ fetch-depth : 0
53+ ref : ${{ github.event_name == 'push' && github.ref || github.event_name == 'pull_request' && github.event.pull_request.head.ref }}
54+
4955 - name : Get PR HEAD Ref
5056 id : getRef
5157 env :
5258 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
59+ CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
60+ CPLN_ORG : ${{ vars.CPLN_ORG }}
5361 run : |
54- # Set PR number based on event type
5562 if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
56- PR_NUMBER="${{ github.event.inputs.pr_number }}"
63+ PR_NUMBER="${{ github.event.inputs.pr }}"
5764 elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then
5865 PR_NUMBER="${{ github.event.issue.number }}"
5966 elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
@@ -68,18 +75,18 @@ jobs:
6875 exit 1
6976 fi
7077 fi
71-
78+
7279 if [[ -z "$PR_NUMBER" ]]; then
7380 echo "Error: Could not determine PR number"
7481 exit 1
7582 fi
76-
83+
7784 # Set environment variables
7885 echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
7986 echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-$PR_NUMBER" >> $GITHUB_ENV
8087
8188 # Get PR data using GitHub CLI
82- PR_DATA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName,headRefOid)
89+ PR_DATA=$(gh pr view $PR_NUMBER --repo shakacode/react-webpack-rails-tutorial --json headRefName,headRefOid)
8390 if [[ $? -eq 0 ]]; then
8491 echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
8592 echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
8895 exit 1
8996 fi
9097
91- - uses : actions/checkout@v4
92- with :
93- 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) }}
97-
9898 - name : Setup Environment
9999 uses : ./.github/actions/setup-environment
100100 with :
@@ -104,26 +104,37 @@ jobs:
104104 - name : Check if Review App Exists
105105 id : check-app
106106 run : |
107- if ! cpflow exists -a ${{ env.APP_NAME }}; then
108- echo "No review app exists for this PR"
109- exit 0
107+ echo "Event type: ${{ github.event_name }}"
108+ if [[ "${{ github.event_name }}" == "push" ]]; then
109+ if ! cpflow exists -a ${{ env.APP_NAME }}; then
110+ echo "No review app exists for this PR"
111+ exit 0
112+ fi
110113 fi
111- echo "app_exists=true" >> $GITHUB_OUTPUT
112114
113115 - name : Validate Deployment Request
114116 id : validate
115117 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
118+ if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \
119+ ("${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == "/deploy-review-app") || \
120+ "${{ github.event_name }}" == "push" ]]; then
123121 echo "Skipping deployment - not a valid trigger (event: ${{ github.event_name }})"
124122 exit 0
125123 fi
126124
125+ - name : Create Initial Comment
126+ id : create-comment
127+ uses : actions/github-script@v7
128+ with :
129+ script : |
130+ const result = await github.rest.issues.createComment({
131+ owner: context.repo.owner,
132+ repo: context.repo.repo,
133+ issue_number: process.env.PR_NUMBER,
134+ body: '🚀 Starting deployment process...\n\n' + process.env.CONSOLE_LINK
135+ });
136+ core.setOutput('comment-id', result.data.id);
137+
127138 - name : Set Deployment URLs
128139 id : set-urls
129140 uses : actions/github-script@v7
@@ -146,81 +157,6 @@ jobs:
146157 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)'
147158 );
148159
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-
224160 - name : Update Status - Building
225161 uses : actions/github-script@v7
226162 with :
@@ -237,7 +173,7 @@ jobs:
237173 await github.rest.issues.updateComment({
238174 owner: context.repo.owner,
239175 repo: context.repo.repo,
240- comment_id: process.env.COMMENT_ID ,
176+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
241177 body: buildingMessage
242178 });
243179
@@ -269,7 +205,7 @@ jobs:
269205 await github.rest.issues.updateComment({
270206 owner: context.repo.owner,
271207 repo: context.repo.repo,
272- comment_id: process.env.COMMENT_ID ,
208+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
273209 body: deployingMessage
274210 });
275211
@@ -329,6 +265,6 @@ jobs:
329265 await github.rest.issues.updateComment({
330266 owner: context.repo.owner,
331267 repo: context.repo.repo,
332- comment_id: process.env.COMMENT_ID ,
268+ comment_id: ${{ steps.create-comment.outputs.comment-id }} ,
333269 body: isSuccess ? successMessage : failureMessage
334270 });
0 commit comments