我就纳闷了,铺天盖地的各种jenkins实操,咋就没有面向白白授人以鱼,快速上手的简单指导呢?自己整理如下:
slave概念:slave需要添加,它是用来干嘛的?它或它们可能是一台服务器,采用jenkins母机对其们进行构建和部署及配置
blue ocean:插件之一,需要jenkins2.7.x以上版本。
同类软件:hudson,bamboo。teamcity。travisci。
非常有用,划重点:☆☆jenkinsfile,pipeline的语法参考,一堆的steps的语法https://jenkins.io/doc/pipeline/steps/
jenkinsfile的sample https://github.com/jfrog/project-examples
jenkins的一些模块的参数:https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.publisher.PublisherContext.publishCloneWorkspace
jenkins的pipeline支持的插件https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md
jenkins pipeline 的 语法https://jenkins.io/doc/pipeline/steps/
cloudbees学习网站,一站式学习,非常经典
https://premium.cbu.cloudbees.com/cloudbees-university-jenkins-pipeline-fundamentals
https://premium.cbu.cloudbees.com/cloudbees-university-jenkins-fundamentals
https://jenkins.io/doc/pipeline/examples/
jenkinsfile语法:https://jenkins.io/doc/book/pipeline/syntax
jenkins命令:https://jenkins.io/doc/book/pipeline/syntax/
jenkins的那些命令哪儿来的?在这里:https://jenkins.io/doc/pipeline/steps/
jenkins的console script的参考:https://github.com/jenkinsci/jenkins-scripts/tree/master/scriptler 划重点:非常重要的资源地址,可以查到 https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console#JenkinsScriptConsole-ExampleGroovyscripts https://javadoc.jenkins-ci.org/hudson/model/package-tree.html
https://javadoc.jenkins-ci.org/hudson/model/package-tree.html
https://github.com/jenkinsci/jenkins-scripts/tree/master/scriptler
jenkins主要案例:
1.基于ECS执行,ECS的绑定方式:安装插件,在manage jenkins内增加ECS,根据aws上的资源号进行设置,需要ECS的credential
pipline特点:
1.master可以重启,pipeline不受影响
2.可暂停
两种:
declarative pipeline:
较新
agent指定slave(declarative采用node指定slave)
严格的语法结构,对groovy要求不高
blueocean生成流水线
script pipeline
1.比较豪放,对groovy要求略高
采用node指定slave
3.Scripted Pipeline code must explicitly handle the checkout scm step,脚本流水线必须显示的执行checkout scm步骤
语言,Uses a DSL based on Apache Groovy ,"DSL" (Domain Scripting Language)
blueocean用法:在jenkins传统界面的左侧有一个打开blueocean(这个是插件安装上去的),打开后进入到blueocean的界面,里面可以点击添加流水线,可以选种类
jenkins规则:
1.slave/agent,一般非特殊情况,命令在这其上执行
2.jenkins构建的包会保留在工作空间内,除主动删除外永久保留
3.parallel:Each "parallelized branch" is a stage A stage can use either steps or parallel at the top level; it cannot use both A stage within a parallel stage can contain agent and steps sections A parallel stage cannot contain agent or tools Add failfast true to force all parallel process to abort if one fails
pipeline和multibranchpipeline
1.优选multibranchpipeline,why???
2.blueocean创建的pipeline全部都是multibranch的
3.multibranch可以为新分支直接创建流程
4.当git的分支被删除时,对应的role也会被删除
5.可以覆盖父任务的设置,即个性定制
agent:
1.agent any,可以在任意一个agent上执行;
2.agent none,写在全局的位置,表示每个stage可以定义自己的agent
3.当在step里时,当有需要在agent上执行的命令时不能使用agent none,废话……
4.agent {label 'xxxx'},很重要,指定用哪个agent
5.agent指定docker镜像或者dockerfile,只能用于multibranch pipline: pipeline { agent { docker 'bzzzcentos:7' } .... }
pipeline { agent none stages { stage ('Build') { agent { label 'bzzzmaven' } steps { ... } } stage ('Deploy') { agent { label 'bzzzproduction' } steps { ... } } } }
6.script方式的:node('xxx'){container('ccccc'){checkout scm stage 'vvvv'{}}}
技巧:
1.可以使用blueocean界面编辑方式生成一个大概的脚本,细节的再具体调试
1.jenkins代码生成应用,就在jenkins内,当创建流水线的时候里面有个链接叫pipeline-syntax:https://dso.ci.kingland.cc/job/ami-update/pipeline-syntax/
2.创建pipeline的时候有definition选项,有俩,一个叫pipeline script from SCM表示从git库拉取jenkinsfile,一个叫pipeline scrip,选择这个后就可以在下面弹出代码框进行编辑(保存后或者执行后是否会在git库中同时生成一个jenkinsfile?)
3.优选multibranch pipline的方式
4.multipipeline的设置,第一级项目设置的进入方法:项目总览页,项目名称旁边有个齿轮设置按钮,进入后左侧有个config。
5.agent(node)可以定义在全局,也可以定义在每个stage内
jenlkins 中k8s配置:在管理界面的系统配置里,添加云服务,俩选项,一个是java一个是k8s,选择k8s后一路向下可以看见有一个pod template选项,点开后即可看见一个标准的pod的镜像参数。即可
agent或者node的设置:https://jenkins.io/doc/book/pipeline/docker/
区别是声明式的是单独的agent{}用在stage下,并且和step{}同级,脚本式的是在stage里的docker.image('').inside{xxxxxx}
宣明式jenkinsfile常用命令:
1.environment: 配置变量,用于stage内或者全局
2.option:
3.parameters: 在全局声明parameters,在stage内引用时使用${params.xx}调用
4.triggers:
脚本式jenkinsfile常用命令:
1.if else
2.try catch finally
jenkins script console 在jenkins server或者node上执行简单的命令,比如查看磁盘空间,巴拉巴拉巴拉之类的咋办?
https://blog.youkuaiyun.com/xiaosongluo/article/details/54564069参考这个链接,然后总结如下:采用groovy的语法执行要干的事儿,比如可以直接println(xxxxxxx),再比如想解密jenkins里的credential的密码,就可以仍在这里面用println打出来,解析的方法是hudson.util.secret.decrypt
println(hudson.util.Secret.decrypt("{xxxxx=}"))
jenkins 的 credential:
1.一些必备的登录验证可以捆绑于此,然后在jenkins内使用withcredential调用。里面有个参数叫credentialids既是这个。
jenkins的几种任务:
1.直接配置的;
2.单独配置pipeline的
3.配置multipipeline的
实战1.jenkins +aws + ECS:创建master和slave
实战2:部署案例:AWS+ ECS + docker部署一个服务:https://aws.amazon.com/getting-started/tutorials/deploy-docker-containers/
关于trigger:https://wiki.jenkins.io/display/JENKINS/Building+a+software+project
实战3,构建案例:jenkins,创建一个freesytle,重点:poll SCM的设置,buildenv的选择是gradle-artifactory(有一个deploy server和一个resolve server,干啥的呢?deploy是传文件的,resolve是从哪里拉依赖之类的),选取这个gradle-artifactory后就会在下方显示artifactory相关的选项(jenkins的一个技巧:jenkins可以安插件,插件会在job的设置里出现,job中可以延伸很多模块)
实战4,在console里通过groovy脚本获取job信息及执行命令https://github.com/jenkinsci/jenkins-scripts
实战5,关于credential,https://jenkins.io/doc/developer/security/secrets/
实战6,jenkins内执行命令操作jenkins master,在script里执行下述代码
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ls /tmp'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"
实战7,disable 和enable:在freestyle中的configure中可以设置,一旦disable,project不会再被执行,除非设置为enable状态,作用是当知道这个project一段时间内不需要执行时设置成disabled
>>>>>>>>>>>>>>>>>>>>>>>>>>>
slave选择,在configure中可以选择是否限定slave,其中可以选择master,也可以选择某个label,也可以或,用||,比如master||xxx
>>>>>>>>>>>>>>>>>>>>>>>>>>>
关于jenkins的语法,查询了一些文章,真TMD的垃圾,就不能用人类的语言好好记录?直接机器翻译jenkins docs……,而且没有系统的整理的文章。中国的各行业实打实的技术知识储备,可见一斑:短,懒,虚
jenkinsfile的语法及变量等信息可在jenkins内查看到,路径是jenkins的地址/pipeline-syntax/globals#env,可以看到全局变量
回答几个问题:
1.properties是干啥的?
2.当点击构建时的可选或者手填参数,是在哪里设置的?multibranch中的branch单独手动添加这个值还是在jenkinsfile中生成的?
3.jenkinsfile内的全局变量,带类型声明和不带类型声明的区别?带类型是强制
4.局部变量的作用域?
5.check scm:
6.artifactory怎么设置?在哪里设置?请见devops架构篇
7.stage内withenv和def的区别?withenv只用于当前stge,而直接声明的变量可以用于全局
8.一些jenkinsfile里常用的step:Pipeline Utility Steps,比如readproperties https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/和https://jenkins.io/doc/pipeline/steps/
9.docker的相关命令:比如withrepository,见jenkins的地址/pipeline-syntax/globals#env,同全局变量在一起
10.parallel命令: 并行执行,比如在pipeline中有测试步骤,没有必要按序执行,此时可采用parallel加于执行的步骤之前表示这个步骤执行中继续进行后续的步骤https://jenkins.io/doc/book/pipeline/jenkinsfile/#parallel-execution
11.build命令:调用另一个job,直接使用build https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
12.基本模块:比如dir之类的https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/
一,变量:
来源:
1.env.xx:jenkins系统变量,在构建过程中可传递至job内,比如构建的版本,变量名是固定不变的,在jenkins主地址/https://dso.ci.kingland.cc/pipeline-syntax/globals下可以找到,等同于ansible中的系统变量的使用方式。貌似可以直接调用已定义的系统变量,比如直接引用BRANCH_NAME而不加env
2.properties():
3.string xx = :用string的意思是:
4.boolean类型: boolean xxx = a = b,第二个等号先执行,返回结果通过第一个等号传值true或false给xxx。
jenkins哪里设置自定义的全局变量?
当变量是list时,声明使用def xxx = [ key : value,key:value]各个值之间可以换行,但是得有逗号。调用时
调用:
${xxx}
语法:调用时如果最外层是单引号,内部的${}不会解析为变量。原装不动的解析为${}.双引号不会,即单引号表示省略转义
二,credential传值
1.credential传值可以通过withcredential这个方法,具体见参考链接https://jenkins.io/doc/pipeline/steps/credentials-binding/
简单套路:withcredential(可以通过变量传进来,一个列表){在这个credential下要干的事儿}
三,方法定义,固定方法引用
四,动态变量传值,在multibranch类型的pipeline里,在每个branch内名称右侧或者左侧菜单栏有一个view configure可以添加动态参数
五,artifactory的调用语法:https://www.jfrog.com/confluence/display/RTF/Working+With+Pipeline+Jobs+in+Jenkins
六.流水线中动态传参,即在执行的时候可以临时传入参数,有两种情况:
1.普通pipeline:创建job时在build里有一个选项叫parameter,在其下可以添加不同类型的参数。在jenkinsfile内就可以使用param.xxx引用
2.multibranch:因为是基于jenkinsfile的一个父job,只有在进入分支后再点击configure才相当于普通piepline的config,里面有预设参数。但是这样就比较麻烦,要逐个分支进行配置
七,Scan Multibranch Pipeline Now的scan的作用:当点击的时候会发生什么事?
八,withcredential:([[],[],()……])可以同时传递多个credential,比如class类型的,file类型的等等https://jenkins.io/doc/pipeline/steps/credentials-binding/
九。jenkins传参:def,env,properties的区别(properties定义的变量,会在点击执行时生成动态传参对话框,有几种类型)
1.env.获取的变量时jenkins的环境变量,随处可以获取
2.设置环境变量:声明式的使用environment,脚本式的使用withenv(),在哪里声明,适用于哪里,比如在stages内声明,那就只能在stages内生效
3.内置变量,比如上一步buil结束后保存的版本号,在execute shell内可以查看到内置了哪些变量(点击下方链接),引用方法:在execute shell内使用%xx%引用,在jenkinsfile内使用env.xxx引用
4.def指定的就是一个可变的变量,可改值
5.赋值变量:env.xxx = xxxxxx
一.agent篇:当有实体机时,在manage jenkins里的sysconfig里配置agent,agent可以选择安装基础镜像
二.
1.执行命令:sh或者bat(windows)declarative里sh咋整? 直接sh ‘xxxx’,如果是多行?'''xxx ccc dddd'''即可,两种流水线都是这样的,windows用bat
2.declarative和script的区别: script很随意,直接node下stage。
3.retry 和 timeout模块,针对一个step重试或者超时,还可以嵌套组合
4.结束后的处理;decla采用的是用post命令,script用try catch和finnally,语法非常像java
jenkins权威指南 https://www.bogotobogo.com/DevOps/Jenkins/images/Intro_install/jenkins-the-definitive-guide.pdf
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
jenkins 与 artifactory集成:参考下面的两个链接即可:
https://www.jfrog.com/confluence/display/RTF/Working+With+Pipeline+Jobs+in+Jenkins
其中有个鸡肋的功能 ,叫artifactory staging,会在job的左侧工具栏生成一个叫这个东东的东西(首先在config里选中那个enable release management)。这个功能在freestyle里可以直接配置,但是在pipeline里没法用。调研了一大堆的东西都不行:
链接里给了一个思路:以maven为例的:首先那个功能核心要干的事儿就是版本迭代的管理(修改pom或者gradle.propoties文件,然后推到repository库内),文档给的办法是先修改pom或者那个propoties文件,然后使用这个Allowing Interactive Promotion for Published Builds 功能promote到artifactory上去。实际上在artifactory上是保留了每次的build的包的,只是状态不同,有的是rollback,有的是promote,有的是其他的。只需要修改即可。
jenkins还是有很大的改进空间的。太不友好了,包括很多功能和参数,都简洁到令人发指,你tmd不说参数叫啥名,choice有哪些可选项,我怎么知道那些个鬼?读源码??????????????额滴神。
artifactory在jenkins上的release management管理,当选中时会生成两个功能,一个在左侧工具栏上,一个在build之后的number的后面。后面那个小按钮可以通过“”“Allowing Interactive Promotion for Published Builds” 这个生成,在pipeline里配上即可。具体方法见上面的链接
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
jenkins中的括号 中括号 https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md
git url: 'https://github.com/jglick/simple-maven-project-with-tests.git', branch: 'master'
As before, Groovy lets you omit parentheses around function arguments. The named-parameter syntax is also a shorthand for creating a map, which in Groovy uses the syntax [key1: value1, key2: value2]
, so you could also write:
git([url: 'https://github.com/jglick/simple-maven-project-with-tests.git', branch: 'master'])
jenkins 使用tool 。首先在manage jenkins里面有个globle tool config,在里面配置tool,比如起个名叫xxx,下拉选取要安装的工具的版本,比如jdk的9.然后确认。在pipeline内就可以通过下面的语法调用:
env.JAVA_HOME="${tool 'jdk-8u45'}"
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
jenkins 报错
fatal: git fetch-pack: expected shallow list 在jenkins的配置文件里不选 shallow clone即可,原理??还没看
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
本地搭建一台jenkins server
步骤:
》》》》》》》》》》》》》》》
jenkins 内置变量:
jenkins引用groovy共通方法:https://www.jianshu.com/p/35b4fe64cebf。。目的是把一些共通
功能抽提出来,然后放到一个公共库内。使用的时候通过配置后直接调用,类似于java的lib包,jenkinsfile里的引用方式是@Library
jenkinsfile内引进tool: 首先在manage jenkins里的globle xx里找到gradle,点击就可以选择版本。选中后保存。在jenkinsfile中使用如下脚本:
tools{
gradle: "the name defined in the globle"
}
stages{
stage(test){
gradle clean .....
}
}
jenkins 在config中设置了环境变量,同时在checkout中引用,但是未识别为变量。其根本原因是选中了一个选项,就在scm里的git的下面的有个可选项,叫Lightweight Checkouts。这个不选即可,选上 的话就不会引入变量。就这么简单,jenkins这个设计实际很鸡肋,真正实践中是不需要这么复杂的场景的
jenkins 设置和使用环境变量: 在jenkinsfile任意位置 environmnet{ xxx = xxxxx} 或者使用withEnv(["xxx=xx","yyy=yyyyy","zzz=${params.zzzzz}"]){ }