Jenkinsfile管道错误:"预期符号"和"未定义部分"

Ida*_*dar 4 jenkins jenkins-pipeline

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

org.codehaus.groovy.control.MultipleCompilationErrorsException:startup failed:WorkflowScript:20:预期符号@第20行,第4列.environment {

WorkflowScript:17:未定义的部分"错误"@第17行,第1列
.duve {

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.'"
      // }
   }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*Orr 6

你的管道大多很好 - 在Declarative pipeline块之前添加Scripted Pipeline元素通常不是问题.

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

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

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

要修复第二条错误消息,请删除从environment块中声明变量的尝试- 此块仅用于导出环境变量以在构建步骤中使用,例如:

environment {
    FOO = 'bar'
    BAR = 'baz'
}
Run Code Online (Sandbox Code Playgroud)

script没有这些声明,您拥有的块将正常工作.或者,您可以将这些变量声明移动到脚本的顶级.