LynxZh
LynxZh

Reputation: 835

Pipeline syntax not recognized in PR build

I have a Jenkins pipeline for my github org using the shared library. The library works well for normal build when manually trigger or commit event. Library code like below:

vars/MyPipeline.groovy

call(body) {
  def usercfg = [:]
  body.resolveStrategy = Closure.DELEGATE_FIRST
  body.delegate = usercfg
  body()

  if (env.BRANCH_NAME ==~ /^PR-\d+$/) {
    pipeline {
      agent any
      tools "JDK8"
      stages {
        stage("build jar") {
          ....
        }
      }
    }
  } else {
    pipeline {
      agent any
      tools "JDK8"
      stages {
        stage("Initialize") {
          ...
        }
        stage("Build Complete pipeline") {
          ...
        }
      }
    }
  }
}

And the Jenkinsfile in my repository:

@Library('MySharedLibrary') _
MyPipeline {
  myconfig = [ build: true ]
}

The body contains some customized configurations.

When non-PR build happens, it runs through correctly, no issues.

When PR build happens, it goes into the first section and it throw out the "agent" is not among the steps.

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps

I can't remember if this PR builder works as this library exists for a certain amount of time. And this should not be an plugin version problem as you can see the main pipeline goes through correctly.

Upvotes: 0

Views: 314

Answers (1)

LynxZh
LynxZh

Reputation: 835

I just figure out the problem myself. It turns out to be a problem when refactory the pipeline in the PR build section which miss the stages.

pipeline {
  stage (" do some stuff") {
  }
}

so after wrap the stage with stages. It works fine. It seems to be the error message is confusing. May be the DSL parser is giving a wrong information.

Upvotes: 1

Related Questions