-
Notifications
You must be signed in to change notification settings - Fork 1
138 lines (117 loc) · 3.94 KB
/
Copy pathdeploy.yml
File metadata and controls
138 lines (117 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
name: Deploy to Azure
on:
push:
branches: [main]
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: true
default: 'dev'
type: choice
options:
- dev
- staging
- prod
env:
AZURE_CONTAINER_REGISTRY: ${{ secrets.AZURE_CONTAINER_REGISTRY }}
RESOURCE_GROUP: ${{ secrets.AZURE_RESOURCE_GROUP }}
CONTAINER_APP_NAME: ${{ secrets.AZURE_CONTAINER_APP_NAME }}
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Azure Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.AZURE_CONTAINER_REGISTRY }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.AZURE_CONTAINER_REGISTRY }}/buildflow-api
tags: |
type=sha,prefix=
type=ref,event=branch
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
- name: Build and push API image
uses: docker/build-push-action@v5
with:
context: ./backend
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy to Azure Container Apps
runs-on: ubuntu-latest
needs: build-and-push
environment:
name: ${{ github.event.inputs.environment || 'dev' }}
url: ${{ steps.deploy.outputs.url }}
steps:
- uses: actions/checkout@v4
- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy to Container Apps
id: deploy
uses: azure/container-apps-deploy-action@v1
with:
resourceGroup: ${{ env.RESOURCE_GROUP }}
containerAppName: ${{ env.CONTAINER_APP_NAME }}
imageToDeploy: ${{ env.AZURE_CONTAINER_REGISTRY }}/buildflow-api:${{ github.sha }}
- name: Output deployment URL
run: |
echo "🚀 Deployed to: ${{ steps.deploy.outputs.url }}"
echo "📦 Image: ${{ env.AZURE_CONTAINER_REGISTRY }}/buildflow-api:${{ github.sha }}"
health-check:
name: Health Check
runs-on: ubuntu-latest
needs: deploy
steps:
- name: Wait for deployment to stabilize
run: sleep 30
- name: Health check
run: |
HEALTH_URL="${{ needs.deploy.outputs.url }}/health"
echo "Checking health at: $HEALTH_URL"
for i in {1..5}; do
response=$(curl -s -o /dev/null -w "%{http_code}" "$HEALTH_URL" || echo "000")
if [ "$response" = "200" ]; then
echo "✅ Health check passed!"
exit 0
fi
echo "Attempt $i: Got response $response, retrying in 10s..."
sleep 10
done
echo "❌ Health check failed after 5 attempts"
exit 1
notify:
name: Notify on Completion
runs-on: ubuntu-latest
needs: [deploy, health-check]
if: always()
steps:
- name: Notify Success
if: ${{ needs.health-check.result == 'success' }}
run: |
echo "🎉 Deployment successful!"
echo "Environment: ${{ github.event.inputs.environment || 'dev' }}"
echo "Commit: ${{ github.sha }}"
- name: Notify Failure
if: ${{ needs.health-check.result != 'success' }}
run: |
echo "❌ Deployment failed or health check did not pass"
echo "Please check the workflow logs for details"
exit 1