Skip to content

Commit 5c8b3ab

Browse files
committed
fixes
1 parent 4744c76 commit 5c8b3ab

File tree

3 files changed

+60
-24
lines changed

3 files changed

+60
-24
lines changed

.github/actions/deploy-to-control-plane/scripts/deploy.sh

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111

1212
set -e
1313

14+
# Validate required environment variables
15+
: "${APP_NAME:?APP_NAME environment variable is required}"
16+
: "${CPLN_ORG:?CPLN_ORG environment variable is required}"
17+
: "${GITHUB_SHA:?GITHUB_SHA environment variable is required}"
18+
19+
# Set deployment timeout (15 minutes)
20+
TIMEOUT=900
21+
1422
TEMP_OUTPUT=$(mktemp)
23+
trap 'rm -f "$TEMP_OUTPUT"' EXIT
1524

1625
# Build the Docker image
1726
echo "🏗️ Building Docker image..."
@@ -22,21 +31,21 @@ fi
2231

2332
# Deploy the application
2433
echo "🚀 Deploying to Control Plane..."
25-
if cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose | tee "$TEMP_OUTPUT"; then
34+
if timeout "$TIMEOUT" cpflow deploy-image -a "$APP_NAME" --run-release-phase --org "$CPLN_ORG" --verbose | tee "$TEMP_OUTPUT"; then
2635
# Extract Rails URL from deployment output
27-
RAILS_URL=$(grep -o 'https://rails-[^[:space:]]*\.cpln\.app' "$TEMP_OUTPUT")
36+
RAILS_URL=$(grep -oP 'https://rails-[^[:space:]]*\.cpln\.app(?=\s|$)' "$TEMP_OUTPUT" | head -n1)
2837
if [ -n "$RAILS_URL" ]; then
29-
echo "rails_url=$RAILS_URL" >> $GITHUB_OUTPUT
38+
echo "rails_url=$RAILS_URL" >> "$GITHUB_OUTPUT"
3039
echo "✅ Deployment successful"
3140
echo "🚀 Rails URL: $RAILS_URL"
3241
else
3342
echo "❌ Failed to extract Rails URL from deployment output"
3443
exit 1
3544
fi
45+
elif [ $? -eq 124 ]; then
46+
echo "❌ Deployment timed out after $TIMEOUT seconds"
47+
exit 1
3648
else
3749
echo "❌ Deployment to Control Plane failed"
3850
exit 1
3951
fi
40-
41-
# Clean up temporary file
42-
rm -f "$TEMP_OUTPUT"

.github/actions/deploy-to-control-plane/scripts/get-commit-sha.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ set -e
1515

1616
if [ -n "$PR_NUMBER" ]; then
1717
# If PR_NUMBER is set, get the PR's head SHA
18-
PR_SHA=$(gh pr view $PR_NUMBER --json headRefOid --jq '.headRefOid')
19-
echo "sha=$PR_SHA" >> $GITHUB_OUTPUT
20-
echo "sha_short=${PR_SHA:0:7}" >> $GITHUB_OUTPUT
18+
if ! PR_SHA=$(gh pr view $PR_NUMBER --json headRefOid --jq '.headRefOid'); then
19+
echo "Failed to get PR head SHA" >&2
20+
exit 1
21+
fi
22+
echo "sha=$PR_SHA" >> "$GITHUB_OUTPUT"
23+
echo "sha_short=${PR_SHA:0:7}" >> "$GITHUB_OUTPUT"
2124
echo "Using PR head commit SHA: ${PR_SHA:0:7}"
2225
else
2326
# For direct branch deployments, use the current commit SHA
24-
CURRENT_SHA=$(git rev-parse HEAD)
25-
echo "sha=$CURRENT_SHA" >> $GITHUB_OUTPUT
26-
echo "sha_short=${CURRENT_SHA:0:7}" >> $GITHUB_OUTPUT
27+
if ! CURRENT_SHA=$(git rev-parse HEAD); then
28+
echo "Failed to get current SHA" >&2
29+
exit 1
30+
fi
31+
echo "sha=$CURRENT_SHA" >> "$GITHUB_OUTPUT"
32+
echo "sha_short=${CURRENT_SHA:0:7}" >> "$GITHUB_OUTPUT"
2733
echo "Using branch commit SHA: ${CURRENT_SHA:0:7}"
2834
fi

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Deploy to Control Plane
22

3-
run-name: ${{ github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request) ? 'Deploying Review App' : format('Deploying {0} to Staging App', github.ref_name) }}
3+
run-name: "${{ github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request) ? 'Deploying Review App' : format('Deploying {0} to Staging App', github.ref_name) }}"
44

55
on:
66
pull_request:
@@ -114,8 +114,18 @@ jobs:
114114
115115
- name: Setup cpflow app
116116
run: |
117+
set -e
118+
119+
# Validate required environment variables
120+
: "${APP_NAME:?APP_NAME environment variable is required}"
121+
117122
if ! cpflow exists -a ${{ env.APP_NAME }} ; then
118-
cpflow setup-app -a ${{ env.APP_NAME }}
123+
echo "🔧 Setting up new application: ${{ env.APP_NAME }}"
124+
if ! cpflow setup-app -a ${{ env.APP_NAME }}; then
125+
echo "❌ Failed to setup application"
126+
exit 1
127+
fi
128+
echo "✅ Application setup complete"
119129
fi
120130
121131
- name: Deploy to Control Plane
@@ -135,9 +145,10 @@ jobs:
135145
with:
136146
script: |
137147
const isSuccess = '${{ job.status }}' === 'success';
138-
const deploymentId = ${{ fromJSON(steps.init-deployment.outputs.result).deploymentId }};
139-
const commentId = ${{ fromJSON(steps.init-deployment.outputs.result).commentId }};
140-
const workflowUrl = ${{ fromJSON(steps.init-deployment.outputs.result).workflowUrl }};
148+
const result = ${{ steps.init-deployment.outputs.result }};
149+
const deploymentId = result.deploymentId;
150+
const commentId = result.commentId;
151+
const workflowUrl = result.workflowUrl;
141152
const railsUrl = '${{ steps.deploy.outputs.rails_url }}';
142153
const prNumber = process.env.PR_NUMBER;
143154
@@ -154,7 +165,12 @@ jobs:
154165
deploymentStatus.environment_url = railsUrl;
155166
}
156167
157-
await github.rest.repos.createDeploymentStatus(deploymentStatus);
168+
try {
169+
await github.rest.repos.createDeploymentStatus(deploymentStatus);
170+
} catch (error) {
171+
console.error('Failed to update deployment status:', error);
172+
throw error;
173+
}
158174
159175
// Update the initial comment
160176
const successMessage = [
@@ -173,9 +189,14 @@ jobs:
173189
'[View Workflow Status](' + workflowUrl + ')'
174190
].join('\n');
175191
176-
await github.rest.issues.updateComment({
177-
owner: context.repo.owner,
178-
repo: context.repo.repo,
179-
comment_id: commentId,
180-
body: isSuccess ? successMessage : failureMessage
181-
});
192+
try {
193+
await github.rest.issues.updateComment({
194+
owner: context.repo.owner,
195+
repo: context.repo.repo,
196+
comment_id: commentId,
197+
body: isSuccess ? successMessage : failureMessage
198+
});
199+
} catch (error) {
200+
console.error('Failed to update comment:', error);
201+
throw error;
202+
}

0 commit comments

Comments
 (0)