Jenkins Job DSL - 从git repo加载groovy库

时间:2017-04-11 08:04:12

标签: jenkins groovy jenkins-job-dsl

我希望尽可能减少种子工作,并将所有逻辑保存在中央git存储库中。此外,我有几个独立的Jenkins实例,然后可以共享代码。如何在Jenkins Job DSL脚本中加载groovy库?

是否有类似Pipeline Remote File Loader Plugin的内容,因此您只需要执行fileLoader.fromGit('lib.groovy', 'https://git.repo')

2 个答案:

答案 0 :(得分:0)

以下是关于在参数化管道作业中实现此目的的快速表, 使用来自git.repo

的SCM的Pipeline脚本

您可能感兴趣的内容:

  • 加载mecanism:stash/unstash
  • "来自SCM"位置:src = "../${env.JOB_NAME}@script/"

<强>詹金斯

Pipeline
    Definition:        "Pipeline script from SCM"
    SCM:               Git
    Repository URL     git.repo
    Branches to build  */master
    Script Path        jobs/build.groovy
This project is parameterized:
    String Parameter   PARAM0
    String Parameter   PARAM1

<强> git.repo

├── jobs
│   ├── helpers
│   │   └── utils.groovy
│   └── build.groovy
└── scripts
    ├── build
    │   └── do_build.sh
    └── inc.sh

内容:utils.groovy

├── jobs
│   ├── helpers
│   │   └── utils.groovy
def log(msg) {
    println("========== " + msg)
}
return this

目录:build.groovy

├── jobs
│   └── build.groovy
stage ('Init') {
    /* Loads */
    def src                 = "../${env.JOB_NAME}@script/"
    def helpers_dir         = 'jobs/helpers'
    def scripts_dir         = 'scripts'
    /* Stages Scripts */
    def do_build            = 'build/do_build.sh'
    utils                   = load src + helpers_dir + "/utils.groovy"
    dir(src) {
        stash name: scripts_dir, includes: "${scripts_dir}/"
    }
}
stage ('Build') {
    node () {
        unstash scripts_dir
        build_return = sh (returnStdout: true, script: """
            ./${scripts_dir}/${do_build} \
            "${PARAM0}" \
            "${PARAM1}"
        """).readLines()
        builded = build_return.get(build_return.size()-1).tokenize(',')
        utils.log("PARAM0: " + builded[0])
        utils.log("PARAM1: " + builded[1])
    }
}

内容:inc.sh

└── scripts
    └── inc.sh
#!/bin/sh
## scripts common includes
common=included

目录:do_build.sh

└── scripts
    ├── build
    │   └── do_build.sh
#!/bin/sh
## includes
. $(dirname $(dirname ${0}))/inc.sh
echo ${common}
## ${0} : PARAM0
## ${1} : PARAM1
echo "${0},${1}"

答案 1 :(得分:0)

Job DSL Gradle Example显示了如何在Git存储库中维护DSL代码。