Echo或sh方法在共享库的jenkins类中不起作用

问题描述 投票:0回答:1

我使用了放在共享库的/ vars /中的函数,它们工作正常。但是我现在需要使用类。为了学习它,我做了一些2个简单的类(父级和子级)来获取git信息。

/ src / ptx / shared / GitInfo.groovy

package ptx.shared

class GitInfo implements Serializable{
    public String commandMsg
    public String commandId
    public String commandAuthor
    public String msg
    public String id 
    public String author
    def steps

    public GitInfo(steps){
        this.steps = steps
        this.commandMsg = "git log --format=%B -n 1"
        this.commandId =  "git log -n 1 --pretty=format:'%h'"
        this.commandAuthor = "git log -n 1 --format=%ae"
        this.msg = this.runCommand(this.commandMsg)
        this.id = this.runCommand(this.commandId)
        this.author = this.runCommand(this.commandAuthor)
    }

    @NonCPS
    public runCommand(command){
    }
}

src / ptx / shared / LinuxGitInfo.groovy

package ptx.shared
//import ptx.shared.GitInfo

class LinuxGitInfo extends GitInfo {
    public LinuxGitInfo(steps){
        super(steps)
    }
    @NonCPS
    public runCommand(command){
        echo "something"
        //return this.steps.sh(script: command, returnStdout: true).trim()
    }
}

和我的管道

@Library('firstlibrary') _
import ptx.shared.*

pipeline{
    agent { label 'agents'}
    //libraries{
    //    lib('firstlibrary')
    //}
    environment{
        GIT_CREDS = credentials('gitlab')
    }
    stages{
        stage('git'){
            steps{
                git 'http://' + env.GIT_CREDS + '@10.11.22.112/root/pytest.git'
            }
        }
        stage('lib functions'){
            steps{
                script{
                    //first.mySh('pwd')
                    def gitinfo = new LinuxGitInfo(this)
                }
            }
        }
    }
}

但是我得到了这个

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: ptx.shared.LinuxGitInfo.echo() is applicable for argument types: (java.lang.String) values: [something]
Possible solutions: each(groovy.lang.Closure), getAt(java.lang.String), wait(), grep(), dump(), any()
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
...

与sh()相同。我缺少什么?

class jenkins inheritance shared-libraries
1个回答
0
投票

标有@NonCPS的方法不能使用内置步骤:https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md#technical-design

您可以在此处使用groovy命令println或删除注释。

© www.soinside.com 2019 - 2024. All rights reserved.