![](/img/trans.png)
[英]Using a shared library class from a custom step with Jenkins pipeline shared libraries
[英]Using another class from Jenkins Shared Pipeline
我目前正在使用 Jenkins 库,而我的工作没有问题。 现在我正在尝试做一些重构,有一大段代码可以确定 AWS 帐户用于我们目前在库中拥有的几乎所有工具。
我创建了以下文件“get account.groovy”
class GetAccount {
def getAccount(accountName) {
def awsAccount = "abcd"
return awsAccount;
}
}
然后我尝试从其他 groovy 脚本之一中执行此操作:
def getaccount = load 'getaccount.groovy'
def awsAccount = getaccount.getAccount(account)
但这不起作用,因为它正在当前工作目录而不是库目录中查找该文件
我无法弄清楚从已经在使用的库中调用另一个 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.
如果您遵循如下标准共享库结构,则可以这样做:
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')
}
在您的 Jenkinsfile(声明式或脚本式)中,您可以同时使用共享库 groovy class ,例如:
利用 aws.groovy
脚本化管道
node {
stage('deploy') {
aws()
}
}
声明性管道
pipeline {
agent any;
stages {
stage('deploy') {
steps {
aws()
}
}
}
}
利用 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')
}
}
声明性管道
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.