gpt4 book ai didi

azure - If/Else 逻辑和自定义变量

转载 作者:行者123 更新时间:2023-12-03 07:02:58 26 4
gpt4 key购买 nike

我正在尝试运行一些 if / else我的管道中的逻辑使用 expressions .

trigger:
- dev
- qa
- main
- test/*

resources:
- repo: self

variables:
vmImageName: 'ubuntu-latest'
# We get rid off the 'refs/heads/' to isolate the branch name and deduce the environment we're running on.
# If running on 'main' branch, env value will be 'prod'.
env: $[replace(replace(variables['Build.SourceBranchName'], 'main', 'prod'), 'refs/heads/', '')]

stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- script: | # this below block works as expected
if [[ $(env) == "dev" ]] ; then
echo "dev"
elif [[ $(env) == "qa" ]] ; then
echo "qa"
elif [[ $(env) == "prod" ]] ; then
echo "prod"
else
echo "We are running on an unmanaged branch"
fi
displayName: Test 1
- script: echo "Deploying to $(env)"
${{ if in(variables['env'], 'prod', 'qa', 'dev') }}:
displayName: Deploy to a managed environment # we never get there
${{ else }}:
displayName: Deploy to an unmanaged environment # always here

我已经尝试了 variables['env']variables.env语法为 stated here但运气不好了。

知道为什么我的实现不起作用吗?我想我可以使用系统的variables['Build.SourceBranchName']进入那个if/else声明,但我想了解我的代码有什么问题。

最佳答案

因为 $[replace(replace(variables['Build.SourceBranchName'], 'main', 'prod'), 'refs/heads/', '')]运行时表达式,并且 displayName 在运行时之前计算 - 当 YAML 文件编译到计划中时。

这就是 Build.SourceBranchName 适合您的原因,因为此变量是在运行时之前评估的。

关于azure - If/Else 逻辑和自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72013416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com