如何将groovy包/类导入管道?

时间:2017-08-07 11:42:44

标签: jenkins groovy jenkins-pipeline

我正在写一个Jenkins共享库。

我自己不是编码员,因此我遇到了很多错误,通常我不知道如何解决。

我的共享库结构如下:

itai@Itais-MBP ~/src/company/pipeline_utils -  (master) $ tree -f
.
├── ./1
├── ./README.md
├── ./functions.groovy
├── ./src
│   └── ./src/com
│       ├── ./src/com/company
│       │   ├── ./src/com/company/pipelines
│       │   │   └── ./src/com/company/pipelines/standardPipeline.groovy
│       │   └── ./src/com/company/utils
│       │       ├── ./src/com/company/utils/Git.groovy
│       │       ├── ./src/com/company/utils/SlackPostBuild.groovy
│       │       ├── ./src/com/company/utils/dockerBuild.groovy
│       │       ├── ./src/com/company/utils/dockerTest.groovy
│       │       ├── ./src/com/company/utils/notifyEmail.groovy
│       │       ├── ./src/com/company/utils/notifySlack.groovy
│       │       ├── ./src/com/company/utils/pipeline.groovy
│       │       └── ./src/com/company/utils/pipelineFunctions.groovy
│       └── ./src/com/company-in-idea
├── ./test_utils.groovy
├── ./utils.groovy
└── ./vars
    ├── ./vars/standardPipeline.groovy
    └── ./vars/utils.groovy

管道文件如下所示:

itai@Itais-MBP ~/src/company/pipeline_utils -  (master) $ cat ./vars/standardPipeline.groovy
import com.company.utils.Git;

 def call(body) {

        def config = [:]
        body.resolveStrategy = Closure.DELEGATE_FIRST
        body.delegate = config
        body()

        node {
            // Clean workspace before doing anything
            deleteDir()

            try {
                stage ('Clone') {
                    checkout scm
                                        def committer = getCommitter()
                }
                stage ('Build') {
                    sh "echo 'building ${config.projectName} ...'"
                }
                stage ('Tests') {
                    parallel 'static': {
                        sh "echo 'shell scripts to run static tests...'"
                    },
                    'unit': {
                        sh "echo 'shell scripts to run unit tests...'"
                    },
                    'integration': {
                        sh "echo 'shell scripts to run integration tests...'"
                    }
                }
                stage ('Deploy') {
                    sh "echo 'deploying to server ${config.serverDomain}...'"
                    sh "echo Itai ganot"
                    sh "echo Itai"
                }
            } catch (err) {
                currentBuild.result = 'FAILED'
                throw err
            }
        }
    }

您可以在管道文件中看到我导入“com.company.utils.Git”,git函数文件如下所示:

itai@Itais-MBP ~/src/company/pipeline_utils -  (master) $ cat ./src/com/company/utils/Git.groovy
#!/usr/bin/env groovy
package com.company.utils;

    def sh_out(command) {
    sh(returnStdout: true, script: command).trim()
  }

    def getCommitter(){
        node {
        committer = this.sh_out('git show -s --format=\'%ce\' | tr -d "\'" | cut -d@ -f1')
        return committer
        }
    }

    def getRepo(){
        node {
            reponame = this.sh_out('basename $(git remote show -n origin | grep Push | cut -d: -f2- | rev | cut -c5- | rev)')
            return reponame
        }
    }

    void gitClean(){
        node {
            this.sh_out('''
                sudo chown -R ubuntu:ubuntu .
                if [ -d ".git" ]; then
                    sudo git reset --hard &>/dev/null
                    sudo git clean -fxd &>/dev/null
                    sudo git tag -d $(git tag) &>/dev/null
                fi
            || true ''')
        }
    }
return this

Jenkinsfile看起来像这样:

@Library("company") _
standardPipeline {
    projectName = "Project1"
    serverDomain = "Project1 Server Domain"
}

当我运行作业时,它失败并出现以下错误:

  

java.lang.NoSuchMethodError:找不到这样的DSL方法'getCommitter'   步骤之间[AddInteractivePromotion,ArtifactoryGradleBuild,   ArtifactoryMavenBuild,ConanAddRemote,ConanAddUser,InitConanClient,   MavenDescriptorStep,RunConanCommand,ansiColor,ansiblePlaybook,   存档...

据我了解,我已将git包导入管道中,因此我不明白为什么无法识别此功能。

我遇到的另一个问题是,管道只会“查看”standardPipeline.groovy处的文件projectDir/vars而不是src/com/company/pipelines/standardPipeline.groovy ...我甚至尝试删除vars dir但是管道一直在那里寻找......为什么会这样?

知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

看起来您的行def committer = getCommitter()正在调用错误,因为它正在寻找名为getCommitter()的步骤,而不是调用Git类的方法。

Referencing the documentation here,您应该在管道文件中执行以下操作:

def gitUtil = new Git()
def committer = gitUtil.getCommitter()