Mappia on Azure Pipelines
This document describes how to utilize Azure Pipelines to automatically build and deploy a Magento 2 store on Kubernetes with Mappia and Terraform.
yaml
name: Mappia Magento CI Build
trigger:
- develop
pool:
vmImage: ubuntu-latest
variables:
repository_cli: 'magento2-cli'
repository_fpm: 'magento2-fpm'
registry_name: 'YOUR_REGISTRY.azurecr.io'
containerRegistryServiceConnection: YOUR_SERVICE_CONNECTION
DOCKER_BUILDKIT: 1
stages:
- stage: build
jobs:
- job: build_docker_image
displayName: Build Docker Images
steps:
- task: Docker@2
displayName: 'Build CLI Docker'
inputs:
buildContext: $(System.DefaultWorkingDirectory)
command: 'build'
containerRegistry: $(containerRegistryServiceConnection)
repository: '$(repository_cli)'
dockerfile: .docker/Dockerfile
tags: latest
arguments: --secret id=composer.auth,src=auth.json --cache-from $(registry_name)/$(repository_cli):latest --build-arg BUILDKIT_INLINE_CACHE=1
- task: Docker@2
displayName: 'Build FPM Docker'
inputs:
buildContext: $(System.DefaultWorkingDirectory)
command: 'build'
containerRegistry: $(containerRegistryServiceConnection)
repository: '$(repository_fpm)'
dockerfile: .docker/Dockerfile.fpm
tags: latest
arguments: --build-arg BUILDER_IMAGE=$(registry_name)/$(repository_cli) --cache-from $(registry_name)/$(repository_cli):latest --build-arg BUILDKIT_INLINE_CACHE=1
- task: Docker@2
displayName: 'Publish CLI Docker Image'
inputs:
command: 'push'
containerRegistry: $(containerRegistryServiceConnection)
repository: '$(repository_cli)'
tags: latest
condition: |
and(
succeeded(),
eq(variables['build.sourceBranch'], 'refs/heads/develop')
)
- task: Docker@2
displayName: 'Publish FPM Docker Image'
inputs:
command: 'push'
containerRegistry: $(containerRegistryServiceConnection)
repository: '$(repository_fpm)'
tags: latest
condition: |
and(
succeeded(),
eq(variables['build.sourceBranch'], 'refs/heads/develop')
)
- stage: deploy_dev
dependsOn: build
jobs:
- deployment: deploy
displayName: Terraform Mappia Magento Magento 2 Environment
environment: YOUR_ENVIRONMENT
strategy:
runOnce:
deploy:
steps:
- checkout: self
- script: |
echo $sourceVersion
commitHash=${sourceVersion:0:7}
echo $commitHash
echo "##vso[task.setvariable variable=commitHash]$commitHash"
env: { sourceVersion: $(Build.SourceVersion) }
displayName: Compute Git Short Hash
- script: terraform init -input=false
displayName: Initialize Terraform
workingDirectory: $(System.DefaultWorkingDirectory)/.terraform
env:
ARM_CLIENT_SECRET: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_SECRET)
ARM_CLIENT_ID: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_ID)
ARM_TENANT_ID: $(AZURE_TENANT)
ARM_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION)
TF_VAR_mappia_helm_pwd: $(MAPPIA_HELM_PASSWORD)
TF_VAR_mappia_helm_user: $(MAPPIA_HELM_USER)
TF_VAR_mappia_sp_id: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_ID)
TF_VAR_mappia_sp_object_id: $(TERRAFORM_SERVICE_PRINCIPAL_OBJECT_ID)
TF_VAR_mappia_sp_password: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_SECRET)
TF_VAR_mappia_sp_tenant_id: $(AZURE_TENANT)
TF_VAR_mappia_subscription_id: $(AZURE_SUBSCRIPTION)
- script: |
terraform apply --auto-approve -input=false -var 'helm_mappia_set_values={"githash": "$(commitHash)"}'
displayName: Terraform Cluster
workingDirectory: $(System.DefaultWorkingDirectory)/.terraform
env:
ARM_CLIENT_SECRET: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_SECRET)
ARM_CLIENT_ID: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_ID)
ARM_TENANT_ID: $(AZURE_TENANT)
ARM_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION)
TF_VAR_mappia_helm_pwd: $(MAPPIA_HELM_PASSWORD)
TF_VAR_mappia_helm_user: $(MAPPIA_HELM_USER)
TF_VAR_mappia_sp_id: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_ID)
TF_VAR_mappia_sp_object_id: $(TERRAFORM_SERVICE_PRINCIPAL_OBJECT_ID)
TF_VAR_mappia_sp_password: $(TERRAFORM_SERVICE_PRINCIPAL_CLIENT_SECRET)
TF_VAR_mappia_sp_tenant_id: $(AZURE_TENANT)
TF_VAR_mappia_subscription_id: $(AZURE_SUBSCRIPTION)