如何从azure管道中的管道资源中获取变量值



基本上,我有2个独立的Azure管道(CI, CD)。而且,我只为CI管道启用了触发器。一旦CI(构建)管道完成,它应该触发CD管道。我在CI管道中传递参数和变量。但是,我也想在CD管道中使用相同的CI参数值。

### CI Pipeline ###
parameters:
- name: environment
displayName: 'Choose Environment'
type: string
default: Dev
values:
- Dev
- Qa
- Stage
- Demo
- Live
variables:
- group: PoC-Web
- group: PoC-Secrets
- name: version

在CI管道中,我使用环境参数并声明版本变量。现在,我想在CD Pipeline中使用这些值。

### CD Pipeline ###
resources:
pipelines:
- pipeline: web-ci
source: PoC-Web-CI
trigger:
branches: 
include:
- cicd/azure-pipelines
- develop
stages:
- stage: 'Package_Deploy'
jobs:
- deployment: 'Deploy_to_Server'
environment: 'PoC-Web.PoC_WEB_SERVER'
strategy:
rolling:
deploy:
steps:
- task: CmdLine@2
inputs:
script: |              
echo "Fetching CI Pipeline Environment Variables"
echo "Pipeline-ID: $(resources.pipeline.web-ci.pipelineID)"
echo "Source-Commit: $(resources.pipeline.web-ci.sourceCommit)"
echo "Version: $(resources.pipeline.web-ci.version)"
echo "Version: $(resources.pipeline.web-ci.variables.version)"
echo "environment: $(resources.pipeline.web-ci.parameters.environment)"
echo "environment: $(resources.pipeline.web-ci.templateParameters.environment)"

在上面的CD管道中,我能够获得管道id &源-提交(即,预定义的变量)。但是无法获取用户定义的变量和参数值,如version和parameter。

如蒙早日答复,不胜感激。

对于您的情况,我将使用Pipeline Artifacts。你可以在这里找到完整的描述https://tsuyoshiushio.medium.com/how-to-pass-variables-with-pipeline-trigger-in-azure-pipeline-5771c5f18f91

In CI Pipeline

导出所有必要的变量到一个文本文件,然后将其作为工件发布。

- task: CmdLine@2
displayName: Create artifact from variables
inputs:
script: |
echo "##vso[task.setvariable variable=FOO;]$(FOO)" >  $(Build.ArtifactStagingDirectory)/pipeline.env
- task: PublishBuildArtifacts@1
displayName: publish variables
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'variables'
publishLocation: 'Container'

In CD Pipeline

创建一个任务,它将下载包含变量的工件。

- task: DownloadBuildArtifacts@0
displayName: 'Download variables'
inputs:
buildType: 'specific'
project: '<YOUR_PROJECT_ID_HERE>'
pipeline: '<YOUR_PIPELINE_ID_HERE>'
buildVersionToDownload: 'latest'
downloadType: 'specific'
downloadPath: '$(System.ArtifactsDirectory)'

以@Cloudziu的出色工作为基础:

根据Microsoft文档,PublishArtifacts已被弃用,MS建议使用publish任务代替。存在一个类似的download任务,但是为了在阶段之间或管道之间传输信息,您仍然必须使用downloadpipelinartifacts任务。

如果你有两个管道,pipeline-one在完成后触发pipeline-two,你可以像这样从一个传递信息到另一个:

从pipeline-one发布:

- powershell: |
$json = @"
{
'build_id': '$(Build.BuildID)',
'build_number': '$(Build.BuildNumber)',
'build_type': '$(Build.Reason)',
'source_repo': '$(Build.Repository.Name)',
'source_branch': '$(Build.SourceBranchName)',
'source_commit_id': '$(Build.SourceVersion)'
}
"@
$f = '$(Pipeline.Workspace)/s/ansible.json'
Add-Content -Path $f -Value $json
displayName: Create artifact from variables
- publish: ansible.json
artifact: theAnsible
displayName: Publish the artifact

读取管道二中的工件:

resources:
pipelines:
- pipeline: pipeline1
source: xyzzy.pipeline-one
trigger:
enabled: true
branches:
include:
- develop
- release_*

- task: DownloadPipelineArtifact@2
displayName: Download the artifact
inputs:
source: 'specific'
project: 'the-project-name'
pipeline: 'xyzzy.pipeline-one' # this works
# pipeline: 12 # or use the pipeline number instead
preferTriggeringPipeline: 'true'
runVersion: 'latest'
artifact: theAnsible
path: '$(Pipeline.Workspace)/s/'
- powershell: |
$f = "$(Pipeline.Workspace)/s/ansible.json"
if( Test-Path $f ) {
Get-Content $f
} else {
Write-Host '$f not found'
}
displayName: Read the artifact

相关内容

  • 没有找到相关文章

最新更新