-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-pipelines-1.yml
98 lines (89 loc) · 3.6 KB
/
azure-pipelines-1.yml
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
trigger:
- main
resources:
- repo: self
variables:
imageRepository: 'statswales-frontend'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
vmImageName: 'ubuntu-latest'
# Reference these securely from the Azure DevOps pipeline or variable group
container_registry: '$(containerRegistryFromAzureDevOps)'
acr_name: '$(acrNameFromAzureDevOps)'
resourceGroup: '$(resourceGroupFromAzureDevOps)'
azureSubscription: 'StatsWales-Dev-Test'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
# Step 1: Login to Azure Container Registry
- task: AzureCLI@2
displayName: Login to Azure Container Registry
inputs:
azureSubscription: '$(azureSubscription)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az acr login --name $(acr_name)
# Step 2: Build and push an image to container registry
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
containerRegistry: 'StatsWales-ACR'
repository: '$(imageRepository)'
command: 'buildAndPush'
Dockerfile: '$(dockerfilePath)'
tags: '$(tag)'
# Step 3: Deploy with AzureContainerApps task
- task: AzureContainerApps@1
displayName: 'Deploy to Azure Container Apps'
inputs:
azureSubscription: '$(azureSubscription)'
acrName: '$(acr_name)'
resourceGroup: '$(resourceGroup)'
containerAppName: 'statswales-develop-frontend'
containerAppEnvironment: 'statswales-container-app-environment'
location: 'UK West'
targetPort: '3000'
ingress: 'external'
imageToDeploy: '$(container_registry)/$(imageRepository):$(tag)'
environmentVariables: >
APP_ENV=$(app_env)
BACKEND_URL=$(backend_url)
BACKEND_PORT=$(backend_port)
FRONTEND_URL=$(frontend_url)
FRONTEND_PORT=$(frontend_port)
SESSION_SECRET=$(session_secret)
JWT_SECRET=$(jwt_secret)
REDIS_URL=$(redis_url)
REDIS_ACCESS_KEY=$(redis_access_key)
# Step 4: Health check
- task: Bash@3
displayName: 'Health Check on New Revision'
inputs:
targetType: 'inline'
script: |
new_revision_url=$(az containerapp ingress show --name statswales-develop-frontend --resource-group $(resourceGroup) --location "UK West" --query 'fqdn' -o tsv)
status=$(curl -s -o /dev/null -w "%{http_code}" $new_revision_url)
if [ "$status" -ne 200 ]; then
echo "New app revision is not healthy. Status code: $status"
exit 1
else
echo "New app revision is healthy."
fi
# Step 5: Rollback
- task: AzureCLI@2
displayName: 'Rollback to Previous Stable Revision'
condition: failed()
inputs:
azureSubscription: '$(azureSubscription)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "Rolling back to previous stable revision..."
az containerapp revision list --name statswales-develop-frontend --resource-group $(resourceGroup) --location "UK West" --query "[?properties.active].name" -o tsv | head -n 1 | xargs -I {} az containerapp update --name statswales-develop-frontend --resource-group $(resourceGroup) --location "UK West" --traffic-revisions "{}=100"