【问题标题】:Declarative Pipeline shared library声明式管道共享库
【发布时间】:2018-12-18 13:48:41
【问题描述】:

尝试在我的 Jenkins 服务器中实现共享库时遇到问题。

我遇到的错误如下 No such DSL method 'agent' found among steps

我已尝试删除代理并仅在节点上运行,但仍然存在问题。 我正在关注以下内容:https://jenkins.io/blog/2017/09/25/declarative-1/

谁能指出我哪里出错了

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()

【问题讨论】:

标签: jenkins shared-libraries jenkins-pipeline


【解决方案1】:

所有有效的声明式管道都必须包含在管道块中 例如:

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

【讨论】:

    【解决方案2】:

    文件 jenkinsJob.groovy 只需要一个方法名即可:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2019-06-01
      • 2017-10-07
      • 2017-12-03
      相关资源
      最近更新 更多