简体   繁体   English

是否可以在 Azure DevOps 的 Azure Powershell 任务中设置变量并将它们传递给另一个任务?

[英]Is it possible to set variables in the Azure Powershell task in Azure DevOps and pass them to another task?

From reading the Azure DevOps documentation here , it looks like it's possible to define variables in scripts, but only for Bash and Powershell tasks.通过阅读此处的 Azure DevOps 文档,看起来可以在脚本中定义变量,但仅适用于 Bash 和 Powershell 任务。 If I want to use the Azure Powershell task, it does not seem to work.如果我想使用 Azure Powershell 任务,它似乎不起作用。 Here is my yaml for what I am trying to do.这是我正在尝试做的事情的 yaml。

- stage: promote_image
 displayName: Promote VM Image to Prod
 jobs:
 - job: VMPush
   steps:
   - task: AzurePowerShell@5
     inputs:
       ScriptType: 'InlineScript'
       Inline: |
         $srcimageversion = get-azgalleryimageversion -ResourceGroupName $srcRG `
         -GalleryName $srcgallery `
         -GalleryImageDefinitionName $srcimagename | select -last 1
         Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$srcimageversion.Name"
         Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$srcimageversion.Id.ToString()"
         Write-Host "Version name is $($env:VersionName)"
         Write-Host "VM source is $($env:SourceId)"
       azurePowerShellVersion: 'LatestVersion'
       azureSubscription: $(nonProd_Subscription)
   - task: AzurePowerShell@5
     inputs:
       ScriptType: 'InlineScript'
       Inline: |
         Write-Host "Version name is $($env:VersionName)"
         Write-Host "VM source is $($env:SourceId)"
       azurePowerShellVersion: 'LatestVersion'
       azureSubscription: $(prod_Subscription)

The VersionName and SourceId variables don't get output in either task. VersionName 和 SourceId 变量不会在任一任务中获得输出。 Is it even possible to set variables like this with the Azure Powershell task?甚至可以使用 Azure Powershell 任务设置这样的变量吗? I am using the Azure Powershell task since I need to obtain information from a resource in one Azure subscription and then use it to deploy something in another Azure subscription.我正在使用 Azure Powershell 任务,因为我需要从一个 Azure 订阅中的资源获取信息,然后使用它在另一个 Azure 订阅中部署某些内容。

Yes, this is possible.是的,这是可能的。 You have a mistake in a way how you get variables.您在获取变量的方式上有错误。 First let's name your fist task and then use just $(setvarStep.VersionName) instead of $($env:VersionName)" you should get what you need首先让我们命名你的第一个任务,然后只使用$(setvarStep.VersionName)而不是$($env:VersionName)"你应该得到你需要的

- task: AzurePowerShell@5
  name: setvarStep
  inputs:
    ScriptType: 'InlineScript'
    Inline: |
      $value1 = "Value1"
      $value2 = "Value2"
      Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$value1"
      Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$value2"value"
    azurePowerShellVersion: 'LatestVersion'
    azureSubscription: 'rg-the-code-manual'
- task: AzurePowerShell@5
  inputs:
    ScriptType: 'InlineScript'
    Inline: |
      Write-Host "Version name is $(setvarStep.VersionName)"
      Write-Host "VM source is $(setvarStep.SourceId)"
    azurePowerShellVersion: 'LatestVersion'
    azureSubscription: 'rg-the-code-manual'

Here agree with Krzysztof Madej .这里同意Krzysztof Madej 的观点 To use the output variable of the task, we need to use the depend name .要使用任务的输出变量,我们需要使用依赖名称

Use outputs in the same job:在同一作业中使用输出:

steps:
- task: MyTask@1  # this step generates the output variable
  name: ProduceVar  # because we're going to depend on it, we need to name the step
- script: echo $(ProduceVar.MyVar) # this step uses the output variable

In addition, use outputs in a different job:此外,在不同的作业中使用输出:

jobs:
- job: A
  steps:
  - task: MyTask@1  # this step generates the output variable
    name: ProduceVar  # because we're going to depend on it, we need to name the step
- job: B
  dependsOn: A
  variables:
    # map the output variable from A into this job
    varFromA: $[ dependencies.A.outputs['ProduceVar.MyVar'] ]
  steps:
  - script: echo $(varFromA) # this step uses the mapped-in variable

For detailed guide, please refer to this official documnet .有关详细指南,请参阅此官方文档

暂无
暂无

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

相关问题 将变量值从 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 Azure DevOps-自定义任务-具有Azure身份验证的PowerShell - Azure DevOps - Custom Task - PowerShell with Azure Authentification Azure powershell 任务中的 AzureRM 命令:Azure DevOps - AzureRM commands in Azure powershell Task: Azure DevOps Azure Devops Powershell 任务:生成变量的编程访问 - Azure Devops Powershell task: Programmatic Access of Build Variables 是否可以在遵循幂等原则的 Azure DevOps 中创建 PowerShell 任务 - Is it possible to create PowerShell task in Azure DevOps which follow the principle of idempotent 是否可以在 Azure DevOps 中明确地将 task.status 设置为成功? - Is it possible to set the task.status as Succeeded in Azure DevOps explicitly? 在 Azure powershell 任务中添加/获取 Azure Keyvault 的秘密 - Add/get secret to Azure Keyvault with Azure powershell task in Azure devops 如何在 Azure DevOps Azure Powershell 任务中启用 PowerShell 交互模式? - How to enable PowerShell Interactive mode in Azure DevOps Azure Powershell Task? 在 Azure DevOps 中将工件传递给 Docker 任务 - Pass artifact to Docker task in Azure DevOps 在 DevOps 中将参数传递给 Azure CLI 任务 - Pass parameter to Azure CLI Task in DevOps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM