如何在 Azure DevOps Pipeline 中设置和读取用户环境变量?

How to set and read user environment variable in Azure DevOps Pipeline?

我有一些测试自动化代码可以从存储在本地计算机上的环境变量中读取一些值,如下所示:

Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);

我正在尝试使用 Azure Pipelines 在管道执行期间创建此变量,然后在我的测试自动化代码中读取它。使用 YAML 文件。

我在 Azure Pipeline 的 VS 测试步骤中读取这个变量。因此,如果我设置变量,它必须在 Azure Pipeline 的生命周期内有效。

我尝试使用文档 here 但没有成功。

也尝试了下面的代码,但失败并出现此错误:

azure-pipelines.yml (Line: 39, Col: 1, Idx: 1252) - (Line: 39, Col: 1, Idx: 1252): While scanning a simple key, could not find expected ':'.

# Create a secret variable
- powershell: |
Write-Host '##vso[task.setvariable variable=sauce.userName;issecret=true]abc'

# Attempt to output the value in various ways
- powershell: |
# Using an input-macro:
Write-Host "This works: $(sauce.userName)"

# Using the env var directly:
Write-Host "This does not work: $env:SAUCE_USERNAME"

# Using the mapped env var:
Write-Host "This works: $env:SAUCE_USERNAME"
env:
SAUCE_USERNAME: $(sauce.userName)

设置管道变量,然后在您的 yaml 文件中尝试此映射:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

pool:
  vmImage: 'VS2017-Win2016'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  yourEnvVar: '$(yourPipelineVariable)'
  yourOtherEnvVar: '$(yourOtherPipelineVariable)'

最简单的方法是像这样将 Azure DevOps(ADO) 环境变量值传递到您的密钥中:

- task: DotNetCoreCLI@2
  displayName: 'Run tests'
  env:
    SAUCE_USERNAME: $(sauceUsername) #this will store the value from 'sauceUsername' into SAUCE_USERNAME
    SAUCE_ACCESS_KEY: $(sauceKey)

如果您尝试,显示或使用该值将有效

- bash: echo $(SAUCE_USERNAME) # will output our username stored in SAUCE_USERNAME env variable

如果您在代码中引用 SAUCE_USERNAME,代码将从 Azure 服务器获取值。

This article has a good explanation

之前我也是用Powershell,但是这个方法比较复杂:

  1. 在 Azure DevOps 管道中创建变量并为这些变量提供值。
  2. 创建一个 Powershell 脚本,您将在开始时 运行 设置环境变量。 This is 我的 Posh 长什么样。
  3. 运行 这个 Posh 在开始时作为您的 CI 管道中的一个单独步骤,这将为用于 运行 您的管道的 VM 设置环境变量。

这是另一个 detailed article,可以帮助您解决这个问题。

根据要求,我还附上了使这成为可能的 PowerShell 代码。

Param(
[string]$sauceUserName,
[string]$sauceAccessKey,
[string]$sauceHeadlessUserName,
[string]$sauceHeadlessAccessKey
)
Write-Output "sauce.userName that was passed in from Azure DevOps=>$sauceUserName"
Write-Output "sauce.accessKey that was passed in from Azure DevOps=>$sauceAccessKey"
Write-Output "sauce.headless.userName that was passed in from Azure DevOps=>$sauceHeadlessUserName"
Write-Output "sauce.headless.access.key that was passed in from Azure DevOps=>$sauceHeadlessAccessKey"

[Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_ACCESS_KEY", "$sauceAccessKey", "User")
[Environment]::SetEnvironmentVariable("SAUCE_HEADLESS_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_HEADLESS_ACCESS_KEY", "$sauceAccessKey", "User")

我尝试使用上面答案中建议的以下两种语法,但是当尝试在管道中更下游的任务中使用它时,环境变量始终为空(例如在构建期间或 运行测试)。

[Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$(sauceUserName)", "User")

variables:
  sauceUserName: '$(sauceUserName)'

对我有用的是使用语法在内联 PowerShell 脚本任务中编写 Azure DevOps 变量:

- task: PowerShell@2
  displayName: Add the username as an environment variable so the tests can find it.
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Making the sauceUsername available as an environment variable."
      Write-Host "##vso[task.setvariable variable=SAUCE_USERNAME;]$(sauceUserName)"

然后我的构建任务能够找到环境变量,我也可以在管道中进一步向下的 PowerShell 脚本任务中访问它,代码如下:

- task: PowerShell@2
  displayName: Display the environment variable value for debugging purposes.
  inputs:
    targetType: 'inline'
    script: |
      [string] $username= $Env:SAUCE_USERNAME
      Write-Host "The SAUCE_USERNAME environment variable value is '$username'."

我们为此苦苦挣扎了几个小时,认为这是由于无法设置环境变量造成的,但最终与此无关。

以下是我们在尝试为 Azure DevOps 解决这个问题时的一些笔记:

  • 管道变量是环境变量,在流程的第一步被注入到管道中。您可以通过查看列出所有环境变量的 Initialize job 任务的日志来确认这一点。
  • 为了证明管道变量在系统中,您可以添加一个 powershell 任务并内联:
Write-Host "Pipeline Variable: $(FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD)"
Write-Host "Environment Variable: $Env:FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"
  • 管道变量周围的 () 关键 如果您想在某处使用管道变量 - 例如$(myVar)

与用户问题无关,但可能对那些尝试使用双因素身份验证 (2FA) 上传到 AppStore 的人有帮助。这是我们关注的documentation

  • 我们必须设置 FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORDFASTLANE_SESSION 才能让它绕过 2FA。我们认为第一个会话允许我们登录 AppStoreConnect,但应用程序专用密码用于上传。尽管您从未看到使用的会话变量,但日志有点暗示正在发生的事情。