在 init.groovy.d 脚本中使用管道初始化 Jenkins 2.0 [英] Initializing Jenkins 2.0 with pipeline in init.groovy.d script

查看:16
本文介绍了在 init.groovy.d 脚本中使用管道初始化 Jenkins 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了自动化,我想用管道作业初始化一个 Jenkins 2.0 实例.我想创建一个在启动时复制到 /usr/share/jenkins/ref/init.groovy.d/ 文件夹的 Groovy 脚本.该脚本应创建一个 Jenkins 2.0 流水线作业,用于处理来自 SCM 的 Jenkinsfile.

For automation, I would like to initialize a Jenkins 2.0 instance with a pipeline job. I want to create a Groovy script that is copied to the /usr/share/jenkins/ref/init.groovy.d/ folder on startup. The script should create a Jenkins 2.0 Pipeline job for processing a Jenkinsfile from SCM.

我找不到 2.0 管道类的相关 Javadoc 或如何执行此操作的示例.

I cannot find the relevant Javadoc for the 2.0 pipeline classes or examples of how to do this.

以前,使用 Job DSL 创建管道,我使用 Groovy 脚本创建带有 ExecuteDslScripts 构建器的 FreeStyleProject.该作业将成为 Job DSL 种子作业.

Previously, using Job DSL to create a pipeline, I used a Groovy script to create a FreeStyleProject with an ExecuteDslScripts builder. That job would then be the Job DSL seed job.

一种选择是使用初始化脚本来创建 Job DSL 种子作业以创建 Jenkins 2.0 管道.它只是看起来不必要地复杂.

One option is to use an init script to create a Job DSL seed job to create a Jenkins 2.0 pipeline. It just seems unnecessarily complex.

我正在这个 repo 中进行试验:https://github.com/martinmosegaard/vigilant-sniffle

I am experimenting in this repo: https://github.com/martinmosegaard/vigilant-sniffle

推荐答案

如果您只需要创建一个简单的管道作业,您可以使用 Jenkins API.但这确实只有在创建一个简单的作业时才有效,对于复杂的设置,您需要一些抽象,例如 Job DSL.

If you only need to create one simple pipeline job, you can use the Jenkins API. But that really only works well when creating one simple job, for a complex setup you need some abstraction like Job DSL.

从这里开始:http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#createProject(java.lang.Class,%20java.lang.String).

例子:

import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.job.WorkflowJob

WorkflowJob job = Jenkins.instance.createProject(WorkflowJob, 'my-pipeline')

然后您需要填充作业,例如设置流定义.

Then you need to populate the job, e.g. setting a flow definition.

或者您可以等待 系统配置 DSL 插件 准备好.但是它还没有发布,我不确定它是否可以创造就业机会.

Or you can wait for the System Config DSL Plugin to be ready. But it has not been released yet and I'm not sure if it can create jobs.

这篇关于在 init.groovy.d 脚本中使用管道初始化 Jenkins 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