Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.'"
      // }
   }
}
like image 253
Idan Adar Avatar asked Mar 07 '17 18:03

Idan Adar


2 Answers

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.

like image 189
Christopher Orr Avatar answered Oct 26 '22 22:10

Christopher Orr


If you're using declarative pipeline (which you are, e.g. the outer pipeline step), then you may only declare the pipeline on the outer layer, e.g. you can't have variable and function definitions. This is the downside of using declarative pipeline.

More info here

As I see it you can solve this the following ways:

  1. Use scripted pipeline instead
  2. Move the code at the beginning to a global pipeline library (Might be tricky to solve variable scoping if a value is used in several places, but it should be doable.
  3. Move the code at the beginning to an script step inside the pipeline and store the values as described here.
like image 34
Jon S Avatar answered Oct 26 '22 22:10

Jon S