Jenkins Job Buidler
文章目录
- Job Definitions
- Job
- 参数
- Job Template
- Template变量的默认值
- Project
- Views
- View Template
- Macro
- Folders
- Item ID’s
- Raw config
- Defaults
- Variable References
- Variable Inheritance
- Yaml Anchors & Aliases
- Custom Yaml Tags
- Action Tags
- Inclusion Tags
- JJB使用
- 配置信息
- 任务操作
- 测试配置
- 更新任务
- 删除任务
Jenkins Job Builder(以下简称“JJB”)就是用来创建Jenkins任务的工具。为了简化配置大量Jenkins任务的工作量,采用更容易阅读的基于yaml或json格式的文件来编辑任务,然后使用JJB将yaml或json格式的配置转化为可以被Jenkins理解的XML格式的任务配置文件,并更新到Jenkins中。
工作原理很简单,Jenkins创建的任务实际是以XML文件的格式保存在$JENKINS_HOME/jobs/[job-name]/config.xml的,JJB能够将YAML转化为XML文件(借助Jenkins提供的命令接口),并更新到Jenkins中。
使用JJB的优点也是显而易见的:
- 无需通过前台页面来进行任务创建,方便puppet、docker等进行服务器配置管理或容器的构建;
- YAML文件作为配置可以通过版本管理工具管理起来;
- 可以通过定义job-template或参数,简化任务配置,有效减少工作量。
Job Definitions
Job
Job定义一个任务,
- job:name: job-nameproject-type: freestyledefaults: globaldescription: 'Do not edit this job through the web!'disabled: falsedisplay-name: 'Fancy job name'concurrent: trueworkspace: /srv/build-area/job-namequiet-period: 5block-downstream: falseblock-upstream: falseretry-count: 3node: NodeLabel1 || NodeLabel2logrotate:daysToKeep: 3numToKeep: 20artifactDaysToKeep: -1artifactNumToKeep: -1
参数
参数 | 意义 |
---|---|
project-type | 项目类型,默认为freestyle,具体见 modules |
defaults | 指定一些列公用默认值。如果Defaults的名字是“global”,那么将默认被所有的任务和任务模板使用。 |
description | 描述 |
disabled | 默认false |
display-name | 显示名称 |
concurrent | job是否并发执行,默认false |
workspace | 自定义workspace路径 |
folder | |
child-workspace | 子workspace |
quiet-period | 一个连续2次执行之间的间隔时间(秒),默认0 |
block-downstream | 下游任务在执行,是否阻塞。默认false |
block-upstream | 上游任务在执行,是否阻塞。默认false |
auth-token | |
retry-count | checkout 重试次数 |
node | job可运行的节点,通过label指定。 |
logrotate | |
jdk | |
raw | 自定义xml内容,可插入job定义中 |
Job Template
如果多个任务都是类似的配置,那么可以使用任务模板来定义,然后在“project”中将模板任务实现。
任务模板的语法及参数与任务定义相同,并且可以在任意地方根据需要增加变量。变量通过两个大括号定义,如{name}(在字符串中,变量最好用引号包起来;如果确实需要表示“{”和“}”字符,用“{{”和“}}”表示)。任何没有在project中定义的变量,将从D***efaults***中继承。
任务模板定义部分以“- job-template:”标识,通常必须包含一个{name}变量,否则所有的任务实例都采用同样的名字。任务模板不会创建任何任务。
Template变量的默认值
为了模板的重复利用,有时不需要值,这就需要为模板指定默认值。
JJB有2种方法为模板指定默认值。
1、在Job-Template中
- job-template:name: '{project-name}-verify'###################### Variable Defaults ######################branch: master###################### Job Configuration ######################parameters:- string:name: BRANCHdefault: '{branch}'scm:- git:refspec: 'refs/heads/{branch}'
上述定义了BRANCH变量的默认值为{branch},branch也是个变量,定义在job-template下。
2、使用{var|default}
- job-template:name: '{project-name}-verify'parameters:- string:name: BRANCHdefault: '{branch|master}'scm:- git:refspec: 'refs/heads/{branch|master}'
Project
项目用于将所有相关的任务归类(在实际使用时可以按照开发的project来分),以及给任务模板提供变量值,所以project的属性并无限制,可以定义任何模板中需要的属性。
- project:name: project-name #变量定义jobs:- '{name}-unit-tests':mail-to: developer@nowhere.net- '{name}-perf-tests':mail-to: projmanager@nowhere.net
project下增加的任何属性,都定义为一个变量,都为了job Template提供变量。jobs下列举的Job Template都会应用到上面定义的变量。
如果变量的值是一个list,那么会基于list的每个元素进行实现。多个list会导致出现“笛卡尔积”个具体实现
- job-template:name: '{name}-{branch}'builders:- shell: 'git checkout {branch_name}'- project:name: project-namebranch:- a:branch_name: feature-a- b:branch_name: feature-bjobs:- '{name}-{branch}'
这样会生成“project-name-a”和“project-name-b”两个具体任务,它们在构建时分别会调用“git checkout feature-a”和“git checkout feature-b”命令。
Views
一个job集合的显示方式。
View Template
Macro
Job定义的操作,例如builders,publishers,可以定义为一个宏。
# The 'add' macro takes a 'number' parameter and will creates a
# job which prints 'Adding ' followed by the 'number' parameter:
- builder:name: addbuilders:- shell: "echo Adding {number}"# A specialized macro 'addtwo' reusing the 'add' macro but with
# a 'number' parameter hardcoded to 'two':
- builder:name: addtwobuilders:- add:number: "two"# Glue to have Jenkins Job Builder to expand this YAML example:
- job:name: "testingjob"builders:# The specialized macro:- addtwo# Generic macro call with a parameter- add:number: "ZERO"# Generic macro called without a parameter. Never do this!# See below for the resulting wrong output :(- add
Folders
Folder层次用于jobs,views,slaves组织。
JJB支持2种上传job到某个目录的方式:
- name中指定,/my-job-name
- job:name: python-jobs/tox-py27builders:- shell: |tox -e py27
- 使用folder属性
- defaults:name: team1folder: team1-jobs- job:name: ruby-jobs/rspecdefaults: team1 # defaults参数builders:- shell: |rvm use --create ruby-2.3.0@rspecbundle installbundle exec rspec
Item ID’s
可以给一个代码块赋值一个id,在其他地方可以引用。
- project:name: test_template_idjobs:- 'simple-template': #通过id引用test_var: Hello Worldtype: periodicnum: 1- 'not-as-simple-template': #通过id引用 test_var: Goodbye Worldtype: canarynum: 2- job-template:name: 'template-test-ids-{num}-{type}'id: simple-templatebuilders:- shell: |echo "Template name: {template-name}"echo "Job name: template-test-ids-{num}-{type}"echo "{test_var}"- job-template:name: 'template-test-ids-{num}-{type}'id: not-as-simple-templatebuilders:- shell: |echo "Template name: {template-name}"echo "Job name: template-test-ids-{num}-{type}"- shell: |echo "{test_var}"
Raw config
wrappers:- raw:xml: |<hudson.plugins.xvnc.Xvnc><takeScreenshot>true</takeScreenshot><useXauthority>false</useXauthority></hudson.plugins.xvnc.Xvnc>
Defaults
许多共性的属性或动作可以提取出来放到一个默认集合中,供其他的任务使用。
如果Defaults的名字是“global”,那么将默认被所有的任务和任务模板使用。当然在任务中也可以显式定义属性值来覆盖全局默认的属性值。
- defaults:name: globalarch: 'i386'- project:name: project-namejobs:- 'build-{arch}' #使用默认属性- 'build-{arch}':arch: 'amd64' #覆盖默认属性。- job-template:name: 'build-{arch}'builders:- shell: "echo Build arch {arch}."
Variable References
可用通过{obj:key}变量把一个对象传入到模板中。
- project:name: test_custom_distridisabled: true #定义变量distributions: !!python/tuple [precise, jessie]architectures: !!python/tuple &architectures- amd64- i386axis_a:type: user-definedname: architecturesvalues: *architecturesjobs:- '{name}-source'- job-template:name: '{name}-source'project-type: matrixdisabled: '{obj:disabled}' #引用变量axes:- axis:type: user-definedname: distributionvalues: '{obj:distributions}'- axis: '{obj:axis_a}'
Variable Inheritance
JJB允许在不同级别为变量定义默认值。优先级如下:
- job-group
- project
- job-template
- defaults
- project:name: foojobs:- '{project-name}-merge':branch: production #覆盖project中变量值- '{project-name}-verify'branch: master
Yaml Anchors & Aliases
参考:https://blog.csdn.net/demon7552003/article/details/99693818
Custom Yaml Tags
Action Tags
!join:
第一个list元组作为分隔符。
- string-with-comma: !join:- ','-- item1- item2- item3- string-with-space: !join:- ' '-- item1- item2- item3
Inclusion Tags
!include
后跟的字符串作为一个yaml文件对待。
- job:name: test-job-1builders:!include: include001.yaml.inc#include001.yaml.inc 文件
- timeout-wrapper
- pre-scm-shell-ant
- copy-files
!include-raw:
对待给定的字符串或者字符串列表作为文件名称,文件内容不做任何转换(字节流)读取。
- job:name: test-job-include-raw-1builders:- shell:!include-raw: include-raw001-hello-world.sh- shell:!include-raw: include-raw001-vars.sh#include-raw001-vars.sh #!/bin/bash
#
# Sample script showing how the yaml include-raw tag can be used
# to inline scripts that are maintained outside of the jenkins
# job yaml configuration.echo "hello world"exit 0
!include-raw-escape:
读取内容需要转义
JJB使用
配置信息
默认路径:/etc/jenkins_jobs/jenkins_jobs.ini,定义要连接的jenkins服务器
[jenkins]
user=jenkins
password=password
url=http://localhost:8081/jenkins
任务操作
测试配置
jenkins-jobs test path/to/myjob.yaml
若测试通过,将输出XML格式的任务配置,如果想将测试的XML格式的任务配置输出到文件,可以添加“-o”参数:
更新任务
jenkins-jobs [--conf jenkins_jobs.ini] update path/to/job1.yaml
删除任务
jenkins-jobs [--conf jenkins_jobs.ini] delete job1
Jenkins Job Buidler相关推荐
- jenkins+sonarqube流水线脚本模板
pipeline { //这个任务在哪个主机上运行 //agent any//将这个项目运行在slave上 agent { label 'node1' }//参数化构建,主要设定git_version ...
- 更改Jenkins升级站点
更新地址:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json [图示]:
- Jenkins首次安装推荐插件出错 No such plugin: cloudbees-folder 超详细解决方案
我的环境:腾讯云 CentOS7 轻量应用服务器 docker run -u root -itd -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/v ...
- jenkins ssl证书报错问题解决
Jenkins 是一款流行的开源持续集成工具用于项目开发,具有自动化构建.测试和部署等功能. 可以.war的方式来运行Jenkins: 从Jenkins下载jenkins.war. 在目录下运行:ja ...
- 修改jenkins启动的默认用户
# 背景 通过yum命令安装的jenkins,通过service jenkins去启动jenkins的话,默认的用户是jenkins,但jenkins这个用户是无法通过su切换过去的 ,在某些环节可能 ...
- 对 Jenkins+ANT+Jmeter 接口测试的实践
转载地址:https://testerhome.com/topics/5262 1.前言 最近感觉大家都在讲Jenkins+jmeter+ant或maven的使用,但没有说到具体怎么投入到项目使用,只 ...
- Jenkins构建Maven聚合工程,指定构建子模块
一.设置单独编译构建子模块 配置: 1.Root POM指向父pom.xml 2.Goals and options指定构建模块的参数: mvn -pl project1/project1-son - ...
- jenkins 插件执行shell命令时,提示“Command not found”处理方法
首先提示找不到"Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具 ...
- Jenkins 插件 地址证书报错问题解决思路
问题提示摘要: SunCertPathBuilderException: unable to find valid certification path to requested target ... ...
最新文章
- 学习笔记 mysql_MySQL 学习笔记
- 从零开始学python数据分析-从零开始学Python数据分析与挖掘 PDF 下载
- c语言的翻译叫什么_什么是编译器?什么是集成开发环境?
- 法流程图_世界五大学习方法之西蒙学习法
- axios.delete()请求方式(含代码)- 应用篇
- 交通安全与智能控制专业学计算机吗,交通安全与智能控制专业主要做什么
- Android6.0以上打电话动态权限
- 计算机界 TOP 3 难题:“相等”是软件工程中许多重大问题的根源!
- Grunt自动化工具相关
- 【WC2013】糖果公园
- Java项目案例之---定时器的使用
- odbc oracle数据源配置文件,MB中使用Oracle ODBC数据源的配置帮助
- android仿漫画源码、抽奖转盘、Google相册、动画源码等
- 优雅地从浏览器打开本地应用
- Shell替换数组元素之间的间隔符号
- 如何降低自动化维护成本?
- 前端js调用摄像头进行录像并传到后端
- 智能安全加密芯片---ACL16
- 基于TCPCopy的仿真压测方案
- Java、JSP就业信息管理系统的设计与实现