如何根据 Azure 中提交的文件夹路径在 Yaml 中设置作业级别条件
How to set Job level condition in Yaml based on committed folder path in Azure
我想根据作业级别的提交路径设置条件?在我的管道中,如果某个文件夹已提交,我只想 运行 一个特定的作业。
我已经在我的管道触发器中定义了一个路径过滤器
trigger:
branches:
include:
- DevopsTest
paths:
include:
- Test/*
- Prod/*
基于提交的路径,我想设置一个条件来调用相应的作业以将变量传递到我的脚本中以便进行部署。如果提交的路径是测试 运行 测试作业并跳过另一个,反之亦然。
基于 方法,我对我的 Yaml 进行了更改。
variables:
- group: Test
- group: Prod
stages:
- stage: CICDPolicyDeployment
jobs:
- job: CICDFlow
steps:
- task: PowerShell@2
name: folderupdate
inputs:
targetType: 'inline'
script: |
$files=$(git diff HEAD HEAD~ --name-only)
$temp=$files -split ' '
$count=$temp.Length
echo "Total changed $count files"
For ($i=0; $i -lt $temp.Length; $i++)
{
$name=$temp[$i]
echo "this is $name file"
if ($name -like "Test/*")
{
Write-Host "##vso[task.setvariable variable=TestUpdate]True"
}
}
- job: Test
condition: and(succeeded(), eq(variables['TestUpdate'], 'True'))
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'test'
ScriptType: 'FilePath'
ScriptPath: '.\scripts\pipelinescript.ps1'
ScriptArguments: -NAME $(testtname) -API_KEY $(testapikey) -DEPLOY_KEY $(testdeploykey)
azurePowerShellVersion: 'LatestVersion'
- job: Prod
condition: and(succeeded(), eq(variables['ProdUpdate'], 'True'))
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'prod'
ScriptType: 'FilePath'
ScriptPath: '.\scripts\pipelinescript.ps1'
ScriptArguments: -NAME $(Prodtname) -API_KEY $(Prodapikey) -DEPLOY_KEY $(proddeploykey)
azurePowerShellVersion: 'LatestVersion'
然而,内联脚本中的 If 条件未与提交的路径匹配且未将值设置为 true
有什么地方我做得不对吗?或者我在这里遗漏了什么
这种方法没有开箱即用的解决方案。您需要先扫描更改,然后设置变量并根据它们创建条件。你可以查看一下.
我想根据作业级别的提交路径设置条件?在我的管道中,如果某个文件夹已提交,我只想 运行 一个特定的作业。
我已经在我的管道触发器中定义了一个路径过滤器
trigger:
branches:
include:
- DevopsTest
paths:
include:
- Test/*
- Prod/*
基于提交的路径,我想设置一个条件来调用相应的作业以将变量传递到我的脚本中以便进行部署。如果提交的路径是测试 运行 测试作业并跳过另一个,反之亦然。
基于
variables:
- group: Test
- group: Prod
stages:
- stage: CICDPolicyDeployment
jobs:
- job: CICDFlow
steps:
- task: PowerShell@2
name: folderupdate
inputs:
targetType: 'inline'
script: |
$files=$(git diff HEAD HEAD~ --name-only)
$temp=$files -split ' '
$count=$temp.Length
echo "Total changed $count files"
For ($i=0; $i -lt $temp.Length; $i++)
{
$name=$temp[$i]
echo "this is $name file"
if ($name -like "Test/*")
{
Write-Host "##vso[task.setvariable variable=TestUpdate]True"
}
}
- job: Test
condition: and(succeeded(), eq(variables['TestUpdate'], 'True'))
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'test'
ScriptType: 'FilePath'
ScriptPath: '.\scripts\pipelinescript.ps1'
ScriptArguments: -NAME $(testtname) -API_KEY $(testapikey) -DEPLOY_KEY $(testdeploykey)
azurePowerShellVersion: 'LatestVersion'
- job: Prod
condition: and(succeeded(), eq(variables['ProdUpdate'], 'True'))
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'prod'
ScriptType: 'FilePath'
ScriptPath: '.\scripts\pipelinescript.ps1'
ScriptArguments: -NAME $(Prodtname) -API_KEY $(Prodapikey) -DEPLOY_KEY $(proddeploykey)
azurePowerShellVersion: 'LatestVersion'
然而,内联脚本中的 If 条件未与提交的路径匹配且未将值设置为 true
有什么地方我做得不对吗?或者我在这里遗漏了什么
这种方法没有开箱即用的解决方案。您需要先扫描更改,然后设置变量并根据它们创建条件。你可以查看一下