首页 > 解决方案 > Azure devops 管道 CmdLine 任务脚本错误

问题描述

嗨,我是 AzureDevops 和管道的新手,我正在尝试使用脚本创建一个 CmdLine 任务,该脚本基于此处的分支名称设置一些变量的脚本:

- task: CmdLine@2
  displayName: Find Branch type
  inputs:
   script: |
     IF contains($(Build.SourceBranch), 'release')==True (set isLongBranch=True
      ) ELSE IF contains($(Build.SourceBranch), 'support') (set isLongBranch=True
      ) ELSE IF contains($(Build.SourceBranchName), 'develop') (set isLongBranch=True
      ) ELSE IF contains($(Build.SourceBranchName), 'master') (set isLongBranch=True
      ) ELSE IF contains($(Build.SourceBranch), 'hotfix') (set isLongBranch=True
      ) ELSE (set isLongBranch=False)
     IF contains($(Build.SourceBranch), 'release') (set isSonar=True
      ) ELSE IF contains($(Build.SourceBranch), 'support'), 'support')] (set isSonar=True
      ) ELSE IF contains($(Build.SourceBranchName), 'develop') (set isSonar=True
      ) ELSE IF contains($(Build.SourceBranchName), 'master') (set isSonar=False
      ) ELSE IF contains($(Build.SourceBranch), 'hotfix') (set isSonar=True
      ) ELSE IF contains($(Build.SourceBranch), 'feature') (set isSonar=True
      ) ELSE IF %isPoolRequest%==True (set isSonar=False
      ) ELSE (set isSonar=False)
      #echo $(Build.SourceBranch)
      #echo $(Build.SourceBranchName)

我得到的错误是:

'release')==True 出乎意料。##[错误]Cmd.exe 已停止。退出代码:'255'。

标签: azure-devopsazure-pipelines

解决方案


Azure devops 管道 CmdLine 任务脚本错误

如果我使用与您相同的代码,我会遇到与您相同的错误。

通常,我们使用findstr来检查变量是否包含子字符串,例如:

echo $(Build.SourceBranch) | findstr "release" >nul &&(
    echo "include"
)

或者,您可以使用 powershell 脚本来执行此操作:

$files = @("$(Build.SourceBranch)")
$excludeTypes = @("*release*","*support*", "*master*")

    foreach ($type in $excludeTypes) {
        if ($file -like $type) { 
            Write-Host ("Match found: {0} matches {1}" -f $file, $type)
            $Env:isLongBranch = true
        } 
          else
        {
          $Env:isLongBranch = False
        }
    }

您可以查看此线程以获取更多详细信息。


推荐阅读