简体   繁体   English

Powershell脚本任务output的AzureDevOps Pipeline变量赋值

[英]AzureDevOps Pipeline variable assignment of Powershell script task output

I have a YAML pipeline which defines a variable called vmPrivateIp :我有一个 YAML 管道,它定义了一个名为vmPrivateIp的变量:

pr: none
  trigger: none

variables:
  vmName: 'MyVmName'
  vmPrivateIp: '10.0.0.1'

I want to assign this variable with the result of a script task which obtains the private IP of MyVmName :我想用脚本任务的结果分配这个变量,该任务获得 MyVmName 的私有IP

- task: PowerShell@2
  inputs:
  targetType: 'inline'
  script: |
    $IP = (Get-AzNetworkInterface -Name $(vmName)-nic -ResourceGroupName MyResourceGroup).IpConfigurations.PrivateIpAddress
    Write-Host "##vso[task.setvariable variable=vmPrivateIp,isOutput=true]$IP"
    Write-Host $(vmPrivateIp)

The task runs fine but the variable still contains the initial value任务运行良好,但变量仍包含初始值

在此处输入图像描述

What do I need to do in order to successfully assign the result of the script task to vmPrivateIp ?我需要做什么才能成功地将脚本任务的结果分配给vmPrivateIp

In a job, if you use the ' SetVariable ' command in a step to set up an output variable with a new value, it is functionally equivalent to creating a new output variable that has the same name but different value with the pipeline variable.在作业中,如果您在步骤中使用“ SetVariable ”命令设置具有新值的 output 变量,则在功能上等同于创建与管道变量具有相同名称但值不同的新 output 变量。 The pipeline variable and its initial value still are available.管道变量及其初始值仍然可用。

This output variable is only available to the subsequent steps in the same job and the subsequent jobs that depend on current job.此 output 变量仅可用于同一作业中的后续步骤以及依赖于当前作业的后续作业。 For the step which runs the ' SetVariable ' command to set up this output variable, this output variable is not available.对于运行“ SetVariable ”命令设置此 output 变量的步骤,此 output 变量不可用。

  • For the subsequent steps in the same job, you should use the expression " $(stepId.varName) " to access the value of the output variable.对于同一作业中的后续步骤,您应该使用表达式“ $(stepId.varName) ”来访问 output 变量的值。 The expression " $(varName) " will return the value of the pipeline variable that is the initial value.表达式“ $(varName) ”将返回管道变量的值,即初始值。

  • For each subsequent job that depends on current job, you should use the expression " $[dependencies.jobId.outputs['stepId.varName']] " to assign the value of the output variable to a job variable, then in the steps of the job you can access the value through this job variable.对于依赖于当前作业的每个后续作业,您应该使用表达式“ $[dependencies.jobId.outputs['stepId.varName']] ”将 output 变量的值分配给作业变量,然后在步骤您可以通过此作业变量访问该值的作业。

To view more details, you can see " SetVariable: Initialize or modify the value of a variable " and " Set a multi-job output variable ".要查看更多详细信息,您可以查看“ SetVariable:初始化或修改变量的值”和“ 设置多作业 output 变量”。

An example as reference:一个例子作为参考:

  • azure-pipelines.yml azure-pipelines.yml
variables:
  vmPrivateIp: '10.0.0.1'

jobs:
- job: 'JobA'
  displayName: 'Job A'
  steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: Write-Host "vmPrivateIp = $(vmPrivateIp)"
    displayName: 'View IP before update'

  - task: PowerShell@2
    name: 'UpdateIp'
    inputs:
      targetType: 'inline'
      script: Write-Host "##vso[task.setvariable variable=vmPrivateIp;isoutput=true]10.0.0.2"
    displayName: 'Update IP and set output'

  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "vmPrivateIp = $(vmPrivateIp)"
        Write-Host "UpdateIp.vmPrivateIp = $(UpdateIp.vmPrivateIp)"
    displayName: 'View IP after update'

- job: 'JobB'
  displayName: 'Job B'
  dependsOn: 'JobA'
  variables:
    IP_from_JobA: $[dependencies.JobA.outputs['UpdateIp.vmPrivateIp']]
  steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: Write-Host "IP_from_JobA = $(IP_from_JobA)"
    displayName: 'View IP from JobA'
  • Result结果在此处输入图像描述

    在此处输入图像描述

    在此处输入图像描述

The purpose of task.setVariable is to communicate variable values to other tasks, jobs, and stages of your pipeline. task.setVariable的目的是将变量值传达给管道的其他任务、作业和阶段。 You can't expect it to reflect a value into a variable within the task because it operates effectively after the task has completed - the pipeline runs Powershell, then scans the output for task.setVariable , and sets variables accordingly.您不能期望它将值反映到任务中的变量中,因为它在任务完成后有效运行 - 管道运行 Powershell,然后扫描 output 中的task.setVariable ,并相应地设置变量。 So, within a task, you use the Powershell variable itself.因此,在任务中,您使用 Powershell 变量本身。

- task: PowerShell@2
  name: GetPrivateIp
  inputs:
  targetType: 'inline'
  script: |
    # Lives local to this script
    $IP = (Get-AzNetworkInterface -Name $(vmName)-nic -ResourceGroupName MyResourceGroup).IpConfigurations.PrivateIpAddress
    # Sets release variable after execution
    Write-Host "##vso[task.setvariable variable=privateIp;isOutput=true]$IP"
    # Writes your initial value as declared
    Write-Host $(vmPrivateIp)
- task: PowerShell@2
  name: UsePrivateIp
  inputs:
  targetType: 'inline'
  script: |
    Write-Host "$(GetPrivateIp.privateIp)"

Note that this syntax is for communication within a job - a different syntax is needed to communicate the variable to another job or stage of your pipeline.请注意,此语法用于作业内的通信 - 需要使用不同的语法将变量与管道的另一个作业或阶段进行通信。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AzureDevOps Powershell 任务命令行参数在 Powershell 脚本中未正确呈现 - AzureDevOps Powershell task command line parameters are not properly rendered in Powershell script AzureDevOps:将任务变量作为对象传递给后续的 powershell 任务类型 - AzureDevOps: Pass task variable to subsequent powershell task type as Object Powershell变量分配与管道 - Powershell variable assignment vs pipeline 在 Azuredevops 发布管道中使用 powershell 脚本执行 SSIS 作业 - Executing SSIS job using powershell script in Azuredevops release pipeline Powershell管道变量无输出 - Powershell Pipeline Variable No Output 如何将powershell脚本变量值传递给Release管道中的下游任务 - How to pass powershell script variable values to downstream task in Release pipeline 将变量值从 Powershell 任务中的一个脚本传递到 Azure DevOps 发布管道中下一个 Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in Powershell task to another script in the next Powershell task in Azure DevOps Release Pipeline 将变量值从 AZ Powershell 任务中的一个 Powershell 脚本传递到 Azure DevOps Release Pipeline 中下一个 AZ Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in AZ Powershell task to another script in the next AZ Powershell task in Azure DevOps Release Pipeline Azure 管道 AWS Powershell 脚本任务 - Azure Pipeline AWS Powershell script task 如何将批处理脚本输出管道化到PowerShell脚本? - How to pipeline batch script output to a PowerShell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM