Jenkinsfile Pipeline errors: “expected a symbol” and “undefined section”

Jenkinsjenkins-pipeline

Can anyone explain why I get the following errors, and what can be a possible solution for them?

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 {

The code in the Jenkinsfile is as follows:

#!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.'"
      // }
   }
}

Best Answer

Your Pipeline is mostly fine — adding Scripted Pipeline elements before the Declarative pipeline block is generally not a problem.

However, at the very start, you're defining an variable called environment (and git), which are basically overriding the elements declared by the various Pipeline plugins.

i.e. When you attempt to do pipeline { environment { … } }, the environment is referring to your variable declaration, which causes things to go wrong.

Rename those two variables, and you'll fix the first error message.

To fix the second error message, remove the attempts to declare variables from the environment block — this block is only intended for exporting environment variables for use during the build steps, e.g.:

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

The script block you have will work fine without these declarations. Alternatively, you can move those variable declarations to the top level of your script.