首页 > 解决方案 > 简单管道“意外令牌”的 Groovy 编译错误

问题描述

我有一个 Jenkinsfile 来使用 Sonarcube 执行代码分析。当我推送提交时,Jenkins 构建触发器但在 Jenkinsfile 的以下行中引发异常:

 -Dsonar.sources=. \

具有以下堆栈跟踪:在耐久性级别运行:MAX_SURVIVABILITY org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:WorkflowScript:21:意外令牌:。@ 第 21 行,第 28 列。-Dsonar.sources=。
^ 1 个错误

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)

这是我的 Jenkinsfile :

    pipeline {
            agent any
            stages {
                stage('Checkout'){
                    steps{
                        checkout([$class: 'GitSCM', branches: [[name: '*/develop']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'Golide', url: 'https://github.com/Username/MyRepo.git']]])
                    }
                }
                stage('Build') {
                        steps {
                            bat "\"${tool 'MSBuild'}\" PaySys.sln /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:SkipInvalidConfigurations=true /t:build /p:Configuration=Release /p:Platform=\"Any CPU\" /p:DeleteExistingFiles=True /p:publishUrl=c:\\inetpub\\wwwroot\\"
                        }
                }
                stage('Quality Gate') {
     steps {
       script {
       def MSBuildScannerHome = tool 'MSBuild_SonarScanner';
           withSonarQubeEnv("LocalSonar") {
           bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe end"
           -Dsonar.projectKey=PaySys \
           -Dsonar.sources=. \
           -Dsonar.css.node=. \
           -Dsonar.host.url=http://localhost:9000 \
           -Dsonar.login=dgdhd6585gjgkgkfkfflf7584949"
               }
           }
       }
   }
            }
}

MSBuild_SonarScanner 指的是 Jenkins 的SonarScanner 用于 MSBuild 安装,LocalSonar 指的是 Jenkins 的SonarQube 安装。两个问题:

  1. 我的 groovy 脚本有什么问题?

  2. 我的配置是否足够/正确,足以触发代码分析

标签: jenkinsgroovymsbuildjenkins-pipeline

解决方案


看起来您需要在这里使用多行脚本语法。将您的 bat 脚本包装成'''引号:

bat '''${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe end
           -Dsonar.projectKey=PaySys \
           -Dsonar.sources=. \
           -Dsonar.css.node=. \
           -Dsonar.host.url=http://localhost:9000 \
           -Dsonar.login=dgdhd6585gjgkgkfkfflf7584949'''

推荐阅读