本文介绍了从git导入课程-无法解析课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个共享库,该库要具有类定义,例如:

 包com.org.pipeline.aws弦乐家族JsonObject taskDefinition类TaskDefinition {def getTaskDefinition(family){def jsonSlurper =新的groovy.json.JsonSlurper()def object = slurper.parseText(sh(returnStdout:是的,脚本:"aws ecs describe-task-definition --task-definition $ {family}"))返回断言对象实例的Map任务定义=对象[任务定义"]}} 

我正在通过一个单独的git存储库导入它

 库标识符:"jenkins-shared-libraries @ master",检索器:modernSCM([$ class:'GitSCMSource',远程:"ssh://[email protected]/smar/jenkins-shared-libraries.git",certificateId:"jenkins-bitbucket-ssh-private-key"])管道{代理任何阶段{stage('Build'){脚步 {脚本 {def z =新的com.org.pipeline.aws.TaskDefinition()withAWS(region:'ap-southeast-1',凭证:'awsId'){z.getTaskDefinition("web-taskdef")}}}}}} 

但是它一直给我这个错误:无法解析类com.org.pipeline.aws.TaskDefinition().知道为什么吗?

解决方案

动态加载共享库有点麻烦,因为:

(摘自Jenkins文档)也可以使用src/目录中的类,但是比较麻烦.@Library批注在编译之前会准备脚本的类路径",而在遇到库步骤时,脚本已经被编译.因此,您无法导入

但是您可以将您的方法设为静态并以这种方式从管道进行访问,如以下示例所示:

共享库文件夹结构如下:

 .├──src│────净│└──导师│──├──Math.groovy 

Math.groovy

 包net.samittutorialMath类实现Serializable {def管道数学(def管道){this.pipeline =管道}数学() {}def writeAndDisplayContent(int x,int y){pipeline.sh"回声$ {x + y}>$ {pipeline.env.WORKSPACE}/result.txt猫$ {pipeline.env.WORKSPACE}/result.txt"}静态def减(int x,int y){返回x-y}静态def add(int x,int y){返回x + y}静态def info(){返回来自数学类的问候"}} 

Jenkinsfile

  def d =库标识符:'jenkins-shared-libraries @ question/stackoverflow',检索器:modernSCM([$ class:'GitSCMSource',远程:'https://github.com/samitkumarpatel/jenkins-shared-libs.git'])net.samittutorial管道{代理任何阶段{stage('debug'){脚步 {脚本 {//信息println d.Math.info()//添加println d.Math.add(5,5)//writeAndDisplayContent(1,2)是非静态的,它将无法像d.Math(this).writeAndDisplayContent(1,2)一样工作}}}}} 

但是,如果您像此 Jenkins管道没有任何限制或限制,您可以像下面的示例一样继续进行,并在管道流中的任何需要的地方使用您的类.

共享库结构 Math.groovy 将与上面的示例相同

Jenkinsfile 如下:

  @Library('jenkins-shared-library')_导入net.samittutorial.Math管道{代理任何阶段{stage('debug'){脚步 {回声"Hello World"脚本 {def math =新的Math(this)println math.add(4,5)math.writeAndDisplayContent(1,2)}}}}} 

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://[email protected]/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?

解决方案

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

这篇关于从git导入课程-无法解析课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:03