Jenkins Artifactory 插件 - 从共享库上传
Jenkins Artifactory plugin - upload from shared library
我使用脚本管道中的 Jenkins Artifactory 插件将工件上传到 Artifactory。管道代码是这样的:
def uploadSpec = ...
def artifactory = Artifactory.server 'myTag'
def buildInfo = server.upload(JsonOutput.toJson(uploadSpec))
server.publishBuildInfo buildInfo
这一直运行良好,但是在许多其他代码中,它已在多个项目中重复。我现在正在提取所有公共代码并将其放入 Jenkins 共享库中。我让其他所有东西都能工作,但是我正在努力让人工制品工作。我已经尝试过各种不同错误的事情。我最后一次尝试是这样的:
def server = getArtifactoryServer serverTag
server.upload(JsonOutput.toJson(myUploadSpec))
这导致 NullPointerException
,我(在插件源代码中)追踪到 cpsScript
变量,该变量未设置。我尝试使用定义的 upload
步骤 here:
def buildInfo = artifactoryUpload(
spec: myUploadSpec,
buildInfo: new BuildInfo(),
module: null,
failNoOp: false,
server: server
)
然而,这会导致不同的错误:
WARNING: Unknown parameter(s) found for class type 'org.jfrog.hudson.pipeline.scripted.steps.UploadStep': module
org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class java.lang.String
at org.kohsuke.stapler.ClassDescriptor.loadConstructorParamNames(ClassDescriptor.java:265)
at org.jenkinsci.plugins.structs.describable.DescribableModel.<init>(DescribableModel.java:153)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:474)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
Caused: java.lang.IllegalArgumentException: Could not instantiate {spec={files=[{pattern=Vertx365/Production/build/vertx365-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertx365/vertx365-production/3.37.0/}, {pattern=Vertx365/QA/build/vertx365-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertx365/vertx365-qa/3.37.0/}, {pattern=VertxBCSS/Production/build/vertxbcss-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbcss/vertxbcss-production/3.37.0/}, {pattern=VertxBCSS/QA/build/vertxbcss-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbcss/vertxbcss-qa/3.37.0/}, {pattern=VertxBIM/Production/build/vertxbim-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbim/vertxbim-production/3.37.0/}, {pattern=VertxBIM/QA/build/vertxbim-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbim/vertxbim-qa/3.37.0/}, {pattern=VertxCatering/QA/build/vertxcatering-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxcatering/vertxcatering-qa/3.37.0/}, {pattern=VertxLockhart/Production/build/vertxlockhart-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxlockhart/vertxlockhart-production/3.37.0/}, {pattern=VertxLockhart/QA/build/vertxlockhart-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxlockhart/vertxlockhart-qa/3.37.0/}]}, buildInfo=org.jfrog.hudson.pipeline.common.types.buildInfo.BuildInfo@caaf82, failNoOp=false, module=null, server=org.jfrog.hudson.pipeline.common.types.ArtifactoryServer@1e1cacd} for org.jfrog.hudson.pipeline.scripted.steps.UploadStep
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
at com.bunzl.pipeline.artifactory.Artifactory.uploadFiles(Artifactory.groovy:10)
at com.bunzl.pipeline.artifactory.Artifactory.uploadFiles(Artifactory.groovy)
at WorkflowScript.run(WorkflowScript:117)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
at sun.reflect.GeneratedMethodAccessor143.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当然,我可以在主管道中检索 ArtifactoryServer
对象,然后将其传递到共享库调用中,但这是一个错误的设计。
那么,我怎样才能在共享库中实现我需要的东西(i.e.upload 工件到具有指定标签的服务器)?
关于如何让它工作的任何想法?
编辑: 根据评论中的请求,我有这个结构(对于 class 调用人工制品):
+
|
+- src
|
+- com
|
+- mycompany
|
+- mypackage
|
+- Artifactory.groovy
这是 class 中的代码(用 private/proprietary 位替换):
package com.mycompany.mypackage
import groovy.json.JsonOutput
def uploadFiles(String serverTag, List files, boolean buildInfoPublish = true) {
def server = getArtifactoryServer serverTag
def buildInfo = server.upload(JsonOutput.toJson([files: files]))
if (buildInfoPublish) {
publishBuildInfo buildInfo: buildInfo, server: server
}
}
该库在 Jenkins 中定义为名为 config-library
的全局库,并且 used/called 如下所示:
def configLibrary = library('config-library@my-version')
def artifactory = configLibrary.com.mycompany.mypackage.Artifactory.new()
def files = ...
artifactory.uploadFiles('myTag', files)
调试本地安装的插件后终于弄明白了。显然,artifactoryUpload
期望 spec
参数是一个 JSON 字符串,而不是一个对象。所以,解决方法很简单:
def server = getArtifactoryServer serverTag
def buildInfo = artifactoryUpload(
spec: JsonOutput.toJson(myUploadSpec),
buildInfo: new BuildInfo(),
module: null,
failNoOp: false,
server: server
)
我使用脚本管道中的 Jenkins Artifactory 插件将工件上传到 Artifactory。管道代码是这样的:
def uploadSpec = ...
def artifactory = Artifactory.server 'myTag'
def buildInfo = server.upload(JsonOutput.toJson(uploadSpec))
server.publishBuildInfo buildInfo
这一直运行良好,但是在许多其他代码中,它已在多个项目中重复。我现在正在提取所有公共代码并将其放入 Jenkins 共享库中。我让其他所有东西都能工作,但是我正在努力让人工制品工作。我已经尝试过各种不同错误的事情。我最后一次尝试是这样的:
def server = getArtifactoryServer serverTag
server.upload(JsonOutput.toJson(myUploadSpec))
这导致 NullPointerException
,我(在插件源代码中)追踪到 cpsScript
变量,该变量未设置。我尝试使用定义的 upload
步骤 here:
def buildInfo = artifactoryUpload(
spec: myUploadSpec,
buildInfo: new BuildInfo(),
module: null,
failNoOp: false,
server: server
)
然而,这会导致不同的错误:
WARNING: Unknown parameter(s) found for class type 'org.jfrog.hudson.pipeline.scripted.steps.UploadStep': module
org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class java.lang.String
at org.kohsuke.stapler.ClassDescriptor.loadConstructorParamNames(ClassDescriptor.java:265)
at org.jenkinsci.plugins.structs.describable.DescribableModel.<init>(DescribableModel.java:153)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:474)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
Caused: java.lang.IllegalArgumentException: Could not instantiate {spec={files=[{pattern=Vertx365/Production/build/vertx365-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertx365/vertx365-production/3.37.0/}, {pattern=Vertx365/QA/build/vertx365-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertx365/vertx365-qa/3.37.0/}, {pattern=VertxBCSS/Production/build/vertxbcss-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbcss/vertxbcss-production/3.37.0/}, {pattern=VertxBCSS/QA/build/vertxbcss-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbcss/vertxbcss-qa/3.37.0/}, {pattern=VertxBIM/Production/build/vertxbim-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbim/vertxbim-production/3.37.0/}, {pattern=VertxBIM/QA/build/vertxbim-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxbim/vertxbim-qa/3.37.0/}, {pattern=VertxCatering/QA/build/vertxcatering-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxcatering/vertxcatering-qa/3.37.0/}, {pattern=VertxLockhart/Production/build/vertxlockhart-production-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxlockhart/vertxlockhart-production/3.37.0/}, {pattern=VertxLockhart/QA/build/vertxlockhart-qa-3.37.0.zip, target=vertx-release/com/bunzl/vertx/release/vertxlockhart/vertxlockhart-qa/3.37.0/}]}, buildInfo=org.jfrog.hudson.pipeline.common.types.buildInfo.BuildInfo@caaf82, failNoOp=false, module=null, server=org.jfrog.hudson.pipeline.common.types.ArtifactoryServer@1e1cacd} for org.jfrog.hudson.pipeline.scripted.steps.UploadStep
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
at com.bunzl.pipeline.artifactory.Artifactory.uploadFiles(Artifactory.groovy:10)
at com.bunzl.pipeline.artifactory.Artifactory.uploadFiles(Artifactory.groovy)
at WorkflowScript.run(WorkflowScript:117)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
at sun.reflect.GeneratedMethodAccessor143.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当然,我可以在主管道中检索 ArtifactoryServer
对象,然后将其传递到共享库调用中,但这是一个错误的设计。
那么,我怎样才能在共享库中实现我需要的东西(i.e.upload 工件到具有指定标签的服务器)?
关于如何让它工作的任何想法?
编辑: 根据评论中的请求,我有这个结构(对于 class 调用人工制品):
+
|
+- src
|
+- com
|
+- mycompany
|
+- mypackage
|
+- Artifactory.groovy
这是 class 中的代码(用 private/proprietary 位替换):
package com.mycompany.mypackage
import groovy.json.JsonOutput
def uploadFiles(String serverTag, List files, boolean buildInfoPublish = true) {
def server = getArtifactoryServer serverTag
def buildInfo = server.upload(JsonOutput.toJson([files: files]))
if (buildInfoPublish) {
publishBuildInfo buildInfo: buildInfo, server: server
}
}
该库在 Jenkins 中定义为名为 config-library
的全局库,并且 used/called 如下所示:
def configLibrary = library('config-library@my-version')
def artifactory = configLibrary.com.mycompany.mypackage.Artifactory.new()
def files = ...
artifactory.uploadFiles('myTag', files)
调试本地安装的插件后终于弄明白了。显然,artifactoryUpload
期望 spec
参数是一个 JSON 字符串,而不是一个对象。所以,解决方法很简单:
def server = getArtifactoryServer serverTag
def buildInfo = artifactoryUpload(
spec: JsonOutput.toJson(myUploadSpec),
buildInfo: new BuildInfo(),
module: null,
failNoOp: false,
server: server
)