Nik*_*kin 20 continuous-integration azure azure-devops azure-pipelines-build-task azure-pipelines
我有一些测试自动化代码从存储在本地机器上的环境变量中读取一些值,如下所示:
Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Azure Pipelines 在管道执行期间创建此变量,然后在我的测试自动化代码中读取它。使用 YAML 文件。
我在 Azure Pipeline 的 VS 测试步骤中读取此变量。因此,如果我设置该变量,它必须在 Azure Pipeline 的整个生命周期内。
也尝试了下面的代码,但由于此错误而失败:
azure-pipelines.yml (Line: 39, Col: 1, Idx: 1252) - (Line: 39, Col: 1, Idx: 1252):扫描一个简单的键时,找不到预期的':'。
# 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)
Run Code Online (Sandbox Code Playgroud)
dea*_*dog 13
我尝试使用上面答案中建议的以下两种语法,但是当尝试在管道中进一步的任务中使用它时(例如在构建期间或运行测试时),环境变量始终为空。
[Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$(sauceUserName)", "User")
variables:
sauceUserName: '$(sauceUserName)'
Run Code Online (Sandbox Code Playgroud)
对我有用的是使用语法在内联 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)"
Run Code Online (Sandbox Code Playgroud)
然后,我的构建任务能够找到环境变量,并且我还可以在管道中的 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'."
Run Code Online (Sandbox Code Playgroud)
Nik*_*kin 11
最简单的方法是将 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)
Run Code Online (Sandbox Code Playgroud)
如果您尝试,显示或使用该值将起作用
- bash: echo $(SAUCE_USERNAME) # will output our username stored in SAUCE_USERNAME env variable
Run Code Online (Sandbox Code Playgroud)
如果您SAUCE_USERNAME在代码中引用,则代码将从 Azure 服务器获取值。
之前我也用过Powershell,但是这个方法比较复杂,比较复杂:
这是另一篇详细的文章,可以帮助您解决这个问题。
根据要求,我还附上了使这成为可能的 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")
Run Code Online (Sandbox Code Playgroud)
设置管道变量,然后在您的 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)'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58281 次 |
| 最近记录: |