0

I am currently using a Jenkins library without issues from my jobs. Right now I am trying to do some refactor, there is a chunk of code to determine with AWS account to use in almost every tool we currently have in the library.

I created the following file "get account.groovy"

class GetAccount {
    def getAccount(accountName) {
        def awsAccount = "abcd" 
        return awsAccount;
    }
}

Then I am trying to do this from within one of the other groovy scripts:

def getaccount = load 'getaccount.groovy'


def awsAccount = getaccount.getAccount(account)

But that does not work since it is looking for that file in the current work directory not in the library directory

I am unable to figure out what the best way to call another class from within a library that is already being used.

Jon Heckman
  • 420
  • 2
  • 7
  • 18
  • Hope this can answer your question: https://stackoverflow.com/questions/9136328/including-a-groovy-script-in-another-groovy – Sourav Dec 12 '20 at 09:52
  • I had tried going down that path. The problem with that, in Jenkins it is not obvious where the library is cloned too. The working directory is not the library but it is the repo I am actively using. The best I can find is that the library is cloned too a directory based on the build number but that seems error prone to attempt to figure out on the fly. – Jon Heckman Dec 14 '20 at 14:14
  • please Share your shared library folder structure. If you are using the way Jenkins asked to use the folder structure for shared library, you no need to load DSL to call a groovy, you can directly use the method by referencing the fileName – Samit Kumar Patel Feb 07 '21 at 11:09

1 Answers1

1

Jenkins load DSL is meant to load an externalize groovy file that is available in the job workspace and it will not work if you try to load a groovy script available in Jenkins shared library, because the shared library never checkout in the job Workspace.

If you follow the standard shared library structure like below, it could be done like :

shared-library
├── src
│   └── org
│       └── any
│           └── GetAccount.groovy
└── vars
    └── aws.groovy

GetAccount.groovy

package org.any
class GetAccount {
  def getAccount(accountName) {
     def awsAccount = "abcd" 
     return awsAccount;
  }
} 

aws.groovy

import org.any;
def call() {
   def x = new GetAccount()
   // make use of val and proceed with your further logic
   def val = x.getAccount('xyz')
}

In your Jenkinsfile (declarative or scripted ) you can use both the shared library groovy class like :

make use of aws.groovy

scripted pipeline

node {
  stage('deploy') {
    aws()
  }
}

declarative pipeline

pipeline {
  agent any;
  stages {
    stage('deploy') {
      steps {
         aws()
      }
    }
  }
}

make use of GetAccount.groovy scripted pipeline

import org.any
node {
  stage('deploy') {
    def x = new GetAccount()
    // make use of val and proceed with your further logic
    def val = x.getAccount('xyz')
  }
}

declarative pipeline

import org.any
pipeline {
  agent any;
  stages {
    stage('deploy') {
      steps {
         script {
            def x = new GetAccount()
            // make use of val and proceed with your further logic
            def val = x.getAccount('xyz')
         }
      }
    }
  }
}
Samit Kumar Patel
  • 1,932
  • 1
  • 13
  • 20