Azure Pipelines:在 Powershell 中设置和访问变量

Posted

技术标签:

【中文标题】Azure Pipelines:在 Powershell 中设置和访问变量【英文标题】:Azure Pipelines: Setting and accessing variables in Powershell 【发布时间】:2020-09-17 21:09:20 【问题描述】:

在 Azure 管道中,我有一个 Powershell 任务(内联脚本),我在其中设置了一个变量

Write-Host "##vso[task.setvariable variable=deployActivity]false"

紧接着这一行,只是为了检查设置变量的目的,我有以下 powershell -

if ($(deployActivity) -eq $true) 
    Write-Host "deployActivity=true (bool)"

但是,这失败并出现错误:

deployActivity : The term 'deployActivity' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

问题-

    在上面做什么来检索变量 - 是否正确/可能?还是说一个变量只能在后续任务中引用 为什么会失败? Powershell中变量的引用是否需要其他语法?

请告诉我。

谢谢

【问题讨论】:

这能回答你的问题吗? Dynamically set Azure DevOps variable in scripts 【参考方案1】:

根据该命令的documentation:

在taskcontext的变量服务中设置一个变量。第一个任务 可以设置一个变量,后面的任务都可以使用这个变量。 该变量作为环境暴露给以下任务 变量。

它仅在设置它之后的任务中可用。如果您需要在同一个脚本中引用该值,您应该已经知道内部变量应该是什么。

这个示例 YML 适合我:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
  vmImage: 'windows-2019'

steps:

    
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "##vso[task.setvariable variable=deployActivity]"Example string""'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Deploy Activity: $(deployActivity)"'

第二个任务的输出:

Starting: PowerShell
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.170.1
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\3a04683f-6159-4bee-862b-2e899e977789.ps1'"
Deploy Activity:  Example string
Finishing: PowerShell

【讨论】:

我更改了我的任务 - 删除了 if 表达式并将其放入后续的 Powerhshell 脚本中,但我现在在这个新脚本中仍然遇到同样的错误。 是内联脚本还是文件脚本? 它是一个内联脚本 @Matt - 这也不起作用 - Powershell 将其视为未定义的 Powershell 变量 @Matt - 这搞定了。事实上,我在重置第二个任务中的值后添加了另一个任务,并且该值确实只在下一个任务中更新,而不是在设置它的同一个任务中。谢谢!【参考方案2】:

尝试用"" - "$(deployActivity)"包装变量:

if ("$(deployActivity)" -eq $true) 
    Write-Host "deployActivity=true (bool)"

【讨论】:

你能分享你的完整管道和完整日志吗?

以上是关于Azure Pipelines:在 Powershell 中设置和访问变量的主要内容,如果未能解决你的问题,请参考以下文章

如何在 azure-pipelines 中使用公钥对某些内容进行 scp?

Azure Pipelines:在 Powershell 中设置和访问变量

不遵守 Azure Pipelines 任务超时

建立Azure Dev Ops持续集成和持续交付(CICD)(使用 Azure Pipelines 建立CICD)

建立Azure Dev Ops持续集成和持续交付(CICD)(使用 Azure Pipelines 建立CICD)

Azure DevOps Pipelines 上的 Android SDK 构建工具