Skip to content

Instantly share code, notes, and snippets.

@akanieski
Created March 25, 2023 17:36
Show Gist options
  • Save akanieski/902b7c17d6da50be2ef79f3a02ab5b3d to your computer and use it in GitHub Desktop.
Save akanieski/902b7c17d6da50be2ef79f3a02ab5b3d to your computer and use it in GitHub Desktop.
Azure Pipelines - Run Stages When Certain Files Are Changed
parameters:
- name: searchPattern
type: string
- name: setVariableNamed
type: string
- name: publishAsArtifactNamed
type: string
default: SkipPublishChanges
steps:
- powershell: |
$found = 'No';
# grab all changes files in last commit
$data = git diff --name-only HEAD~ HEAD
$files = $data.Split([System.Environment]::NewLineCharacter)
# filter all files looking for ones that match the searchpattern
$files = $files | where {$_ -like '${{ parameters.searchPattern }}'}
# if there are any files under the given search pattern
if ($files.count -gt 0) {
Write-Host 'Located: '
Write-Host $files
$found = 'Yes';
} else {
Write-Host "No files found matching ${{ parameters.searchPattern }}"
}
# if they indicated they would like to configure a variable with the outcome
if (![string]::IsNullOrEmpty('${{ parameters.setVariableNamed }}')) {
Write-Host "##vso[task.setvariable variable=${{ parameters.setVariableNamed }};isOutput=true]$found"
}
# Copy all changes to a folder to publish them
if (![string]::IsNullOrEmpty('${{parameters.publishAsArtifactNamed}}')) {
New-Item -Type Directory "_${{parameters.publishAsArtifactNamed}}"
$path = "_${{parameters.publishAsArtifactNamed}}" + [IO.Path]::DirectorySeparatorChar
foreach ($i in $files) {
$tgt = $path + $i
Copy-Item "$i" "$tgt"
}
}
name: Changes
displayName: Analyze Changes
- task: PublishBuildArtifacts@1
displayName: Publish Changes
condition: ${{ ne(parameters.publishAsArtifactNamed, 'SkipPublishChanges') }}
inputs:
pathToPublish: '_${{parameters.publishAsArtifactNamed}}'
artifactName: ${{parameters.publishAsArtifactNamed}}
trigger: none
pool:
vmImage: windows-latest
stages:
- stage: AnalyzeStage
jobs:
- job: FirstJob
steps:
- template: analyze-changes.yml
parameters:
searchPattern: '*.sql'
setVariableNamed: 'SqlChangesFound'
publishAsArtifactNamed: 'SqlChanges'
- powershell: |
Write-Host 'Current Value: $(Changes.SqlChangesFound)'
- job: SecondJob
variables:
SqlChangesFound: $[dependencies.FirstJob.outputs['Changes.SqlChangesFound']]
dependsOn: FirstJob
steps:
- powershell: |
Write-Host 'Current Value: $(SqlChangesFound)'
- stage: ConditionalSqlStage
condition: eq(dependencies.AnalyzeStage.outputs['FirstJob.Changes.SqlChangesFound'], 'Yes')
dependsOn: AnalyzeStage
jobs:
- job: SomeSqlWork
variables:
SqlChangesFound: $[stageDependencies.AnalyzeStage.FirstJob.outputs['Changes.SqlChangesFound']]
steps:
- powershell: |
Write-Host 'Value From another stage: $(SqlChangesFound)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment