Azure DevOps YAML流水线打印变量时出现‘command not found’错误
问题原因
你当前用System.PullRequest.TargetBranch变量判断加载变量组,但这个变量仅在PR触发的流水线(即pr:字段配置的触发)中存在。而你的流水线是通过trigger:配置的分支推送触发(PR合并后会推到目标分支,属于分支推送事件),此时这个变量为空,导致所有if条件都不满足,变量组没有被加载,脚本里的$(OrgAlias)就会被bash当成命令执行,从而报错。
解决方案
把判断分支的变量换成Build.SourceBranch(这个变量在分支推送触发的流水线中会返回当前触发的分支完整路径,比如refs/heads/master),修改后的YAML配置如下:
# specific branch build with batching trigger: batch: true branches: include: - master - DEV - QA paths: exclude: - README.md pr: none jobs: - job: Deploy variables: - ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}: group: sfdx-org-prod - ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/QA') }}: group: sfdx-org-qa - ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/DEV') }}: group: sfdx-org-dev steps: - script: | echo "当前触发分支: $(Build.SourceBranch)" echo "OrgAlias: $(OrgAlias)" echo "SalesforceUserName: $(SalesforceUserName)" displayName: "Selected Variable Group"
额外验证步骤
- 确认变量组中确实存在
OrgAlias和SalesforceUserName变量,且变量名称完全匹配(大小写敏感) - 检查流水线的权限:确保流水线所在的项目有权限访问这些变量组(在变量组的"安全"设置中添加流水线的服务主体或项目组)
内容的提问来源于stack exchange,提问作者Shashank
相关产品推荐
相关产品推荐

