Itai Ganot
Itai Ganot

Reputation: 6305

How to import groovy package/class into pipeline?

I'm writing a Jenkins shared library.

I'm not a coder myself and because of that I bump into many errors, which usually I don't know how to solve.

My shared library structure looks like so:

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

The pipeline file looks like so:

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

You can see in the pipeline file that I import "com.company.utils.Git", the git function file looks like so:

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

The Jenkinsfile looks like so:

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

When I run the job, it fails with the following error:

java.lang.NoSuchMethodError: No such DSL method 'getCommitter' found among steps [AddInteractivePromotion, ArtifactoryGradleBuild, ArtifactoryMavenBuild, ConanAddRemote, ConanAddUser, InitConanClient, MavenDescriptorStep, RunConanCommand, ansiColor, ansiblePlaybook, archive...

As far as I understand, I've imported the git package into the pipeline so I don't understand why this function is not recognized.

Another problem I have is that the pipeline only "looks" at the standardPipeline.groovy file at projectDir/vars and not under src/com/company/pipelines/standardPipeline.groovy ... I even tried removing the vars dir but the pipeline keeps looking there... why is that?

Any idea what I'm doing wrong?

Upvotes: 0

Views: 13169

Answers (1)

teakvinyl
teakvinyl

Reputation: 281

It looks like your line def committer = getCommitter() is what is calling the error because it's looking for a step named getCommitter(), instead of calling the Git class's method.

Referencing the documentation here, you should do something like this in the pipeline file:

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

Upvotes: 1

Related Questions