我正在创建一个 azure yaml 管道,并且在 AzureClI@2 任务中我想运行 cli stmt 'az group contains...' 以局部变量作为名称并评估语句的响应,如下所示:
if ( az group exists -n $(dev-resource-group) = false)
then
# do something super cool here.
elseif
# do something less cool else.
fi
有很多变量的示例,但我无法让它执行 cli stmt。我认为这需要运行时,所以可能需要
${{ }}
。感谢这里的任何帮助!
我确实同意@Daniel Mann你需要使用脚本类型作为Shell,请参阅下面:-
我在 AzureCLI@2 中尝试使用 if 和 else 块来检查我的资源组是否存在并且它成功运行,如下所示:-
通过将 $y 设置为 true:-
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'subscripption'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
x="rgname"
y=$(az group exists --name $x --output json)
if [ "$y" = "true" ]; then
echo "Hello Rithwik Resource group exist"
else
echo "Hello Rithwik Resource group does not exist"
fi
输出:-
通过将 $y 设置为 false:-
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'subscription'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
x="rgname2"
y=$(az group exists --name $x --output json)
if [ "$y" = "false" ]; then
echo "Hello Rithwik Resource group exist"
else
echo "Hello Rithwik Resource group does not exist"
fi
输出:-
我能够使用 Powershell 解决这个问题,尽管我更喜欢使用 @RithwikBojja 和他的 bash 示例。如果 powershell 是您的首选语言,希望这对其他人有所帮助。如果剪切并粘贴 yaml,可能需要调整制表符/间距。
pool:
vmImage: 'windows-latest'
variables:
- name: rgName
value: test-resource-group
- name: loc
value: eastus
- name: serviceConnection
value: my-test-sp-conn-all
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: $(serviceConnection)
azurePowershellVersion: latestVersion
pwsh: true
scriptType: inlineScript
inline: |
Get-AzResourceGroup -Name $(rgName) -Location $(loc) -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent)
{
echo "##[debug]Starting creation of $(rgName)"
New-AzResourceGroup -Name $(rgName) -Location $(loc)
echo "##[debug]Created resource group"
}
else
{
echo "##[debug]Resource group '$(rgName)' already exists."
}