Skip to content

Commit 081e364

Browse files
committed
feat: add autdev pr
1 parent b02465c commit 081e364

File tree

2 files changed

+192
-2
lines changed

2 files changed

+192
-2
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
name: Azure Container Apps PR Deployment
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize, closed]
6+
7+
permissions:
8+
id-token: write
9+
contents: read
10+
pull-requests: write
11+
12+
env:
13+
AZURE_CONTAINER_REGISTRY: dotinc.azurecr.io
14+
AZURE_RESOURCE_GROUP: rg-brease-pr-${{ github.event.number }}
15+
AZURE_LOCATION: westeurope
16+
API_APP_NAME: brease-api-pr-${{ github.event.number }}
17+
CONTAINER_APP_ENVIRONMENT: brease-pr-${{ github.event.number }}-env
18+
19+
jobs:
20+
deploy:
21+
if: github.event.action != 'closed'
22+
runs-on: ubuntu-latest
23+
concurrency:
24+
group: pr-${{ github.event.number }}
25+
cancel-in-progress: true
26+
outputs:
27+
api-url: ${{ steps.get-api-url.outputs.url }}
28+
29+
steps:
30+
- name: Checkout code
31+
uses: actions/checkout@v4
32+
33+
- name: Debug OIDC token
34+
run: |
35+
echo "GitHub context:"
36+
echo "Event: ${{ github.event_name }}"
37+
echo "Ref: ${{ github.ref }}"
38+
echo "SHA: ${{ github.sha }}"
39+
40+
- name: Azure Login
41+
uses: azure/login@v2
42+
with:
43+
client-id: 2bfd5f96-2fa1-44ff-af35-17722c04027f
44+
tenant-id: 760d74d4-e9ad-46f8-bbae-e20bce9596ab
45+
subscription-id: 5533053b-de97-432f-908a-c7018c458532
46+
47+
- name: Set up Docker Buildx
48+
uses: docker/setup-buildx-action@v3
49+
50+
- name: Log in to Azure Container Registry
51+
run: az acr login --name dotinc
52+
53+
- name: Build and push API image
54+
uses: docker/build-push-action@v6
55+
with:
56+
context: ./apps/api
57+
push: true
58+
file: ./apps/api/Dockerfile
59+
tags: ${{ env.AZURE_CONTAINER_REGISTRY }}/brease-api:pr-${{ github.event.number }}
60+
61+
- name: Create resource group
62+
run: |
63+
az group create \
64+
--name ${{ env.AZURE_RESOURCE_GROUP }} \
65+
--location ${{ env.AZURE_LOCATION }}
66+
67+
- name: Create Container App Environment
68+
run: |
69+
az containerapp env create \
70+
--name ${{ env.CONTAINER_APP_ENVIRONMENT }} \
71+
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} \
72+
--location ${{ env.AZURE_LOCATION }}
73+
74+
- name: Deploy API service
75+
run: |
76+
az containerapp create \
77+
--name ${{ env.API_APP_NAME }} \
78+
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} \
79+
--environment ${{ env.CONTAINER_APP_ENVIRONMENT }} \
80+
--image ${{ env.AZURE_CONTAINER_REGISTRY }}/brease-api:pr-${{ github.event.number }} \
81+
--registry-server ${{ env.AZURE_CONTAINER_REGISTRY }} \
82+
--cpu 0.5 \
83+
--memory 1Gi \
84+
--min-replicas 1 \
85+
--max-replicas 2 \
86+
--ingress external \
87+
--target-port 4400 \
88+
--env-vars \
89+
PORT=4400 \
90+
NODE_ENV=staging \
91+
INFISICAL_PROJECT_ID=642ed4939db25595ac7eb9cd \
92+
INFISICAL_ENVIRONMENT=staging \
93+
INFISICAL_CLIENT_ID=cd3cc75e-d3bc-4b42-ba5d-6b68f56afa78 \
94+
INFISICAL_CLIENT_SECRET=b8791a2f3571a3a24c8d938c627f31490427b7466ef7e335782bd1023d145b9a
95+
96+
- name: Get API URL
97+
id: get-api-url
98+
run: |
99+
URL=$(az containerapp show \
100+
--name ${{ env.API_APP_NAME }} \
101+
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} \
102+
--query properties.configuration.ingress.fqdn \
103+
--output tsv)
104+
echo "url=https://$URL" >> $GITHUB_OUTPUT
105+
106+
- name: Comment on PR
107+
uses: marocchino/sticky-pull-request-comment@v2
108+
with:
109+
header: pr-deployment
110+
message: |
111+
## 🚀 Brease PR Environment Deployed Successfully!
112+
113+
**API Service**: ${{ steps.get-api-url.outputs.url }}
114+
**OpenAPI Docs**: ${{ steps.get-api-url.outputs.url }}/docs
115+
**Health Check**: ${{ steps.get-api-url.outputs.url }}/health
116+
117+
**Resources Created:**
118+
- Resource Group: `${{ env.AZURE_RESOURCE_GROUP }}`
119+
- Container App Environment: `${{ env.CONTAINER_APP_ENVIRONMENT }}`
120+
- API Service: `${{ env.API_APP_NAME }}`
121+
122+
**API Testing:**
123+
```bash
124+
# Test the API endpoint
125+
curl ${{ steps.get-api-url.outputs.url }}/health
126+
127+
# View OpenAPI documentation
128+
open ${{ steps.get-api-url.outputs.url }}/docs
129+
```
130+
131+
> 💡 This environment will be automatically cleaned up when the PR is merged or closed.
132+
133+
cleanup:
134+
if: github.event.action == 'closed'
135+
runs-on: ubuntu-latest
136+
steps:
137+
- name: Azure Login
138+
uses: azure/login@v2
139+
with:
140+
client-id: 2bfd5f96-2fa1-44ff-af35-17722c04027f
141+
tenant-id: 760d74d4-e9ad-46f8-bbae-e20bce9596ab
142+
subscription-id: 5533053b-de97-432f-908a-c7018c458532
143+
144+
- name: Delete resource group
145+
run: |
146+
if az group exists --name ${{ env.AZURE_RESOURCE_GROUP }}; then
147+
echo "Deleting resource group: ${{ env.AZURE_RESOURCE_GROUP }}"
148+
az group delete \
149+
--name ${{ env.AZURE_RESOURCE_GROUP }} \
150+
--yes \
151+
--no-wait
152+
else
153+
echo "Resource group ${{ env.AZURE_RESOURCE_GROUP }} does not exist"
154+
fi
155+
156+
- name: Clean up container images
157+
run: |
158+
# Delete PR-specific images from ACR
159+
az acr repository delete \
160+
--name dotinc \
161+
--repository brease-api \
162+
--tag pr-${{ github.event.number }} \
163+
--yes || true
164+
165+
- name: Comment on PR
166+
uses: marocchino/sticky-pull-request-comment@v2
167+
with:
168+
header: pr-deployment
169+
message: |
170+
## 🧹 Brease PR Environment Cleaned Up
171+
172+
All Azure resources for this PR have been deleted:
173+
- Resource Group: `${{ env.AZURE_RESOURCE_GROUP }}`
174+
- Container image: `brease-api:pr-${{ github.event.number }}`
175+
176+
> ✅ Cleanup completed successfully.

