Skip to content

Commit

Permalink
Merge pull request #28 from Marvell-Consulting/feature/new-pipeline
Browse files Browse the repository at this point in the history
Update devops pipeline to run in new RG
  • Loading branch information
easternbloc authored Oct 22, 2024
2 parents 6784c0d + db97026 commit 56d0707
Showing 1 changed file with 85 additions and 52 deletions.
137 changes: 85 additions & 52 deletions azure-pipelines-1.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,98 @@
trigger:
- main
- main

resources:
- repo: self
- repo: self

variables:
# Container registry service connection established during pipeline creation
imageRepository: 'statswales-frontend'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'

# Agent VM image name
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:
- task: AzureCLI@2
displayName: Login to Azure Container Registry
inputs:
azureSubscription: 'StatsWales-Dev-Test'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az acr login --name $(container_registry)
- 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)
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
containerRegistry: '$(container_registry)'
repository: '$(imageRepository)'
command: 'buildAndPush'
Dockerfile: '$(dockerfilePath)'
tags: '$(tag)'
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
- stage: Deploy
displayName: Deploy stage
jobs:
- job: Deploy
displayName: Deploy to Azure Container App
pool:
vmImage: $(vmImageName)
steps:
- task: AzureCLI@2
displayName: Deploy to Azure Container App
inputs:
azureSubscription: 'StatsWales-Dev-Test'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az containerapp update \
--name statswales-develop-frontend \
--resource-group $(resource_group) \
--environment statswales-container-app-environment \
--image $(container_registry)/$(imageRepository):$(tag) \
--ingress external \
--target-port 3000 \
--environment-variables BACKEND_SERVER=$(backend_server) BACKEND_PORT=$(backend_port) BACKEND_PROTOCOL=$(backend_protocol) GOOGLE_CLIENT_ID=$(client_id) GOOGLE_CLIENT_SECRET=$(client_secret) SESSION_SECRET=$(session_secret)
# 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"

0 comments on commit 56d0707

Please sign in to comment.