azure - 在 AzurePowerShellV5 内联脚本中引用 yaml 变量
问题描述
我有一个 Azure DevOps yaml 管道,看起来(有点像)是这样的:
variables:
MyVar: Test
Steps:
- task: AzurePowerShell@5
displayName: 'Test variables from yml file'
inputs:
azureSubscription: MyServiceConnection
ScriptType: InLineScript
InLine: |
Write-Host "I really want to see the values from the variables in the yml file here"
Write-Host "parameters from the yml file would be great too"
Write-Host "But what would I write to do that?"
Write-Host "$(MyVar) <- Nothing here"
Write-Host "$(variables.MyVar) <- Nothing here"
Write-Host "$DoesThisWork <- Nothing here"
Write-Host "$OrThis <- Nothing here"
env:
DoesThisWork: $(MyVar)
OrThis: $(variables.MyVar)
如何在 InLine 脚本中使用 MyVar?
解决方案
我把它精简到最简单的,它工作得很好:
pool:
vmImage: 'windows-latest'
variables:
myVar: test value
steps:
- powershell: Write-Host "$(myVar)"
生成:
我修改了您的示例以删除env
未编译的块,并删除不正确的变量引用:
pool:
vmImage: 'windows-latest'
variables:
myVar: test value
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'My Service Connection Name'
ScriptType: 'InlineScript'
Inline: |
Write-Host "I really want to see the values from the variables in the yml file here"
Write-Host "parameters from the yml file would be great too"
Write-Host "But what would I write to do that?"
Write-Host "$(MyVar) <- Nothing here"
azurePowerShellVersion: 'LatestVersion'
并得到:
它没有通过第一个,因为语法$(variables.MyVar)
无效。语法如下:
- 编译时间(只能在声明变量的文件中使用,而不是像模板这样的嵌套文件):
${{ variables.MyVar }}
- 运行时(任务执行前):
$(MyVar)
- 如果为空,则扩展为“$(MyVar)” - 运行时(为条件设计,或默认值导致问题):
$[variables.MyVar]
- 如果为空,则扩展为空字符串
我想知道缺少 Azure Powershell 版本是否是您的问题的一部分?
推荐阅读
- python - 使用 Pandas 和 SQL Alchemy 根据某些列更新或更新 SQL 记录?
- flutter - 如何在键盘按下时更新 Pin?
- tsql - 如何忽略查询中的记录以避免JOIN中的转换错误?
- python - 在条形图中设置不同的颜色
- lua - lua - 如何比较不同的数组
- python - 如何将一个列表中的每个元素乘以另一个列表中的每个元素?
- javascript - IonSlides 在 lastSLide 上渲染一些东西。离子反应
- google-sheets - Google 表格 - [未找到 URL 处的资源]
- java - Android MongoDB Atlas - 使用连接 url 字符串时出错
- gpu - 分析 JAX 代码:什么是 redzone_checker,为什么要花这么多时间?