apps/api/Dockerfile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@ RUN go build -v -o /brease .
1010

1111

1212
FROM debian:bookworm
13-
# Update package lists and install ca-certificates
13+
# Update package lists and install ca-certificates and Infisical CLI
1414
RUN apt-get clean && \
1515
rm -rf /var/lib/apt/lists/* && \
1616
apt-get update --fix-missing && apt-get install -y \
1717
ca-certificates \
18+
curl \
19+
bash \
20+
&& curl -1sLf 'https://artifacts-cli.infisical.com/setup.deb.sh' | bash \
21+
&& apt-get update && apt-get install -y infisical \
1822
&& rm -rf /var/lib/apt/lists/*
23+
1924
COPY --from=builder /brease /usr/local/bin/
20-
CMD ["brease"]
25+
26+
# Create startup script that fetches secrets and runs the app
27+
RUN echo '#!/bin/bash\n\
28+
set -e\n\
29+
echo "Fetching secrets from Infisical..."\n\
30+
INFISICAL_TOKEN=$(infisical login --method=universal-auth --client-id=$INFISICAL_CLIENT_ID --client-secret=$INFISICAL_CLIENT_SECRET --plain --silent)\n\
31+
infisical run --projectId=$INFISICAL_PROJECT_ID --env=staging brease\n\
32+
' > /usr/local/bin/start.sh && chmod +x /usr/local/bin/start.sh
33+
34+
CMD ["/usr/local/bin/start.sh"]

0 commit comments

Comments
 (0)