luk*_*fer 7 git azure-pipelines azure-repos azure-pipelines-yaml
我有一个包含 Dockerfile 和其他代码的存储库。无论发生什么变化,我们都希望运行某些步骤,但只想在发生变化docker build时运行作业。**/Dockerfile
我已经查看了条件文档和表达式文档,但我不清楚如何(如果可能的话)如何组合这些文档或以其他方式实现所需的结果。
我意识到在bash(例如git rev-list...和git diff --name-only <previous_commit>..HEAD | grep <pattern>)中这是可能的,但这有点麻烦,并且它仍然在 Azure Pipelines 中显示作业已运行,只是短路了。理想情况下,它会(适当地)表明该作业被全部跳过。
我还意识到 Docker 部分和代码部分可以位于具有单独构建触发器的单独存储库中,但如果可能的话,希望将它们放在同一个存储库中。
抱歉,每个作业都没有触发器。触发器适用于管道范围。
根据您的要求,您可以采用此结构作为解决方法:
jobs:
- job: OtherSteps
steps:
Your other steps in this job.
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$changedfiles = git diff ... (Choose right git command depending on your needs.)
Write-Host $changedfiles
If ($changedfiles.Contains("Dockerfile")) {
echo "##vso[task.setvariable variable=IfRunDockerBuild;isOutput=true]run"
}
name: 'DetermineIfRunNextJob'
- job: DockerBuild
dependsOn: OtherSteps
condition: eq(dependencies.OtherSteps.outputs['DetermineIfRunNextJob.IfRunDockerBuild'],'run')
steps:
- script: echo Only run this job when IfRunDockerBuild=run instead of Null!
Run Code Online (Sandbox Code Playgroud)
1.假设你有job1和job2(docker build),你只需要在job1的末尾添加一个像上面这样的PS任务。然后它输出一个变量,该变量决定我们是否需要运行 job2 或者跳过它。
2.Powershell任务可以在Linux、macOS或Windows上运行。
2.这个解决方法的核心来自于这个特性:在后续作业的条件中使用作业的输出变量。
虽然问题很老,但我也遇到了同样的问题,并且我认为我有一个很好的解决方案。面临的挑战是确保解决方案能够正常工作,即使同时推送多个提交,或者构建失败(因此无法部署),或者在 PR 合并(部署仅发生在主分支上)时也是如此。
我已经在此要点中完整描述了我的解决方案:https://gist.github.com/Myrddraal/f5a84cf3242e3b3804fa727005ed2786
它利用管道 API,它可以提供自上次成功执行管道以来的所有提交的列表。这确保了即使一次推送多个提交,或者基础设施更改的构建失败时,它也能正常工作。管道 API 完成了确定哪些提交需要检查的艰苦工作。
逻辑在这个 powershell 中:
[CmdletBinding()]
param (
$authorisation,
$pathFilter,
$collectionUri,
$project,
$buildId
)
$changesUrl = "$collectionUri/$project/_apis/build/builds/$buildId/changes?api-version=6.0"
$changesResponse = Invoke-RestMethod -Uri $changesUrl -Headers @{Authorization = $authorisation } -Method Get
$commits = @($changesResponse.value | ForEach-Object { $_.id })
Write-Host "##vso[task.setvariable variable=filesUpdated;isOutput=true]False"
Write-Host "Checking $($commits.Length) commits for changes matching path $pathFilter"
for ($j = 0; $j -lt $commits.Length; $j++) {
Write-Host "Checking commit: $($commits[$j]) with its parent"
$files = $(git diff "$($commits[$j])~" $commits[$j] --name-only)
Write-Host $files
if ($files -like "*$pathFilter/*") {
Write-Host "Found file matching path filter in commit $($commits[$j])"
Write-Host "##vso[task.setvariable variable=filesUpdated;isOutput=true]True"
break
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下 YAML 调用它(在拉取存储库后的构建作业中):
- task: PowerShell@2
inputs:
filePath: "azure-pipelines/Test-ChangesMadeInPath.ps1"
arguments: >-
-authorisation "Bearer $(system.accesstoken)"
-pathFilter "azure-pipelines/deployment"
-buildId $(Build.BuildId)'
-collectionUri $(System.CollectionUri)
-project $(System.TeamProject)
name: DetermineChangesMadeInDeploymentFolder
env:
SYSTEM_ACCESSTOKEN: $(system.accesstoken)
Run Code Online (Sandbox Code Playgroud)
然后将以下条件添加到您的部署作业中:
- deployment: DeployInfrastructure
condition: eq(stageDependencies.Build.BuildJob.outputs['DetermineChangesMadeInDeploymentFolder.filesUpdated'], 'True')
displayName: Deploy infrastructure
environment: "prod"
strategy:
runOnce:
deploy:
steps:
- template: deployment/YAML/deploy-infrastructure.yml
parameters:
environment: $(Environment.Name)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11653 次 |
| 最近记录: |