Wie import groovy package/Klasse in der pipeline?

Schreibe ich einen Jenkins gemeinsam genutzte Bibliothek.

Ich bin kein Programmierer, mich und deswegen treffe ich viele Fehler, die in der Regel ich weiß nicht, wie zu lösen.

My shared library-Struktur sieht so aus:

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

Den pipeline-Datei sieht so aus:

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
            }
        }
    }

Sehen Sie in der pipeline-Datei, die ich importieren "com.Unternehmen.utils.Git", das git-Funktion Datei sieht so aus:

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

Den Jenkinsfile sieht so aus:

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

Wenn ich den job ausführen, schlägt es fehl mit der folgenden Fehlermeldung:

java.lang.NoSuchMethodError: Keine solche DSL-Methode 'getCommitter' gefunden
unter-Schritten [AddInteractivePromotion, ArtifactoryGradleBuild,
ArtifactoryMavenBuild, ConanAddRemote, ConanAddUser, InitConanClient,
MavenDescriptorStep, RunConanCommand, ansiColor, ansiblePlaybook,
Archiv...

Soweit ich das verstanden habe, habe ich importiert, das git-Paket in der pipeline, so verstehe ich nicht, warum diese Funktion nicht erkannt.

Ein anderes problem das ich habe ist, dass die pipeline nur "sieht" an der standardPipeline.groovy Datei auf projectDir/vars und nicht unter src/com/company/pipelines/standardPipeline.groovy ... ich habe sogar versucht haben, entfernen die vars dir aber die pipeline hält es suchen... warum ist das so?

Irgendeine Idee, was ich falsch mache?

InformationsquelleAutor Itai Ganot | 2017-08-07
Schreibe einen Kommentar