我使用了放在共享库的/ 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()相同。我缺少什么?
标有@NonCPS的方法不能使用内置步骤:https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md#technical-design
您可以在此处使用groovy命令println或删除注释。