spo*_*ser 5 azure-devops azure-pipelines
我有一个场景,我需要在两个管道之间传递一个变量,并在目标管道上的编译时评估该变量。
我有一个构建管道,可以
由于 Azure Pipelines 的限制,构建管道需要保持独立于部署管道。触发器批处理功能当前不支持运行新构建,除非第一个构建已 100% 完成管道。我们有手动批准,所以这是不行的。
我有一个部署管道,理想情况下应该支持使用参数传递“AllServices”或用户想要部署的单个服务的名称来手动触发,并支持在自动构建期间由资源管道触发。
我正在努力解决的问题是部署管道如何获取在触发期间发送给它的信息,或者可以将其设置在编译时读取的地方?这些变量仅在运行时可用
resources.pipeline.<Alias>.projectName
resources.pipeline.<Alias>.projectID
resources.pipeline.<Alias>.pipelineName
resources.pipeline.<Alias>.pipelineID
resources.pipeline.<Alias>.runName
resources.pipeline.<Alias>.runID
resources.pipeline.<Alias>.runURI
resources.pipeline.<Alias>.sourceBranch
resources.pipeline.<Alias>.sourceCommit
resources.pipeline.<Alias>.sourceProvider
resources.pipeline.<Alias>.requestedFor
resources.pipeline.<Alias>.requestedForID
Run Code Online (Sandbox Code Playgroud)
编译时间与运行时在这里很重要,因为我使用 ${{ }}: 语法来控制将哪些参数注入到部署模板中。
resources:
pipelines:
- pipeline: services_build
source: Services.Build
trigger: true
name: Services-$(Date:yyyyMMdd).$(Rev:r)
trigger: none
pr: none
stages:
- template: ../templates/deploy_the_services.yaml
parameters:
${{ if startsWith(resources.pipeline.services_build.pipelineName, 'AllServices') }}:
services:
- Service1
- Service2
- Service3
${{ if startsWith(resources.pipeline.services_build.pipelineName, 'Service1') }}:
services:
- Service1
Run Code Online (Sandbox Code Playgroud)
部署服务.yaml
parameters:
- name: services
type: object
stages:
- stage: EnvironmentA
displayName: 'Push to EnvironmentA'
jobs:
- template: ../templates/deploy.appservice.yaml
parameters:
environment: 'EnvironmentA'
services: ${{ parameters.services }}
... # Many more stages after this
Run Code Online (Sandbox Code Playgroud)
部署.appservice.yaml
parameters:
- name: services
type: object
- name: environment
type: string
jobs:
# This is the important part. This is an array of services to iterate over
- ${{ each service in parameters.services }}:
- deployment:
displayName: 'Deploy ${{ service.name }}'
environment: '${{ parameters.environment }}'
variables:
- group: '${{ service.name }}'
- group: '${{ parameters.environment }}-secrets'
pool:
vmImage: 'windows-2019'
strategy:
runOnce:
deploy:
steps:
- powershell: |
# Do the stuff and things
Run Code Online (Sandbox Code Playgroud)
我进一步检查了您的问题,如果您希望部署管道在触发期间获取发送给它的信息,这些变量仅在运行时可用。您可以尝试使用pipeline run rest api来传递变量。
\n您可以按照以下步骤进行检查:
\n1.在管道中定义运行时参数以保存传递的变量。见下文:
\nparameters:\n- name: contentKey\n displayName: Pool Image\n default: contentDefaultValue\nRun Code Online (Sandbox Code Playgroud)\n见下文:
\n {\n "templateParameters":{\n "contentKey": "contentValue"\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n下面是我测试的demo,也可以参考这个\xef\xbc\x9a
\n在 yaml 管道文件中。您可以按如下方式定义参数:
\nparameters:\n- name: contentKey\n displayName: pass variables\n default: variable1\nRun Code Online (Sandbox Code Playgroud)\nyaml 中的 powershell 任务如下:
\n- powershell: |\n $token = "PAT"\n $url="https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.1-preview.1"\n $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))\n $body = "{\n `"templateParameters`":{\n `"contentKey`": `"contentValue`"\n }\n }"\n $response3 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $body -ContentType application/json\nRun Code Online (Sandbox Code Playgroud)\n然后您可以在 bash 任务中访问参数,如下所示:
\necho ${{ parameters.contentKey }}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
8628 次 |
| 最近记录: |