簡體   English   中英

設置Jenkins管道共享庫

[英]Set up of Jenkins pipeline shared library

我們嘗試切換到jenkins管道,但是在設置共享庫時,我對groovy / java的低水平阻止了我們。

在這里,我的共享庫(在bitbucket的git repo中的{root} /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']) }
            }
        }
    }
}

詹金斯工作的設置: 詹金斯職位設置

通過所有這些設置,我完成了以下錯誤:

(... trimed setup of the jenkins job)
> git fetch --no-tags --progress ssh://git@bitbucket.org/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)

我懷疑我的groovy軟件包的定義,但是我堅持這一點。

非常歡迎您提供反饋。

最好的問候,喬迪

共享庫中的文件夾結構是什么樣的? 文檔指出,必須看起來像這樣:

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

在您的情況下, Utils必須為src/com/pipeline/Utils.groovy

根據jenkins文檔 ,以下是您所用的目錄結構。

(root)
+- src                       # Groovy source files
|   +- com
|       +- pipeline
|           +- Utils.groovy  # for com.pipeline.Utils class

設置Jenkins作業時(根據您的圖像),使用簡單的庫名稱代替com.pipeline ,因此在您的情況下,您可以將utils更改為庫名稱

注意:請勿更改Utils.groovy文件中提到的包名稱com.pipeline

並在Jenkinsfile中進行以下更改

@Library('utils') _           // change the library name to utils
import com.pipeline.*         // Add this line

//change com.pipeline to pipeline
withEnv(pipeline.Utils.getCommonEnv() + pipeline.Utils.getPythonEnv() + specificEnv) {  

也可以直接使用類名而不是pipeline.Classname進行訪問

@Library('utils') _ 
import com.pipeline.Utils

withEnv(Utils.getCommonEnv() +Utils.getPythonEnv() + specificEnv) { 

進行上述更改,它肯定會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM