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.netproject下增加的任何屬性,都定義為一個變量,都為了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 :(- addFolders
Folder層次用于jobs,views,slaves組織。
JJB支持2種上傳job到某個目錄的方式:
- name中指定,/my-job-name
- 使用folder屬性
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允許在不同級別為變量定義默認值。優先級如下:
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- item3Inclusion 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sentinel 源码分析(一)
- 下一篇: IDEA同时使用maven和gradle