1

I'm facing an issue when trying to implement shared library in my Jenkins servers.

The error I'm getting is around the following No such DSL method 'agent' found among steps

I have tried to remove the agent and just run on node, but still issue. I was following the following: https://jenkins.io/blog/2017/09/25/declarative-1/

could someone please point out where I'm be going wrong

vars/jenkinsJob.groovy

def call() {
    // Execute build pipeline job
    build_pipeline()
}

def build_pipeline() {
    agent {
       node {
          label params.SLAVE
      }
  }
        parameters {
            string(name: 'SETTINGS_CONFIG_FILE_NAME', defaultValue: 'maven.settings')
            string(name: 'SLAVE', defaultValue: 'new_slave')
        }

        environment {
            mvn = "docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8"
        }

        stages {
            stage('Inject Settings.xml File') {
                steps {
                    configFileProvider([configFile(fileId: "${env.SETTINGS_CONFIG_FILE_NAME}", targetLocation: "${env.WORKSPACE}")]) {
                    }
                }
            }
            stage('Clean') {
                steps {
                    sh "${mvn} clean"
                }
            }
            stage('Lint') {
                steps {
                    sh "${mvn} lint"
                }
            }
            stage('Build package and execute tests') {
                steps {
                    sh "${mvn} build"
                }
            }
        }

        post {
            always {
                archive "**/target/surefire-reports/*"
                junit '**/target/surefire-reports/*.xml'
                step([$class: 'JacocoPublisher'])
            }
        }
    }

Jenkinsfile

@Library('pipeline-library-demo') _

jenkinsJob.call()
user3292394
  • 609
  • 2
  • 11
  • 24

2 Answers2

1

All valid Declarative Pipelines must be enclosed within a pipeline block eg:

pipeline {
  /* insert Declarative Pipeline here */
  /* import libraries and call functions */
}
-1

The file jenkinsJob.groovy needs to have a single method only by the name:

def call(Map params[:]){
    // method body
}