从 git 导入 class - 无法解析 class
Importing class from git - unable to resolve class
我正在构建一个共享库,我希望它有 class 个定义,例如:
package com.org.pipeline.aws
String family
JsonObject taskDefinition
class TaskDefinition {
def getTaskDefinition(family) {
def jsonSlurper = new groovy.json.JsonSlurper()
def object = slurper.parseText(
sh(returnStdout: true,
script: "aws ecs describe-task-definition --task-definition ${family}"
)
)
return assert object instanceof Map
taskDefinition = object["taskDefinition"]
}
}
我正在通过单独的 git 存储库导入它
library identifier: 'jenkins-shared-libraries@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'ssh://git@bitbucket.org.net/smar/jenkins-shared-libraries.git',
credentialsId: 'jenkins-bitbucket-ssh-private-key'])
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
def z = new com.org.pipeline.aws.TaskDefinition()
withAWS(region: 'ap-southeast-1', credentials: 'awsId') {
z.getTaskDefinition("web-taskdef")
}
}
}
}
}
}
但它一直给我这个错误:unable to resolve class com.org.pipeline.aws.TaskDefinition()
。知道为什么吗?
Loading Shared library dynamically 有点棘手,因为 :
(从 Jenkins 文档复制)
使用 src/ 目录中的 classes 也是可能的,但更棘手。尽管 @Library 注释在编译之前准备好脚本的“classpath”,但在遇到库步骤时脚本已经编译完毕。因此你不能导入
但是您可以使您的方法静态化并通过这种方式从管道访问,如下例所示:
共享库文件夹结构如下:
.
├── src
│ └── net
│ └── samittutorial
│ ├── Math.groovy
Math.groovy
package net.samittutorial
class Math implements Serializable {
def pipeline
Math(def pipeline) {
this.pipeline = pipeline
}
Math() {
}
def writeAndDisplayContent(int x, int y) {
pipeline.sh """
echo ${x+y} > ${pipeline.env.WORKSPACE}/result.txt
cat ${pipeline.env.WORKSPACE}/result.txt
"""
}
static def substract(int x, int y) {
return x - y
}
static def add(int x, int y) {
return x + y
}
static def info() {
return "Hello from Math class"
}
}
詹金斯文件
def d = library identifier: 'jenkins-shared-libraries@question/Whosebug', retriever: modernSCM([
$class: 'GitSCMSource',
remote: 'https://github.com/samitkumarpatel/jenkins-shared-libs.git'
]) net.samittutorial
pipeline {
agent any
stages {
stage('debug') {
steps {
script {
//info
println d.Math.info()
//add
println d.Math.add(5,5)
// writeAndDisplayContent(1,2) is non static , it will not work like d.Math(this).writeAndDisplayContent(1,2)
}
}
}
}
}
但是如果你像 this 一样加载你的库,Jenkins 管道将没有任何限制或局限,你可以像下面的例子一样继续并在你的管道流中任何你想要的地方使用你的 class。
共享库结构和Math.groovy将与上面的示例保持相同
Jenkinsfile 看起来像:
@Library('jenkins-shared-library') _
import net.samittutorial.Math
pipeline {
agent any;
stages {
stage('debug') {
steps {
echo "Hello World"
script {
def math = new Math(this)
println math.add(4,5)
math.writeAndDisplayContent(1,2)
}
}
}
}
}
我正在构建一个共享库,我希望它有 class 个定义,例如:
package com.org.pipeline.aws
String family
JsonObject taskDefinition
class TaskDefinition {
def getTaskDefinition(family) {
def jsonSlurper = new groovy.json.JsonSlurper()
def object = slurper.parseText(
sh(returnStdout: true,
script: "aws ecs describe-task-definition --task-definition ${family}"
)
)
return assert object instanceof Map
taskDefinition = object["taskDefinition"]
}
}
我正在通过单独的 git 存储库导入它
library identifier: 'jenkins-shared-libraries@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'ssh://git@bitbucket.org.net/smar/jenkins-shared-libraries.git',
credentialsId: 'jenkins-bitbucket-ssh-private-key'])
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
def z = new com.org.pipeline.aws.TaskDefinition()
withAWS(region: 'ap-southeast-1', credentials: 'awsId') {
z.getTaskDefinition("web-taskdef")
}
}
}
}
}
}
但它一直给我这个错误:unable to resolve class com.org.pipeline.aws.TaskDefinition()
。知道为什么吗?
Loading Shared library dynamically 有点棘手,因为 :
(从 Jenkins 文档复制) 使用 src/ 目录中的 classes 也是可能的,但更棘手。尽管 @Library 注释在编译之前准备好脚本的“classpath”,但在遇到库步骤时脚本已经编译完毕。因此你不能导入
但是您可以使您的方法静态化并通过这种方式从管道访问,如下例所示:
共享库文件夹结构如下:
.
├── src
│ └── net
│ └── samittutorial
│ ├── Math.groovy
Math.groovy
package net.samittutorial
class Math implements Serializable {
def pipeline
Math(def pipeline) {
this.pipeline = pipeline
}
Math() {
}
def writeAndDisplayContent(int x, int y) {
pipeline.sh """
echo ${x+y} > ${pipeline.env.WORKSPACE}/result.txt
cat ${pipeline.env.WORKSPACE}/result.txt
"""
}
static def substract(int x, int y) {
return x - y
}
static def add(int x, int y) {
return x + y
}
static def info() {
return "Hello from Math class"
}
}
詹金斯文件
def d = library identifier: 'jenkins-shared-libraries@question/Whosebug', retriever: modernSCM([
$class: 'GitSCMSource',
remote: 'https://github.com/samitkumarpatel/jenkins-shared-libs.git'
]) net.samittutorial
pipeline {
agent any
stages {
stage('debug') {
steps {
script {
//info
println d.Math.info()
//add
println d.Math.add(5,5)
// writeAndDisplayContent(1,2) is non static , it will not work like d.Math(this).writeAndDisplayContent(1,2)
}
}
}
}
}
但是如果你像 this 一样加载你的库,Jenkins 管道将没有任何限制或局限,你可以像下面的例子一样继续并在你的管道流中任何你想要的地方使用你的 class。
共享库结构和Math.groovy将与上面的示例保持相同
Jenkinsfile 看起来像:
@Library('jenkins-shared-library') _
import net.samittutorial.Math
pipeline {
agent any;
stages {
stage('debug') {
steps {
echo "Hello World"
script {
def math = new Math(this)
println math.add(4,5)
math.writeAndDisplayContent(1,2)
}
}
}
}
}