问题
I am constructing a Shared Library which I want to have class definitions, such as:
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"]
}
}
And I am importing it via a separate git repository
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")
}
}
}
}
}
}
But it keeps giving me this error: unable to resolve class com.org.pipeline.aws.TaskDefinition()
. Any idea why?
回答1:
Loading Shared library dynamically is a bit tricky to make your expected class available during the class loader before compilation of the pipeline, because of :
(copied from Jenkins documentation) Using classes from the src/ directory is also possible, but trickier. Whereas the @Library annotation prepares the “classpath” of the script prior to compilation, by the time a library step is encountered the script has already been compiled. Therefore you cannot import
But you can make your method static and access from the pipeline this way, like the below example:
The shared library folder structure looks like:
.
├── 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"
}
}
Jenkinsfile
def d = library identifier: 'jenkins-shared-libraries@question/stackoverflow', 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)
}
}
}
}
}
But If you load your library like this Jenkins pipeline will have no restriction or limitation, you can go ahead like below example and use your class where ever you want on your pipeline flow.
Shared Library structure and Math.groovy will remain the same like above example
The Jenkinsfile will look like:
@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)
}
}
}
}
}
来源:https://stackoverflow.com/questions/63948975/importing-class-from-git-unable-to-resolve-class