Skip to content

Commit 01c47a1

Browse files
committed
Fix deploy
1 parent 42ee53f commit 01c47a1

8 files changed

Lines changed: 806 additions & 10 deletions

File tree

.github/workflows/deploy-gcp.yml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: Deploy to Google Cloud Run
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
- 'feature/**'
9+
pull_request:
10+
branches:
11+
- main
12+
13+
env:
14+
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
15+
SERVICE: l0002
16+
REGION: us-central1
17+
18+
jobs:
19+
deploy:
20+
name: Deploy to Cloud Run
21+
runs-on: ubuntu-latest
22+
23+
# Add "id-token" with the intended permissions.
24+
permissions:
25+
contents: 'read'
26+
id-token: 'write'
27+
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
32+
- name: Google Auth
33+
id: auth
34+
uses: google-github-actions/auth@v2
35+
with:
36+
credentials_json: ${{ secrets.GCP_SA_KEY }}
37+
# Or use Workload Identity Federation (recommended)
38+
# workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
39+
# service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
40+
41+
- name: Set up Cloud SDK
42+
uses: google-github-actions/setup-gcloud@v2
43+
44+
- name: Configure Docker to use gcloud as a credential helper
45+
run: |
46+
gcloud auth configure-docker
47+
48+
- name: Set environment name
49+
id: env-name
50+
run: |
51+
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
52+
echo "env_name=production" >> $GITHUB_OUTPUT
53+
echo "service_name=${{ env.SERVICE }}" >> $GITHUB_OUTPUT
54+
elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then
55+
echo "env_name=staging" >> $GITHUB_OUTPUT
56+
echo "service_name=${{ env.SERVICE }}-staging" >> $GITHUB_OUTPUT
57+
else
58+
# For feature branches, create a preview environment
59+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
60+
SAFE_BRANCH_NAME=$(echo $BRANCH_NAME | sed 's/[^a-z0-9-]/-/g' | cut -c1-28)
61+
echo "env_name=preview" >> $GITHUB_OUTPUT
62+
echo "service_name=${{ env.SERVICE }}-$SAFE_BRANCH_NAME" >> $GITHUB_OUTPUT
63+
fi
64+
65+
- name: Build Docker image
66+
run: |
67+
docker build -t gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }} .
68+
docker tag gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }} \
69+
gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ steps.env-name.outputs.env_name }}
70+
71+
- name: Push Docker image
72+
run: |
73+
docker push gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
74+
docker push gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ steps.env-name.outputs.env_name }}
75+
76+
- name: Deploy to Cloud Run
77+
id: deploy
78+
uses: google-github-actions/deploy-cloudrun@v2
79+
with:
80+
service: ${{ steps.env-name.outputs.service_name }}
81+
image: gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
82+
region: ${{ env.REGION }}
83+
flags: |
84+
--port=50002
85+
--allow-unauthenticated
86+
env_vars: |
87+
AUTH_URL=${{ secrets.AUTH_URL || 'https://auth.graffiticode.org' }}
88+
NODE_ENV=${{ steps.env-name.outputs.env_name }}
89+
labels: |
90+
commit-sha=${{ github.sha }}
91+
branch=${{ github.ref_name }}
92+
environment=${{ steps.env-name.outputs.env_name }}
93+
94+
- name: Show deployment URL
95+
run: echo "${{ steps.deploy.outputs.url }}"
96+
97+
- name: Comment on PR with deployment URL
98+
if: github.event_name == 'pull_request'
99+
uses: actions/github-script@v7
100+
with:
101+
script: |
102+
github.rest.issues.createComment({
103+
issue_number: context.issue.number,
104+
owner: context.repo.owner,
105+
repo: context.repo.repo,
106+
body: `🚀 Deployed to ${{ steps.deploy.outputs.url }}`
107+
})
108+
109+
# Run smoke tests
110+
- name: Run smoke test
111+
run: |
112+
sleep 10 # Wait for service to be ready
113+
response=$(curl -s -o /dev/null -w "%{http_code}" "${{ steps.deploy.outputs.url }}/health" || echo "000")
114+
if [ "$response" = "200" ]; then
115+
echo "✅ Health check passed"
116+
else
117+
echo "❌ Health check failed with status $response"
118+
exit 1
119+
fi
120+
121+
cleanup:
122+
name: Cleanup old preview deployments
123+
runs-on: ubuntu-latest
124+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
125+
126+
permissions:
127+
contents: 'read'
128+
id-token: 'write'
129+
130+
steps:
131+
- name: Google Auth
132+
uses: google-github-actions/auth@v2
133+
with:
134+
credentials_json: ${{ secrets.GCP_SA_KEY }}
135+
136+
- name: Set up Cloud SDK
137+
uses: google-github-actions/setup-gcloud@v2
138+
139+
- name: Delete preview service
140+
run: |
141+
BRANCH_NAME=${{ github.head_ref }}
142+
SAFE_BRANCH_NAME=$(echo $BRANCH_NAME | sed 's/[^a-z0-9-]/-/g' | cut -c1-28)
143+
SERVICE_NAME="${{ env.SERVICE }}-$SAFE_BRANCH_NAME"
144+
145+
# Check if service exists before trying to delete
146+
if gcloud run services describe $SERVICE_NAME --region=${{ env.REGION }} 2>/dev/null; then
147+
gcloud run services delete $SERVICE_NAME --region=${{ env.REGION }} --quiet
148+
echo "✅ Deleted preview service: $SERVICE_NAME"
149+
else
150+
echo "ℹ️ Preview service $SERVICE_NAME not found, skipping cleanup"
151+
fi

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ COPY . .
1818
# Build the application
1919
RUN npm run build
2020

21-
EXPOSE 50002
2221
CMD [ "npm", "start" ]

0 commit comments

Comments
 (0)