Azure DevOps Pipelines在错误的分支上运行

36次阅读
没有评论

问题描述

在使用Azure DevOps Pipelines时遇到了一个问题。他希望在不同的分支上使用不同的YAML配置文件构建项目。然而,当他提交到应该只触发一个流水线的分支时,却触发了两个流水线。
Azure DevOps Pipelines在错误的分支上运行
以下是开发分支的YAML配置:

trigger:
- master_Development
pool:
  vmImage: 'windows-latest'
variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  buildPackageDirectory: 'packages'
  nugetConfig: 'SW.Api.Net/nuget.config'
steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.4.1'
  inputs:
    versionSpec: 4.4.1
- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: $(solution)
    feedsToUse: config
    nugetConfigPath: $(nugetConfig)
    externalFeedCredentials: TelerikFeed
    restoreDirectory: $(buildPackageDirectory)
- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    solution: $(solution)
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)\"'
    platform: $(buildPlatform)
    configuration: $(buildConfiguration)
- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testFiltercriteria: 'TestCategory=Unit Test'
    platform: $(buildPlatform)
    configuration: $(buildConfiguration)
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathToPublish: $(build.artifactStagingDirectory)
    ArtifactName: 'drop'
  condition: succeededOrFailed()

生产分支的YAML配置:

trigger:
- master
pool:
  vmImage: 'windows-latest'
variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  buildPackageDirectory: 'packages'
  nugetConfig: 'SW.Api.Net/nuget.config'
steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.4.1'
  inputs:
    versionSpec: 4.4.1
- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: $(solution)
    feedsToUse: config
    nugetConfigPath: $(nugetConfig)
    externalFeedCredentials: TelerikFeed
    restoreDirectory: $(buildPackageDirectory)
- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    solution: $(solution)
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)\"'
    platform: $(buildPlatform)
    configuration: $(buildConfiguration)
- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testFiltercriteria: 'TestCategory=Unit Test'
    platform: $(buildPlatform)
    configuration: $(buildConfiguration)
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathToPublish: $(build.artifactStagingDirectory)
    ArtifactName: 'drop'
  condition: succeededOrFailed()

为什么这两个流水线不会分别触发,它们分别针对不同的分支?

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

问题的原因是流水线的路径设置不正确。为了解决这个问题,你可以在仓库的根目录下创建两个目录,分别命名为pipelines/devpipelines/prod,然后将每个构建流水线指向相应的YAML配置文件。这样,只有在特定分支的提交时,才会触发相应的流水线。
以下是解决方案的步骤:
1. 在仓库的根目录下创建两个目录:pipelines/devpipelines/prod
2. 将开发分支的构建流水线指向pipelines/dev目录下的YAML配置文件。
3. 将生产分支的构建流水线指向pipelines/prod目录下的YAML配置文件。
请注意,这种方法需要确保每个分支的提交都在相应的目录中,并且每个目录中都有正确的YAML配置文件。

方案2

使用脚本或工具来管理流水线的触发可能会增加复杂性,并且需要确保分支和路径的映射关系正确设置。
另一种方法是编写脚本或使用工具来控制流水线的触发。你可以使用Azure DevOps提供的API来手动控制流水线的触发,或者使用一些第三方工具来管理流水线的触发逻辑。

示例:

以下是一个简单的bash脚本示例,可以在提交到特定分支时触发相应的流水线:

#!/bin/bash
# 获取当前分支
current_branch=$(git rev-parse --abbrev-ref HEAD)
# 根据分支触发相应的流水线
if [ "$current_branch" == "dev" ]; then
  # 触发开发分支的流水线
  curl -X POST -u username:password https://dev.azure.com/organization/project/_apis/pipelines/pipeline_id/runs?api-version=6.0-preview.1
elif [ "$current_branch" == "prod" ]; then
  # 触发生产分支的流水线
  curl -X POST -u username:password https://dev.azure.com/organization/project/_apis/pipelines/pipeline_id/runs?api-version=6.0-preview.1
else
  echo "No pipeline configured for the current branch."
fi

在这个示例中,我们首先使用git rev-parse --abbrev-ref HEAD命令获取当前分支的名称。然后,根据分支的名称,使用curl命令触发相应的流水线。你需要将usernamepasswordorganizationprojectpipeline_id替换为你自己的值。
请注意,这种方法需要确保脚本中的分支和流水线的映射关系正确设置,并且在每次提交时运行脚本。
以上是两种解决方案,你可以根据自己的需求选择适合的方法来解决这个问题。

正文完