-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-pipelines.yml
99 lines (88 loc) · 3.66 KB
/
azure-pipelines.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
99
# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- main
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'StatsWalesRegistry'
imageRepository: 'statswales-frontend'
containerRegistry: 'statswalesregistry.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
# Resource Group
resourceGroup: 'statswales-container-app-rg'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
# Step 1: Build and push an image to container registry
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
containerRegistry: '$(dockerRegistryServiceConnection)'
repository: '$(imageRepository)'
command: 'buildAndPush'
Dockerfile: '**/Dockerfile'
tags: '$(tag)'
# Step 2: Deploy with AzureContainerApps task
- task: AzureContainerApps@1
displayName: 'Deploy to Azure Container Apps'
inputs:
azureSubscription: 'Marvell Consulting(3f7a282e-96dc-4706-9b9e-65609c29b2c8)'
acrName: 'StatsWalesRegistry'
acrUsername: 'StatsWalesRegistry'
acrPassword: '$(acr_password)'
dockerfilePath: 'Dockerfile'
resourceGroup: '$(resourceGroup)'
containerAppName: 'statswales-develop-frontend'
containerAppEnvironment: 'statswales-container-app-environment'
targetPort: '3000'
ingress: 'external'
imageToDeploy: '$(containerRegistry)/$(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 3: Check deployment success and health check
- task: Bash@3
displayName: 'Health Check on New Revision'
inputs:
targetType: 'inline'
script: |
# Fetch the new revision URL
new_revision_url=$(az containerapp ingress show --name statswales-develop-frontend --resource-group $(resourceGroup) --query 'fqdn' -o tsv)
# Perform a health check (check if the app returns HTTP 200)
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 # Fail the build if health check fails
else
echo "New app revision is healthy."
fi
# Step 4: Rollback to previous revision if needed
- task: AzureCLI@2
displayName: 'Rollback to Previous Stable Revision'
condition: failed() # Run this rollback step only if the previous step fails
inputs:
azureSubscription: 'Marvell Consulting(3f7a282e-96dc-4706-9b9e-65609c29b2c8)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "Rolling back to previous stable revision..."
az containerapp revision list --name statswales-develop-frontend --resource-group $(resourceGroup) --query "[?properties.active].name" -o tsv | head -n 1 | xargs -I {} az containerapp update --name statswales-develop-frontend --resource-group $(resourceGroup) --traffic-revisions "{}=100"