我有一个 Azure YAML 管道,它在一个步骤中设置变量以在后续步骤中使用。例如,为变量设置时间戳,然后将其传递到“计划开始日期”字段的 ServiceNow 更改管理连接器中。问题是,计划的开始日期字段似乎不接受管道变量的任何语法。无论我如何尝试,管道都会失败。 ServiceNow 更改管理连接器存在于管道步骤中的环境检查中。
这是我的示例 YAML:
trigger:
branches:
include:
- development
paths:
include:
- /migrations
stages:
- stage: SetVariablesStage
jobs:
- job: SetVariableJob
steps:
- powershell: |
$dateTimeFormat = "yyyy-MM-dd HH:mm:ss"
$scheduledDateTimeFormat = "yyyy-MM-dd HH:mm"
$plannedStartDateTime = (Get-Date).AddMinutes(-60).ToString($dateTimeFormat)
$plannedEndDateTime = (Get-Date).ToString($dateTimeFormat)
$scheduledStartDateTime = (Get-Date).AddMinutes(-60).ToString($scheduledDateTimeFormat)
$scheduledEndDateTime = (Get-Date).ToString($scheduledDateTimeFormat)
Write-Host "##vso[task.setvariable variable=plannedStartDateTime;isOutput=true]$plannedStartDateTime"
Write-Host "##vso[task.setvariable variable=plannedEndDateTime;isOutput=true]$plannedEndDateTime"
Write-Host "##vso[task.setvariable variable=scheduledStartDateTime;isOutput=true]$scheduledStartDateTime"
Write-Host "##vso[task.setvariable variable=scheduledEndDateTime;isOutput=true]$scheduledEndDateTime"
echo "plannedStartDateTime: $plannedStartDateTime"
echo "plannedEndDateTime: $plannedEndDateTime"
echo "scheduledStartDateTime: $scheduledStartDateTime"
echo "scheduledEndDateTime: $scheduledEndDateTime"
displayName: 'Set Custom Variables'
name: SetVariableStep
pool:
vmImage: ubuntu-latest
- stage: 'dev_stage'
dependsOn: SetVariablesStage
variables:
- group: TestPipeline-Variables_Dev
- name: env
value: "dev"
jobs:
- deployment: DeployToDevelopment
variables:
plannedStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedStartDateTime'] ]
plannedEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedEndDateTime'] ]
scheduledStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledStartDateTime'] ]
scheduledEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledEndDateTime'] ]
displayName: 'Deploying to Development'
pool:
vmImage: 'ubuntu-latest'
environment:
name: 'Development'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: UsePythonVersion@0
displayName: 'Use Python 3.8.x'
inputs:
versionSpec: '3.8.x'
- task: Bash@3
inputs:
targetType: 'inline'
script: |
<redacted: Deployment script>
- job: update_ServiceNow
dependsOn: DeployToDevelopment
variables:
plannedStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedStartDateTime'] ]
plannedEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedEndDateTime'] ]
scheduledStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledStartDateTime'] ]
scheduledEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledEndDateTime'] ]
steps:
- task: UpdateServiceNowChangeRequest@2
inputs:
ServiceNowConnection: 'ServiceNow_Sandbox'
NewStatus: '6'
otherParameters: '{ "u_work_notes": "$(Build.BuildNumber) $(plannedStartDateTime) $(plannedEndDateTime)", "u_scheduled_start": "$(scheduledStartDateTime)", "u_scheduled_end": "$(scheduledEndDateTime)" }'
pool: server
ServiceNow 连接配置示例:
对于控制选项:链接变量组,我将其连接到正确的变量组。
当我运行管道时,环境检查失败。它与计划开始日期字段相关,因为如果我硬编码一个值,管道就会起作用。如果我删除该值,它可以工作,但后来由于我们现场 ServiceNow 中的业务规则而失败。
如何在字段中使用变量,这样我就不必对其进行硬编码?更新服务的管道中的最后一步现在可以正常工作。我可以使用管道变量正确设置“otherParameters”。只是不在环境检查 ServiceNow 连接器中。
问题是,计划的开始日期字段似乎不接受管道变量的任何语法。
环境审批中的
ServiceNow Change Management
cannot
直接调用上一阶段/作业中创建的变量。
您可以尝试将变量值输出到validate。例如,以
Approvals
输出:
要使用变量值,也可以将目标值放入
variable group
,并链接到ServiceNow Change Management
中的该组,然后就可以使用里面的变量了。
我没有servicenow帐户,所以我以
rest api approval
为例进行了检查。
我更新了powershell任务中的变量组变量值。确保
{project} build service(org)
帐户在变量组安全性上具有管理员角色,以便它可以更新变量值。
stages:
- stage: SetVariablesStage
jobs:
- job: SetVariableJob
steps:
- powershell: |
$dateTimeFormat = "yyyy-MM-dd HH:mm:ss"
$scheduledDateTimeFormat = "yyyy-MM-dd HH:mm"
$plannedStartDateTime = (Get-Date).AddMinutes(-60).ToString($dateTimeFormat)
$plannedEndDateTime = (Get-Date).ToString($dateTimeFormat)
$scheduledStartDateTime = (Get-Date).AddMinutes(-60).ToString($scheduledDateTimeFormat)
$scheduledEndDateTime = (Get-Date).ToString($scheduledDateTimeFormat)
Write-Host "##vso[task.setvariable variable=plannedStartDateTime;isOutput=true]$plannedStartDateTime"
Write-Host "##vso[task.setvariable variable=plannedEndDateTime;isOutput=true]$plannedEndDateTime"
Write-Host "##vso[task.setvariable variable=scheduledStartDateTime;isOutput=true]$scheduledStartDateTime"
Write-Host "##vso[task.setvariable variable=scheduledEndDateTime;isOutput=true]$scheduledEndDateTime"
echo "plannedStartDateTime: $plannedStartDateTime"
echo "plannedEndDateTime: $plannedEndDateTime"
echo "scheduledStartDateTime: $scheduledStartDateTime"
echo "scheduledEndDateTime: $scheduledEndDateTime"
displayName: 'Set Custom Variables'
name: SetVariableStep
- powershell: |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/27?api-version=5.0-preview.1"
Write-Host $url
$json = '{"id":27,"type":"Vsts","name":"TestPipeline-Variables_Dev","variables":{"plannedStartDateTimeVG":{"isSecret":false,"value":"$(SetVariableStep.plannedStartDateTime)"}, "plannedEndDateTimeVG":{"isSecret":false,"value":"$(SetVariableStep.plannedEndDateTime)"}}}'
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "New Variable Value:" $pipeline.variables.plannedStartDateTimeVG.value
Write-Host "New Variable Value:" $pipeline.variables.plannedEndDateTimeVG.value
env:
SYSTEM_ACCESSTOKEN: $(system.accesstoken)
pool:
vmImage: ubuntu-latest
- stage: 'dev_stage'
dependsOn: SetVariablesStage
variables:
- group: TestPipeline-Variables_Dev
- name: env
value: "dev"
jobs:
- deployment: DeployToDevelopment
variables:
plannedStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedStartDateTime'] ]
plannedEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedEndDateTime'] ]
scheduledStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledStartDateTime'] ]
scheduledEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledEndDateTime'] ]
displayName: 'Deploying to Development'
pool:
vmImage: 'ubuntu-latest'
environment:
name: 'Development'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: UsePythonVersion@0
displayName: 'Use Python 3.8.x'
inputs:
versionSpec: '3.8.x'
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo 1 $(plannedStartDateTime)
echo 2 $(plannedEndDateTime)
echo 3 $(scheduledStartDateTime)
echo 4 $(scheduledEndDateTime)
我只是从 url 中的变量组中调用变量: