2020 PR_NUMBER : ${{ github.event.pull_request.number || github.event.issue.number }}
2121
2222jobs :
23- process-command :
23+ Process-Deployment-Command :
2424 if : |
2525 github.event_name == 'pull_request' ||
2626 (github.event_name == 'issue_comment' &&
2727 (github.event.comment.body == '/deploy-review-app' ||
28- github.event.comment.body == '/delete-app') &&
28+ github.event.comment.body == '/delete-review- app') &&
2929 github.event.issue.pull_request)
3030 runs-on : ubuntu-latest
3131 permissions :
@@ -35,10 +35,19 @@ jobs:
3535 issues : write
3636
3737 steps :
38+ - name : Get PR HEAD Ref
39+ if : github.event_name == 'issue_comment'
40+ id : getRef
41+ run : |
42+ echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
43+ echo "PR_SHA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefOid | jq -r '.headRefOid')" >> $GITHUB_OUTPUT
44+ env :
45+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
46+
3847 - name : Determine Action
3948 id : determine_action
4049 run : |
41- if [[ "${{ github.event.comment.body }}" == "/delete-app" ]]; then
50+ if [[ "${{ github.event.comment.body }}" == "/delete-review- app" ]]; then
4251 echo "action=delete" >> $GITHUB_OUTPUT
4352 else
4453 echo "action=deploy" >> $GITHUB_OUTPUT
8392 script : |
8493 const success = '${{ steps.delete.outcome }}' === 'success';
8594 const message = success
86- ? '✅ App deletion successful '
87- : '❌ App deletion failed';
95+ ? '✅ Review app successfully deleted '
96+ : '❌ Review app deletion failed';
8897
8998 await github.rest.issues.updateComment({
9099 owner: context.repo.owner,
@@ -93,74 +102,46 @@ jobs:
93102 body: message
94103 });
95104
96- # Deploy Steps (existing steps)
97- - name : Get PR HEAD Ref
98- if : steps.determine_action.outputs.action == 'deploy'
99- id : getRef
100- run : |
101- echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
102- env :
103- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
104-
105- - uses : actions/checkout@v4
106- with :
107- fetch-depth : 0
108- ref : ${{ steps.getRef.outputs.PR_REF || github.ref }}
109-
105+ # Deploy Steps
110106 - name : Initialize Deployment
107+ if : steps.determine_action.outputs.action == 'deploy'
111108 id : init-deployment
112109 uses : actions/github-script@v7
113110 with :
114111 script : |
115- // Helper functions
112+ const prNumber = process.env.PR_NUMBER;
116113 const getWorkflowUrl = (runId) =>
117114 `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
118-
119- const getJobUrl = (runId, jobId, prNumber) =>
120- `${getWorkflowUrl(runId)}/job/${jobId}?pr=${prNumber}`;
121115
122- // Get PR number consistently
123- const prNumber = process.env.PR_NUMBER ;
116+ const getJobUrl = (runId, jobId, prNumber) =>
117+ `${ process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}?pr=${prNumber}` ;
124118
125- // Create GitHub deployment
119+ // Create deployment
126120 const deployment = await github.rest.repos.createDeployment({
127121 owner: context.repo.owner,
128122 repo: context.repo.repo,
129123 ref: context.sha,
130- environment: 'review-app' ,
124+ environment: 'review-' + prNumber ,
131125 auto_merge: false,
132126 required_contexts: []
133127 });
134128
135- // Get the job ID
129+ // Get job URL
136130 const jobs = await github.rest.actions.listJobsForWorkflowRun({
137131 owner: context.repo.owner,
138132 repo: context.repo.repo,
139133 run_id: context.runId
140134 });
141135
142- const jobId = jobs.data.jobs.find(job => job.name === 'process-command ')?.id;
136+ const jobId = jobs.data.jobs.find(job => job.name === 'Process-Deployment-Command ')?.id;
143137 const jobUrl = getJobUrl(context.runId, jobId, prNumber);
144138
145139 // Create initial comment
146140 const comment = await github.rest.issues.createComment({
147141 issue_number: prNumber,
148142 owner: context.repo.owner,
149143 repo: context.repo.repo,
150- body: [
151- '🚀 Starting deployment for PR ' + prNumber,
152- '',
153- '[View Deployment Log](' + jobUrl + ')'
154- ].join('\n')
155- });
156-
157- // Set deployment status to in_progress
158- await github.rest.repos.createDeploymentStatus({
159- owner: context.repo.owner,
160- repo: context.repo.repo,
161- deployment_id: deployment.data.id,
162- state: 'in_progress',
163- description: 'Deployment is in progress'
144+ body: '🚀 Deploying to Control Plane...\n\n[View Deployment Progress](' + jobUrl + ')'
164145 });
165146
166147 return {
@@ -169,35 +150,23 @@ jobs:
169150 workflowUrl: getWorkflowUrl(context.runId)
170151 };
171152
172- - name : Setup cpflow app
173- run : |
174- set -e
175-
176- # Validate required environment variables
177- : "${APP_NAME:?APP_NAME environment variable is required}"
178-
179- if ! cpflow exists -a ${{ env.APP_NAME }} ; then
180- echo "🔧 Setting up new application: ${{ env.APP_NAME }}"
181- if ! cpflow setup-app -a ${{ env.APP_NAME }}; then
182- echo "❌ Failed to setup application"
183- exit 1
184- fi
185- echo "✅ Application setup complete"
186- fi
153+ - name : Build Docker Image
154+ if : steps.determine_action.outputs.action == 'deploy'
155+ uses : ./.github/actions/build-docker-image
156+ with :
157+ github_token : ${{ secrets.GITHUB_TOKEN }}
187158
188159 - name : Deploy to Control Plane
160+ if : steps.determine_action.outputs.action == 'deploy'
189161 id : deploy
190162 uses : ./.github/actions/deploy-to-control-plane
191- env :
192- CPLN_TOKEN : ${{ secrets.CPLN_TOKEN_STAGING }}
193- CPLN_ORG : ${{ secrets.CPLN_ORG_STAGING }}
194163 with :
195164 app_name : ${{ env.APP_NAME }}
196165 org : ${{ env.CPLN_ORG }}
197166 github_token : ${{ secrets.GITHUB_TOKEN }}
198167
199168 - name : Update Status
200- if : always()
169+ if : steps.determine_action.outputs.action == 'deploy'
201170 uses : actions/github-script@v7
202171 with :
203172 script : |
@@ -208,6 +177,7 @@ jobs:
208177 const workflowUrl = result.workflowUrl;
209178 const railsUrl = '${{ steps.deploy.outputs.rails_url }}';
210179 const prNumber = process.env.PR_NUMBER;
180+ const commitSha = '${{ github.event.comment.body && steps.getRef.outputs.PR_SHA || github.sha }}';
211181
212182 // Update deployment status
213183 const deploymentStatus = {
@@ -231,17 +201,17 @@ jobs:
231201
232202 // Update the initial comment
233203 const successMessage = [
234- '✅ Deployment Successful for PR ' + prNumber ,
204+ '🚀 Rails App Deployed to Control Plane for Commit ' + commitSha.substring(0, 7) ,
235205 '',
236- '🚀 Rails App: [' + railsUrl + '](' + railsUrl + ')',
206+ '🌐 Rails App: [' + railsUrl + '](' + railsUrl + ')',
237207 '',
238208 '[View Workflow Status](' + workflowUrl + ')'
239209 ].join('\n');
240210
241211 const failureMessage = [
242212 '❌ Deployment failed for PR ' + prNumber,
243213 '',
244- 'Commit: ' + context.sha .substring(0, 7),
214+ 'Commit: ' + commitSha .substring(0, 7),
245215 '',
246216 '[View Workflow Status](' + workflowUrl + ')'
247217 ].join('\n');
0 commit comments