diff --git a/azure-pipelines-1.yml b/azure-pipelines-1.yml index c727bb2..c071c6d 100644 --- a/azure-pipelines-1.yml +++ b/azure-pipelines-1.yml @@ -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) \ No newline at end of file + # 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"