Created
February 7, 2022 09:00
-
-
Save ehrnst/c4c2f5901a1a85ff53422b74760c7e88 to your computer and use it in GitHub Desktop.
azure pipeline with bicep files push to acr. uses git diff to only push modified modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$version = get-date -Format yyyy-MM-dd | |
$commit = "$ENV:BUILD_SOURCEVERSION" | |
# get the latest files changed | |
$files = $(git diff HEAD HEAD~ --name-only -- *.bicep modules/) | |
if ($files.count -ge 1) { | |
# copy files to staging | |
copy-item $files -Destination $ENV:BUILD_ARTIFACTSTAGINGDIRECTORY | |
# set a variable and build number | |
Write-Host ("##vso[task.setvariable variable=bicepFiles]true") | |
write-host ("##vso[build.updatebuildnumber]$($version)") | |
} | |
else { | |
Write-Host ("##vso[task.setvariable variable=bicepFiles]false") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger: | |
- master | |
name: $(Date:yyyyMMdd)$(Rev:.r) | |
variables: | |
serviceConnectionName: '' | |
stages: | |
- stage: Build | |
jobs: | |
- job: | |
steps: | |
- checkout: self | |
- task: PowerShell@2 | |
displayName: 'Get changed or added files' | |
inputs: | |
targetType: filePath | |
filePath: .scripts/diff.ps1 | |
pwsh: true | |
- task: PublishPipelineArtifact@1 | |
displayName: 'Publish files as artifact' | |
condition: and(succeeded(), eq(variables.bicepFiles, 'true')) | |
inputs: | |
artifact: 'bicep' | |
targetPath: '$(Build.ArtifactStagingDirectory)' | |
publishLocation: pipeline | |
- task: AzureCLI@2 | |
displayName: 'Validate bicep modules' | |
inputs: | |
azureSubscription: $(serviceConnectionName) | |
addSpnToEnvironment: true | |
scriptLocation: scriptPath | |
ScriptType: 'pscore' | |
scriptPath: .scripts/bicepvalidate.ps1 | |
- stage: deploy | |
dependsOn: Build | |
jobs: | |
- deployment: Deploy | |
environment: 'bicep-prod' | |
displayName: 'Deploy bicep files to registry' | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- checkout: self | |
- task: DownloadBuildArtifacts@1 | |
inputs: | |
artifactName: 'bicep' | |
allowPartiallySucceededBuilds: false | |
- task: AzureCLI@2 | |
displayName: 'Deploy bicep files to ACR' | |
inputs: | |
azureSubscription: $(serviceConnectionName) | |
addSpnToEnvironment: true | |
scriptLocation: scriptPath | |
ScriptType: 'pscore' | |
scriptPath: .scripts/publish.ps1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# publish bicep modules to acr | |
$version = get-date -Format yyyy-MM-dd | |
$modules = Get-ChildItem "$env:SYSTEM_ARTIFACTSDIRECTORY" -Include *.bicep -Recurse | |
foreach ($module in $modules) { | |
$name = $module.BaseName | |
bicep publish $module.FullName --target "br:myregistry.azurecr.io/modules/${name}:${version}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment