Jenkinsfile管道错误:“预期一步”和“未定义部分”

时间:2018-02-26 15:48:04

标签: jenkins jenkins-pipeline

任何人都可以解释为什么我会收到以下错误,以及可能为他们提供哪些解决方案?

  

在耐久性级别运行:MAX_SURVIVABILITY   org.codehaus.groovy.control.MultipleCompilationErrorsException:   启动失败:WorkflowScript:43:预期步骤@第43行,列   17。                      if(isUnix()){                      ^

     

WorkflowScript:71:未定义的部分"成功" @第71行,第5栏。          成功{          ^

     

WorkflowScript:78:未定义的部分"失败" @第78行,第5栏。          失败{          ^

     

WorkflowScript:16:工具类型" maven"没有安装   " MAVEN_HOME"配置 - 你的意思是" Maven M2"? @第16行,列   19。                  maven" MAVEN_HOME"                        ^

     

WorkflowScript:17:工具类型" jdk"没有安装   " JAVA_HOME"配置 - 你的意思是" null"? @第17行,第17栏。                  jdk" JAVA_HOME"                      ^

Jenkinsfile中的代码如下:

  pipeline {

        // agent defines where the pipeline will run.
        agent {
            // Here we define that we wish to run on the agent with the label SL202_win
            label "SL202_win"
        }

        // The tools directive allows you to automatically install tools configured in
        // Jenkins - note that it doesn't work inside Docker containers currently.
        tools {
            // Here we have pairs of tool symbols (not all tools have symbols, so if you
            // try to use one from a plugin you've got installed and get an error and the
            // tool isn't listed in the possible values, open a JIRA against that tool!)
            // and installations configured in your Jenkins master's tools configuration.
            maven "MAVEN_HOME"
            jdk "JAVA_HOME"
        }

        environment {
            // Environment variable identifiers need to be both valid bash variable
            // identifiers and valid Groovy variable identifiers. If you use an invalid
            // identifier, you'll get an error at validation time.
            // Right now, you can't do more complicated Groovy expressions or nesting of
            // other env vars in environment variable values, but that will be possible
            // when https://issues.jenkins-ci.org/browse/JENKINS-41748 is merged and
            // released.
            mvnHome = "D:/Tools/apache-maven-3.5.2"
        }

        stages {
            // At least one stage is required.
            stage("Preparation") {
                // Every stage must have a steps block containing at least one step.
                steps {
                    // Get some code from a GitHub repository
                    git 'https://git.ceesiesdomain.nl/scm/rsd/test_automation.git'
                }
            }
            stage('Build') {
                steps {
                // Run the maven build
                if (isUnix()) {
                    sh "'${mvnHome}/bin/mvn' clean test -Dtest=TestRunner"
                } else {
                    bat(/"${mvnHome}\bin\mvn" clean test -Dtest=TestRunner/)
                }
            }
        }
            stage('Results') {
            steps {
                cucumber buildStatus: 'UNSTABLE', failedFeaturesNumber: 999, failedScenariosNumber: 999, failedStepsNumber: 3, fileIncludePattern: '**/*.json', skippedStepsNumber: 999
            }
        }
    }
        // Post can be used both on individual stages and for the entire build.
        post {
            success {
                echo "Test run completed succesfully."
            }
            failure {
                echo "Test run failed."
            }
            always {
        // Let's wipe out the workspace before we finish!
                deleteDir()
                echo "Workspace cleaned"
            }
    }

    success {
        mail(from: "jenkins@ceesiesdomain.nl",
                to: "ceesie@ceesiesdomain.nl",
                subject: "That build passed.",
                body: "Nothing to see here")
    }

    failure {
        mail(from: "jenkins@ceesiesdomain.nl",
                to: "ceesie@ceesiesdomain.nl",
                subject: "That build failed!",
                body: "Nothing to see here")
    }

    // The options directive is for configuration that applies to the whole job.
    options {
        // For example, we'd like to make sure we only keep 10 builds at a time, so
        // we don't fill up our storage!
        buildDiscarder(logRotator(numToKeepStr:'10'))

        // And we'd really like to be sure that this build doesn't hang forever, so
        // let's time it out after an hour.
        timeout(time: 60, unit: 'MINUTES')
    }
}

2 个答案:

答案 0 :(得分:0)

我设法通过修剪所有多余的东西来开始工作,并从纯粹的基本要素开始,并在每次更改后迭代添加步骤和配置并运行它以验证工作情况。

我现在最终得到的脚本是:

pipeline {
    agent {
        label 'SL202_win' 
    }
    stages {
        stage("Fetch repository") {
            steps {
                    git 'https://git.ceesiesdomain.nl/scm/rsd/test_automation.git'
                }
            }
        stage('Run test') {
            steps {
                bat 'cd d:/SL202_Data/workspace/Front-end-SwiftNL/Sanctie_Regressie_Workflows_WCM' 
                bat 'mvn clean test -f d:/SL202_Data/workspace/Front-end-SwiftNL/Sanctie_Regressie_Workflows_WCM/pom.xml -Dtest=TestRunner'
            }
        }
    }
    post {
        always {
            echo 'Test run completed'
            cucumber buildStatus: 'UNSTABLE', failedFeaturesNumber: 999, failedScenariosNumber: 999, failedStepsNumber: 3, fileIncludePattern: '**/*.json', skippedStepsNumber: 999
        }
        success {
            echo 'Successfully!'
        }
        failure {
            echo 'Failed!'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
    options {
        timeout(time: 60, unit: 'MINUTES')
    }
}

答案 1 :(得分:0)

我的方法是在使用声明性管道时将'if'或'for'块放在“ script {}”块或“ dir(”“){}”块中。