-1

I am trying to write a jenkins declarative pipeline code to execute a shell script and capture the exit status of the code. When I try executing, it throws an exception.

pipeline {
    stages {
        stage ('shcheck'){
            steps {
                echo 'Hello world - shcheck'
                status = sh(
                    returnStdout: true, 

                    script: '''#!/usr/bin/bash
                        echo 'testing sh'
                        exit 0
                    '''
                )

            }
        }

    }
}

On executing this, I get the following execption:

Running in Durability level: MAX_SURVIVABILITY
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
java.lang.NullPointerException
    at org.jenkinsci.plugins.jx.pipelines.dsl.PipelineDSLGlobal.getGlobalForName(PipelineDSLGlobal.java:114)
    at org.jenkinsci.plugins.jx.pipelines.dsl.JXPipelinesValidator.validateElement(JXPipelinesValidator.java:17)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.DeclarativeValidatorContributor.validateElementAll(DeclarativeValidatorContributor.java:226)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.DeclarativeValidatorContributor$validateElementAll$4.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.DeclarativeValidatorContributor$validateElementAll$5.call(Unknown Source)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl$_validateFromContributors_closure43.doCall(ModelValidatorImpl.groovy:804)
    at sun.reflect.GeneratedMethodAccessor484.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at groovy.lang.Closure.call(Closure.java:414)
    at groovy.lang.Closure.call(Closure.java:430)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3202)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3172)
    at org.codehaus.groovy.runtime.dgm$67.invoke(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl.validateFromContributors(ModelValidatorImpl.groovy:801)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl.this$2$validateFromContributors(ModelValidatorImpl.groovy)
    at sun.reflect.GeneratedMethodAccessor578.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:182)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl.validateFromContributors(ModelValidatorImpl.groovy)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl.this$2$validateFromContributors(ModelValidatorImpl.groovy)
    at sun.reflect.GeneratedMethodAccessor579.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
    at org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidatorImpl.validateElement(ModelValidatorImpl.groovy:420)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStep.validate(ModelASTStep.java:70)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBranch.validate(ModelASTBranch.java:40)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStage.validate(ModelASTStage.java:141)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages.validate(ModelASTStages.java:43)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages.validate(ModelASTStages.java:37)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef.validate(ModelASTPipelineDef.java:66)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef$validate.call(Unknown Source)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser.parsePipelineStep(ModelParser.groovy:264)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser.this$2$parsePipelineStep(ModelParser.groovy)
    at sun.reflect.GeneratedMethodAccessor982.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser.parse(ModelParser.groovy:162)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser$parse.callCurrent(Unknown Source)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser.parse(ModelParser.groovy:129)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser.parse(ModelParser.groovy)
    at org.jenkinsci.plugins.pipeline.modeldefinition.parser.GroovyShellDecoratorImpl$1.call(GroovyShellDecoratorImpl.java:78)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1065)
Caused: BUG! exception in phase 'semantic analysis' in source unit 'WorkflowScript' unexpected NullpointerException
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1070)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.lambda$doParse$0(CpsGroovyShell.java:135)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:132)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

I expect that 0 is stored in 'status' variable. I am not sure if I am missing any plugins or configuration settings.

I am a newbie to jenkins and any help would be appreciated.

Jenkins ver. 2.150.1

0

Your pipeline has syntax errors that cause the validation failed.

  1. missing agent
  2. status = sh () need to put inside script

Fixed code:

pipeline {
  agent any

  stages {

    stage ('shcheck'){
      steps {
        echo 'Hello world - shcheck'

        script {
          status = sh(
              returnStdout: true, 

              script: '''#!/usr/bin/bash
                  echo 'testing sh'
                  exit 0
              '''
          )
        }
      }
    }

  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.