2017-08-07 24 views
0

我正在寫一個Jenkins共享庫。如何將groovy包/類導入流水線?

我不是自己的編碼員,因此我遇到了許多錯誤,通常我不知道如何解決。

我的共享庫結構看起來像這樣:

[email protected] ~/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 

管道文件看起來像這樣:

[email protected] ~/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功能文件看起來像這樣:

[email protected] ~/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 [email protected] -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: No such DSL method 'getCommitter' found among steps [AddInteractivePromotion, ArtifactoryGradleBuild, ArtifactoryMavenBuild, ConanAddRemote, ConanAddUser, InitConanClient, MavenDescriptorStep, RunConanCommand, ansiColor, ansiblePlaybook, archive...

據我瞭解,我已經進口的git包入管道,所以我不明白爲什麼此功能無法識別。

還有一個問題我是唯一的管道「看起來」在standardPipeline.groovy文件在projectDir/vars,而不是根據src/com/company/pipelines/standardPipeline.groovy ......我甚至試圖消除瓦爾目錄,但管道繼續尋找有......這是爲什麼?

任何想法我做錯了什麼?

回答

1

看起來您的行def committer = getCommitter()是調用錯誤的原因,因爲它正在尋找名爲getCommitter()的步驟,而不是調用Git類的方法。

Referencing the documentation here,你應該做這樣的事情在管線檔案:

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

好人!非常感謝。 –