简体   繁体   English

使用 Jenkins 共享管道中的另一个 class

[英]Using another class from Jenkins Shared Pipeline

I am currently using a Jenkins library without issues from my jobs.我目前正在使用 Jenkins 库,而我的工作没有问题。 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.现在我正在尝试做一些重构,有一大段代码可以确定 AWS 帐户用于我们目前在库中拥有的几乎所有工具。

I created the following file "get account.groovy"我创建了以下文件“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:然后我尝试从其他 groovy 脚本之一中执行此操作:

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.我无法弄清楚从已经在使用的库中调用另一个 class 的最佳方法是什么。

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. 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 GetAccount.groovy

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

aws.groovy 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:在您的 Jenkinsfile(声明式或脚本式)中,您可以同时使用共享库 groovy class ,例如:

make use of aws.groovy利用 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利用 GetAccount.groovy脚本化管道

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM