Yaml 构建脚本 - 运行 git 分支的任务
Yaml Build Scripts - Run Tasks by git branch
我有一个构建脚本,我想 运行 master
、features/*
和 releases/*
上的一些代码构建步骤,然后是一些发布功能 只有 在 master
和 releases/*
分支上。
我似乎找不到任何用于按分支划分构建脚本的文档。这是我的构建脚本的粗略概述(只是任务,为简洁起见省略了参数)
trigger:
- master
- releases/*
- features/*
pool:
vmImage: 'windows-latest'
name: $(Major).$(Minor).$(rev:r)
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Major: 1
Minor: 1
steps:
- task: NuGetCommand@2
- task: VSBuild@1
- task: VSTest@2
## I would like everything below this line to only be run on releases/* or master
- task: WhiteSource Bolt@20
- task: NuGetCommand@2
- task: PublishBuildArtifacts@1
下半场需要有条件地运行,并且只有上半场成功。我愿意将它们分成两个脚本,这样更容易。
您可以使用 custom condition 检查分支名称:
and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
在 YAML 中添加步骤:
condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
您可以在管道中使用 - ${{if...}}:
脚本块。请参阅以下示例:
steps:
- powershell: echo "task 0"
displayName: task 0
- ${{ if or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/release/')) }}:
- powershell: echo "task 1"
displayName: task 1
- powershell: echo "task 2"
displayName: task 2
注意:Yaml 管道可能会在 - ${{if...}}
脚本块上方用红色波浪线突出显示。但是在你保存管道后它会消失。 缩进非常重要。
在上面的示例中,如果 - ${{if...}}
脚本块的计算结果为真,则将执行任务 1 和任务 2。请参阅上面的 yaml 示例的以下屏幕截图:
请参阅 here for more information about - ${{if...}}
script block. And here 了解可在 azure yaml 管道中使用的表达式。
另一种解决方法是使用 Shayki 提到的 conditions。但是我使用表达式 startsWith
来检查分支 - releases/*
。请参阅以下示例:
steps:
- powershell: echo "task 0"
displayName: task 0
- powershell: echo "task 1"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 1
- powershell: echo "task 2"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 2
我有一个构建脚本,我想 运行 master
、features/*
和 releases/*
上的一些代码构建步骤,然后是一些发布功能 只有 在 master
和 releases/*
分支上。
我似乎找不到任何用于按分支划分构建脚本的文档。这是我的构建脚本的粗略概述(只是任务,为简洁起见省略了参数)
trigger:
- master
- releases/*
- features/*
pool:
vmImage: 'windows-latest'
name: $(Major).$(Minor).$(rev:r)
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Major: 1
Minor: 1
steps:
- task: NuGetCommand@2
- task: VSBuild@1
- task: VSTest@2
## I would like everything below this line to only be run on releases/* or master
- task: WhiteSource Bolt@20
- task: NuGetCommand@2
- task: PublishBuildArtifacts@1
下半场需要有条件地运行,并且只有上半场成功。我愿意将它们分成两个脚本,这样更容易。
您可以使用 custom condition 检查分支名称:
and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
在 YAML 中添加步骤:
condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
您可以在管道中使用 - ${{if...}}:
脚本块。请参阅以下示例:
steps:
- powershell: echo "task 0"
displayName: task 0
- ${{ if or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/release/')) }}:
- powershell: echo "task 1"
displayName: task 1
- powershell: echo "task 2"
displayName: task 2
注意:Yaml 管道可能会在 - ${{if...}}
脚本块上方用红色波浪线突出显示。但是在你保存管道后它会消失。 缩进非常重要。
在上面的示例中,如果 - ${{if...}}
脚本块的计算结果为真,则将执行任务 1 和任务 2。请参阅上面的 yaml 示例的以下屏幕截图:
请参阅 here for more information about - ${{if...}}
script block. And here 了解可在 azure yaml 管道中使用的表达式。
另一种解决方法是使用 Shayki 提到的 conditions。但是我使用表达式 startsWith
来检查分支 - releases/*
。请参阅以下示例:
steps:
- powershell: echo "task 0"
displayName: task 0
- powershell: echo "task 1"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 1
- powershell: echo "task 2"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 2