Jenkins共享库中的Groovy作用域

时间:2019-09-04 07:13:41

标签: jenkins groovy jenkins-pipeline perforce

我目前正在将Jenkins管道从管道项目重构为多分支管道项目,并试图使某些功能更具模块化,并尝试将更多代码迁移到共享管道项目中。但是,这会导致产生与常规作用域范围相关的编译错误,该错误会在调用第三方库时尝试使用与函数相同的作用域,而不是使用全局作用域。

原始功能驻留在我脚本化的管道jenkinsfile中,如下所示:

def bootstrapPythonEnvironment( String client, String credentials, String label = "#head" ) {
        String bootstrapWorkspace = "${client}_BootstrapWorkspace"
        def p4 = p4(credential: credentials,
            workspace: manualSpec(
                charset: 'winansi',
                name: bootstrapWorkspace,
                spec: clientSpec(
                    view: "//MyGameContent/BuildScripts/Python/... //${bootstrapWorkspace}/BootstrapScripts/..." )
            ))
        p4.run('sync', "//MyGameContent/...", label)
}

但是,将其从Jenkins文件移至: vars / BuildTools.groovy 我通过以下方式致电: BuildTools.bootstrapJenkinsEnvironment( env.MyClient, 'CSSBuildmachine', '#head' ) 然后出现以下错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static BuildTools.bootstrapPythonEnvironment() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [jenkins-Stekdatorn-TestWorkspace-null-0, CSSBuildmachine, ...]
Possible solutions: bootstrapPythonEnvironment(java.lang.String, java.lang.String, java.lang.String), bootstrapPythonEnvironment(java.lang.String, java.lang.String)

因此,我将函数声明更改为包含静态: static def bootstrapPythonEnvironment( String client, String credentials, String label = "#head" ) 这会有所帮助,但是现在我得到的错误是:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static BuildTools.clientSpec() is applicable for argument types: (java.util.LinkedHashMap) values: [[view://MyGameContent/BuildScripts/Python/... //jenkins-Stekdatorn-TestWorkspace-null-0_BootstrapWorkspace/BootstrapScripts/...]]
    at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1501)
    at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1487)
    at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
    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 BuildTools.bootstrapPythonEnvironment(BuildTools.groovy:11)
    at WorkflowScript.run(WorkflowScript:18)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:84)
    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.GeneratedMethodAccessor324.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.GeneratedMethodAccessor320.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.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:107)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor324.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.ContinuationGroup.methodCall(ContinuationGroup.java:87)
    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.GeneratedMethodAccessor324.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.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.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.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:66)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.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)

我尝试将文件移动到 src / com / coffeestain / build / Python.groovy 并将其放在函数中:

class Python {
    static def bootstrap( String client, String credentials, String label = "#head" ) {
        // Snipped out code
    }
}

但是当我尝试通过以下方式导入它时,它只会给我类似的消息:

@Library('MyGameBuildtools') import com.coffeestain.build.*

并调用:

Python.bootstrap( env.P4CLIENT, 'CSSBuildmachine', "#head" )

但是,这以另一种形式给了我同样的错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static com.coffeestain.build.Python.clientSpec() is applicable for argument types: (java.util.LinkedHashMap) values: [[view://MyGameContent/BuildScripts/Python/... //jenkins-Stekdatorn-TestWorkspace-null-0_BootstrapWorkspace/BootstrapScripts/...]]

我当前的解决方法是将其添加到文件 vars / bootstrapPythonEnvironment.groovy 中,定义为:

def call( String client, String credentials, String label = "#head" ) {
    // Snipped out code
}

但是从长远来看,为我添加的每个功能都拥有一个文件是不可行的。

1 个答案:

答案 0 :(得分:0)

感谢Dominik Gebhart帮助我解决了这个问题!

clientSpec和P4来自P4 Plugin

我通过将函数更改为此来解决它,请注意如何使用上下文:

unsafe

我通过以下方式呼叫它:

if (navigator.mediaDevices) {
  // access the web cam
  navigator.mediaDevices.getUserMedia({
   video: {
    width: { 
     min: 1280,
    },
    height: {
     min: 720,
    },
    facingMode: {
     exact: 'environment'
    }
   }
  }).then(function(stream) {
      video.srcObject = stream;
      video.addEventListener('click', takeSnapshot);
  })
  .catch(function(error) {
      document.body.textContent = 'Could not access the camera. Error: ' + error.name;
  });
}


var video = document.querySelector('video'), canvas;

function takeSnapshot(){
  var img =  document.createElement('img');
  var context;
  var width = video.offsetWidth, height = video.offsetHeight;
  var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height; 
    context = canvas.getContext('2d');          
    context.webkitImageSmoothingEnabled = false;
    context.mozImageSmoothingEnabled = false;
    context.imageSmoothingEnabled = false;
    context.drawImage(video, 0,  0, width,  height);    
    img.src = canvas.toDataURL('image/jpeg');
}