如何根据矩阵中的vmImage修改Pipeline发布任务的目标路径?
解决方案
当然可以针对不同系统配置不同的路径,以下是几种简洁可行的实现方式:
方法1:在矩阵中定义系统专属路径变量
直接在matrix配置里为每个系统添加对应的目标路径变量,任务中直接引用该变量即可:
jobs: - job: PowerShell_Tests strategy: matrix: linux: imageName: 'ubuntu-latest' artifactPath: '$(Agent.BuildDirectory)/s/src/bin/Debug/net6.0/publish/package.dll' windows: imageName: 'windows-latest' artifactPath: '$(Agent.BuildDirectory)\\s\\src\\bin\\Debug\\net6.0\\publish\\package.dll' mac: imageName: 'macos-latest' artifactPath: '$(Agent.BuildDirectory)/s/src/bin/Debug/net6.0/publish/package.dll' pool: vmImage: $(imageName) - task: PublishPipelineArtifact@1 inputs: targetPath: '$(artifactPath)' publishLocation: 'pipeline'
方法2:基于Agent.OS变量做条件判断
利用Azure DevOps内置的Agent.OS变量(Windows对应Windows_NT,Linux对应Linux,macOS对应Darwin),通过条件分支动态设置路径:
jobs: - job: PowerShell_Tests strategy: matrix: linux: imageName: 'ubuntu-latest' windows: imageName: 'windows-latest' mac: imageName: 'macos-latest' pool: vmImage: $(imageName) - task: PublishPipelineArtifact@1 inputs: targetPath: ${{ if eq(variables['Agent.OS'], 'Windows_NT') }}: '$(Agent.BuildDirectory)\\s\\src\\bin\\Debug\\net6.0\\publish\\package.dll' targetPath: ${{ else }}: '$(Agent.BuildDirectory)/s/src/bin/Debug/net6.0/publish/package.dll' publishLocation: 'pipeline'
方法3:使用系统无关的路径写法
Azure DevOps的任务会自动处理路径分隔符,你可以统一用正斜杠/作为分隔符,Windows系统中任务会自动将其转换为反斜杠:
jobs: - job: PowerShell_Tests strategy: matrix: linux: imageName: 'ubuntu-latest' windows: imageName: 'windows-latest' mac: imageName: 'macos-latest' pool: vmImage: $(imageName) - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Agent.BuildDirectory)/s/src/bin/Debug/net6.0/publish/package.dll' publishLocation: 'pipeline'
三种方法都能解决跨系统路径不兼容的问题,其中方法3最简洁,推荐优先尝试。
内容的提问来源于stack exchange,提问作者Alex_P
相关产品推荐
相关产品推荐

