2017-05-11 34 views
0

私たちはjenkinsパイプラインに切り替えようとしますが、Groovy/Javaの低レベルは共有ライブラリを設定するときに私たちを止めます。ここでJenkinsパイプライン共有ライブラリの設定

私の共有ライブラリ(ビットバケットでのgitリポジトリ内で{ルート} /src/com/pipeline.groovy。)私は、パッケージの定義については、ここでやっているどのような手掛かりを認めていないする必要が

package com.pipeline // not sure about the package definition here 

class Utils implements Serializable { // seems I need to serialize but not sure :(

    /** 
    * Execute common steps for the clean up of the workspace before the job really starts 
    */ 
    static def preBuildCleanUp() { 
     sh(script: "git clean -xdf && git remote prune origin && find . -name '*.pyc' -delete") 

    } 
    /** 
    * commit ID is not exposed to the jenkins env in pipeline (see https://issues.jenkins-ci.org/browse/JENKINS-26100) 
    * 
    * These should all be performed at the point where you've 
    * checked out your sources on the agent. A 'git' executable 
    * must be available. 
    * Most typical, if you're not cloning into a sub directory 
    * 
    * @return String short SHA 
    */ 
    static def getShortCommitID() { 
     gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim() 
     // short SHA, possibly better for chat notifications, etc. 
     return gitCommit.take(6) 
    } 

    /** 
    * Run a pip to install the package locally to the user. 
    * @param String packageName name of the pip package to install 
    */ 
    static def pipInstall(packageName) { 
     sh(script: "python -m pip.__main__ install --user --quiet $packageName") 
    } 
    /** 
    * Build a virtualenv at env.PYENV_HOME location. 
    */ 
    static def buildVirtualEnv() { 
     pipInstall("virtualenv") 
     sh(script: "python -m virtualenv --no-site-packages ${PYENV_HOME} || exit 1") 
    } 

    /** 
    * Return the list of environment variables common to all jenkins jobs 
    * @set ${WORKSPACE} 
    * @return List 
    */ 
    static def getCommonEnv() { 
     return ["WORKSPACE=${pwd()}"] 
    } 

    /** 
    * Return the list of environment variables common to all python jobs 
    * @set ${PYENV_HOME} 
    * @set ${PYTHONDONTWRITEBYTECODE} 
    * @extend ${PYTHONPATH} 
    * @return List 
    */ 
    static def getPythonEnv() { 
     return [ 
       "PYENV_HOME=$WORKSPACE/.pyenv/", 
       "PYTHONDONTWRITEBYTECODE=1", 
       "PYTHONPATH=/rdo/rodeo/setup/lib/pure:$PYTHONPATH" 
     ] 
    } 

    /** 
    * Run the test using py.test inside a virtual env. 
    */ 
    static def runTestsWithPyTest() { 
     sh(script: "source ${PYENV_HOME}/bin/activate || exit 1 && /rdo/rodeo/setup/jenkins_scripts/jenkins_pytest.bash ${TEST_FOLDER} ${COVER_FOLDER}") 
    } 

    /** 
    * Send a report to the commiter about the state of the tests 
    * 
    * @param Int returnCode return code to use to build the slack message. 
    */ 
    static def sendSlackNotification(returnCode) { 
     shortCommitID = getShortCommitID() 
     sh(script: "export RC=$returnCode && export GIT_COMMIT=$shortCommitID && /rdo/rodeo/setup/jenkins_scripts/jenkins_slack_notification.bash") 
    } 

    /** 
    * List of parameters to use for the coverage report. 
    * 
    * @param String coverageFile name of the file to parse for the coverage report 
    * @return List 
    */ 
    static def getCoberturaParameters(coverageFile) { 
     return [ 
       $class    : 'CoberturaPublisher', 
       autoUpdateHealth : true, 
       autoUpdateStability: true, 
       coberturaReportFile: coverageFile, 
       failUnhealthy  : true, 
       failUnstable  : true, 
       maxNumberOfBuilds : 0, 
       onlyStable   : false, 
       sourceEncoding  : 'UTF_8', 
       zoomCoverageChart : false 
     ] 
    } 
} 

ジェンキンスパイプラインが

properties([ 
     buildDiscarder(
       logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '20') 
     ), 
     pipelineTriggers([ 
       [$class: 'BitBucketTrigger'], 
       pollSCM('H/1 * * * *') 
     ]) 
]) 
@Library('com.pipeline') _ 
node("linux") { 
    def specificEnv = [ 
      "TEST_FOLDER=tests/unit_tests/", 
      "COVER_FOLDER=rdo_shotgun_workflows", 

    ] 
    stage('SCM') {checkout scm} 

    withEnv(com.pipeline.Utils.getCommonEnv() + com.pipeline.Utils.getPythonEnv() + specificEnv) { 
     ansiColor('xterm') { 
      try { 
       stage('Pre-Build Cleanup') { com.pipeline.Utils.preBuildCleanUp() } 
       stage('Set VirtualEnv') { com.pipeline.Utils.buildVirtualEnv() } 
       stage('Test') { retry(3) { com.pipeline.Utils.runTestsWithPyTest() } } 
       env.RC = 0 
      } catch (err) { 
       env.RC = 1 
       throw err as java.lang.Throwable 
      } finally { 
       stage('Publish results') { 
        step(com.pipeline.Utils.getCoberturaParameters("coverage.xml")) 
        junit 'nosetests.xml' 
       } 
       stage('notification') { com.pipeline.Utils.sendSlackNotification(env.RC) } 
       stage('Post-Build Cleanup') { step([$class: 'WsCleanup']) } 
      } 
     } 
    } 
} 

ジェンキンスジョブの設定のようになります。すべてはセットアップを jenkins job setup

、私はに仕上げますエラーがあります:

(... trimed setup of the jenkins job) 
> git fetch --no-tags --progress ssh://[email protected]/rodeofx/rdo_shotgun_workflows.git +refs/heads/*:refs/remotes/origin/* --depth=20 
Checking out Revision 48c78e3220a47fae9823578a1c30fa5a25349958 (RDEV-8036-jenkinsFile-slack) 
> git config core.sparsecheckout # timeout=10 
> git checkout -f 48c78e3220a47fae9823578a1c30fa5a25349958 
> git rev-list 48c78e3220a47fae9823578a1c30fa5a25349958 # timeout=10 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
groovy.lang.MissingPropertyException: No such property: com for class: groovy.lang.Binding 
    at groovy.lang.Binding.getVariable(Binding.java:63) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224) 
    at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221) 
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28) 
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) 
    at WorkflowScript.run(WorkflowScript:121) 
    at ___cps.transform___(Native Method) 
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74) 
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) 
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66) 
    at sun.reflect.GeneratedMethodAccessor758.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    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:74) 
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) 
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:330) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:242) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:230) 
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:748) 

私はグルーヴィーパッケージの定義を疑っていますが、私はそれに固執しています。

ご意見は大歓迎です。

よろしく、 ジョルディ

+0

「com.pipeline」が適切なライブラリ名であるかどうかわかりません。 "com-pipeline"で置き換えてみてください。 – Jayan

答えて

0

何共有ライブラリ内のフォルダ構造は次のように見えますか? documentation状態このように見えるように持って、それは次のとおりです。あなたの場合は

(root) 
+- src      # Groovy source files 
| +- org 
|  +- foo 
|   +- Bar.groovy # for org.foo.Bar class 

を、Utilssrc/com/pipeline/Utils.groovyする必要があります。

関連する問題