Jenkinsfile 管道错误:"expected a symbol" 和 "undefined section"

Jenkinsfile Pipeline errors: "expected a symbol" and "undefined section"

任何人都可以解释为什么我会收到以下错误,有什么可能的解决方案吗?

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 20: Expected a symbol @ line 20, column 4. environment {

WorkflowScript: 17: Undefined section "error" @ line 17, column 1.
pipeline {

Jenkinsfile中的代码如下:

#!groovy

def application, manifest, git, environment, artifactory, sonar

fileLoader.withGit('git@<reducted>', 'v1', 'ssh-key-credential-id-number') {
   application = fileLoader.load('<reducted>');
   manifest = fileLoader.load('<reducted>');
   git = fileLoader.load('<reducted>');
   environment = fileLoader.load('<reducted>');
}

pipeline {
   agent { label 'cf_slave' }

   environment {
      def projectName = null
      def githubOrg = null
      def gitCommit = null
   }

   options {
      skipDefaultCheckout()
   }

   stages {
      stage ("Checkout SCM") {
         steps {
            checkout scm

            script {
               projectName = git.getGitRepositoryName()
               githubOrg = git.getGitOrgName()
               gitCommit = manifest.getGitCommit()
            }
         }
      }

      stage ("Unit tests") {
         steps {
            sh "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./testResults/results.xml"
            junit allowEmptyResults: true, testResults: 'testResults/results.xml'
         }
      }

      //stage ("SonarQube analysis") {
      //...
      //}

      // stage("Simple deploy") {
      //    steps {
      //       // Login
      //       sh "cf api <reducted>"
      //       sh "cf login -u <reducted> -p <....>"
      //       
      //       // Deploy
      //       sh "cf push" 
      //    }
      // }
   }

   post {
      // always {

      //  }
      success {
         sh "echo 'Pipeline reached the finish line!'"

         // Notify in Slack
         slackSend color: 'yellow', message: "Pipeline operation completed successfully. Check <reducted>"
      }
      failure {
         sh "echo 'Pipeline failed'"
         // Notify in Slack
         slackSend color: 'red', message: "Pipeline operation failed!"

         //Clean the execution workspace
         //deleteDir()
      }
      unstable {
         sh "echo 'Pipeline unstable :-('"
      }
      // changed {
      //    sh "echo 'Pipeline was previously failing but is now successful.'"
      // }
   }
}

如果您使用的是声明式管道(您就是这样,例如外部 pipeline 步骤),那么您只能在外层声明 pipeline,例如你不能有变量和函数定义。这是使用声明式管道的缺点。

更多信息here

据我所知,您可以通过以下方式解决此问题:

  1. 改为使用脚本化管道
  2. 将开头的代码移到全局管道库中(如果在多个地方使用一个值,解决变量作用域可能会很棘手,但它应该是可行的。
  3. 将开头的代码移动到管道内的 script 步骤,并按照 所述存储值。

您的流水线大部分都很好——在声明性 pipeline 块之前添加脚本化流水线元素通常不是问题。

但是,在一开始,您定义了一个名为 environment(和 git)的变量,它基本上覆盖了各种管道插件声明的元素。

即当您尝试执行 pipeline { environment { … } } 时,environment 指的是您的变量声明,这会导致出错。

重命名这两个变量,您将修复第一条错误消息。

要修复第二条错误消息,请删除从 environment 块中声明变量的尝试 — 此块仅供 exporting environment variables 在构建步骤中使用,例如:

environment {
    FOO = 'bar'
    BAR = 'baz'
}

您拥有的 script 块在没有这些声明的情况下也可以正常工作。或者,您可以将这些变量声明移动到脚本的顶层。