Skip to content

Instantly share code, notes, and snippets.

@lukemurraynz
Created November 22, 2023 05:15
Show Gist options
  • Save lukemurraynz/8e43d88c1fb005491f3d597dde841c19 to your computer and use it in GitHub Desktop.
Save lukemurraynz/8e43d88c1fb005491f3d597dde841c19 to your computer and use it in GitHub Desktop.
Azure Image Builder
# Define the pipeline name and trigger
name: Azure Image Builder - Build and Publish Image Template
trigger:
- main
# Define pipeline variables
variables:
serviceconnection: azserviceconnections
overwrite: false
# Define the VM image for the pipeline
pool:
vmImage: ubuntu-latest
# Define the stages of the pipeline
stages:
# First stage: Deploy Azure Storage Account
- stage: ImageBuilderDeploy
jobs:
- deployment: Bicepstgaccount
displayName: 'Deploy Azure Storage Account to Azure for Apps'
environment: 'AzureDeployment'
strategy:
runOnce:
deploy:
steps:
- checkout: self
# Deploy the Bicep template for the Azure Storage account
- task: AzureCLI@2
displayName: 'Deploy Bicep - Azure Storage account and IaC App Container'
inputs:
azureSubscription: $(serviceconnection) # replace with your service connection name
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
az group create --name $(ResourceGroupName) --location $(location)
az deployment group create `
--template-file $(Build.SourcesDirectory)/iac/storageaccount.bicep `
--resource-group $(resourceGroupName) `
--parameters location=$(location) stgaccountname=$(storageaccountname) publicaccess=true
# Copy the app install files to the Azure Storage account
- task: AzureCLI@2
displayName: 'Copy App install files to Azure Storage Account'
inputs:
azureSubscription: $(serviceconnection)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az storage blob upload-batch -d 'iac' --account-name $(storageaccountname) -s $(Build.SourcesDirectory)/apps --type block --overwrite $(overwrite) --verbose
blobs=$(az storage blob list --account-name $(storageaccountname) --container-name 'iac' --query '[].{name:name, url:properties.url}' -o tsv)
echo $blobs
# Second job: Deploy Azure Image Builder Infrastructure
- job: ImageBuilderDeployment
dependsOn: Bicepstgaccount
displayName: 'Deploy Azure Image Builder Infrastructure'
steps:
# Build the Azure Image Builder template
- task: AzureCLI@2
displayName: ' Build Azure Image Builder Template'
inputs:
azureSubscription: $(serviceconnection) # replace with your service connection name
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
az group create --name $(ResourceGroupName) --location $(location)
az deployment group create `
--template-file $(Build.SourcesDirectory)/iac/main.bicep `
--resource-group $(resourceGroupName) `
--parameters location=$(location) imagetemplatename=$(imagetemplatename) stgaccountname=$(storageaccountname) # Add more parameters as needed
# Second stage: Run Azure Image Builder Template Build
- stage: ImageBuilderRun
jobs:
- job: ImageBuilderRun
displayName: 'Run Azure Image Builder Template Build'
steps:
# Run the Azure Image Builder
- task: AzureCLI@2
displayName: 'Run Azure Image Builder'
inputs:
azureSubscription: $(serviceconnection) # replace with your service connection name
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
az image builder run -n $(imagetemplatename) -g $(resourceGroupName) --no-wait --verbose
az image builder wait -n $(imagetemplatename) -g $(resourceGroupName) --custom "lastRunStatus.runState!='Running'" --verbose
- task: AzureCLI@2
displayName: 'Deploy Bicep - Set Azure Storage account public access to false'
inputs:
azureSubscription: $(serviceconnection) # replace with your service connection name
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
az group create --name $(ResourceGroupName) --location $(location)
az deployment group create `
--template-file $(Build.SourcesDirectory)/iac/storageaccount.bicep `
--resource-group $(resourceGroupName) `
--parameters location=$(location) stgaccountname=$(storageaccountname) publicaccess=false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment