【CICD技术专题】|「Jenkins实战系列」jenkins+pipeline构建自动化部署
前提引言Jenkins 的精髓是 Pipeline(流水线技术),那为什么要用 Pipeline 呢?实现自动化构建,其中 Pipeline 能够将以前 project 中的配置信息以 steps 的方式放在一个脚本里,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程,形成流水式发布,构建步骤视图化。
简单来说,Pipeline 适用的场景更广泛,能胜任更复杂的发布流程。举个例子,job 构建工作在 master 节点,自动化测试脚本在 slave 节点,而不同节点的执行处理通过 Pipeline 可以。
基本概念【Stage: 阶段】,一个 Pipeline 可以划分为若干个 Stage,每个 Stage 代表一组操作。注意,Stage 是一个逻辑分组的概念,可以跨多个 Node。
【Node: 节点】,一个 Node 就是一个 Jenkins 节点,或者是 Master,或者是 slave,是执行 Step 的具体运行期环境。
【Step: 步骤】,Step 是最基本的操作单元,小到创建一个目录,大到构建一个 Docker 镜像,由各类 Jenkins Plugin 提供。
Pipeline 配置新建一个 “流水线” 的 job配置 Pipeline 脚本Pipeline 也支持 Poll SCMpipline 流水线流程PIpeline 语法Pipeline 支持两种语法:Declarative Pipeline(在 Pipeline 2.5 中引入,结构化方式) 和 Scripted Pipeline,两者都支持建立连续输送的 Pipeline。共同点两者都是 pipeline 代码的持久实现,都能够使用 pipeline 内置的插件或者插件提供的 steps,两者都可以利用共享库扩展。
不同点:两者不同之处在于语法和灵活性。Declarative pipeline 对用户来说,语法更严格,有固定的组织结构,更容易生成代码段,使其成为用户更理想的选择。
但是 Scripted pipeline 更加灵活,因为 Groovy 本身只能对结构和语法进行限制,对于更复杂的 pipeline 来说,用户可以根据自己的业务进行灵活的实现和扩展。
下面举例介绍两种语法的使用Declarative Pipelinewindows 环境脚本案例pipeline{
agentany//在可用的节点运行
stages{
stage('Prepare'){
steps{
//清空发布目录
bat'''ifexistD:\\publish\\LoginServiceCore(rd/s/qD:\\publish\\LoginServiceCore)
ifexistC:\\Users\\Administrator\\.nuget(rd/s/qC:\\Users\\Administrator\\.nuget)exit'''
}
}
//拉取git代码仓库
stage('Checkout'){
steps{
checkout([$class:'GitSCM',branches:[[name:'*/master']],doGenerateSubmoduleConfigurations:false,extensions:[],submoduleCfg:[],
userRemoteConfigs:[[credentialsId:'c6d98bbd-5cfb-4e26-aa56-f70b054b350d',
url:'http://xxx/xxx/xxx']]])
}
}
//构建
stage('Build'){
steps{
bat'''cd"D:\\ProgramFiles(x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web"
dotnetrestore
dotnetbuild
dotnetpublish--configurationRelease--outputD:\\publish\\LoginServiceCore'''
}
}
//部署
stage('Deploy'){
steps{
bat'''cdD:\\PipelineScript\\LoginServiceCore
pythonLoginServiceCore.py'''
}
}
//自动化测试(python代码实现)
stage('Test'){
steps{
bat'''cdD:\\PipelineScript\\LoginServiceCore
pythonLoginServiceCoreApitest.py'''
}
}
}
}
复制代码
Scripted Pipelinenode('master'){//master节点运行,以下stage也可指定节点
stage'Prepare'//清空发布目录
bat'''ifexistD:\\publish\\LoginServiceCore(rd/s/qD:\\publish\\LoginServiceCore)
ifexistC:\\Users\\Administrator\\.nuget(rd/s/qC:\\Users\\Administrator\\.nuget)
exit'''
//拉取git代码仓库
stage'Checkout'
checkout([$class:'GitSCM',branches:[[name:'*/master']],doGenerateSubmoduleConfigurations:false,extensions:[],submoduleCfg:[],userRemoteConfigs:[[credentialsId:'c6d98bbd-5cfb-4e26-aa56-f70b054b350d',
url:'http://xxx/xxx/xxx']]])
//构建
stage'Build'
bat'''cd"D:\\ProgramFiles(x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web"
dotnetrestore
dotnetbuild
dotnetpublish--configurationRelease--outputD:\\publish\\LoginServiceCore'''
//部署
stage'Deploy'
bat'''
cdD:\\PipelineScript\\LoginServiceCore
pythonLoginServiceCore.py
'''
//自动化测试(python代码实现)
stage'Test'
bat'''
cdD:\\PipelineScript\\LoginServiceCore
pythonLoginServiceCoreApitest.py
'''
}
复制代码
Pipeline Docker 脚本示例node{
//代码检出
stage('getCode'){
gitcredentialsId:'git-credentials-id',url:'http://192.168.19.250/libo/test.git'
}
//镜像中进行单元测试
stage('unittesting'){
//启动golnag:1.7并在golang内编译代码
docker.image('golang:1.7').inside{
sh'./script/unittest.sh'
}
}
//镜像中代码构建
stage('Build'){
defconfFilePath='conf/app.conf'
defconfig=readFileconfFilePath
writeFilefile:confFilePath,text:config
//启动golnag:1.7并在golang内编译代码
docker.image('golang:1.7').inside{
sh'./script/build.sh'
}
}
//编译镜像并push到仓库
defimagesName='192.168.18.250:5002/ufleet/uflow:v0.9.1.${BUILD_NUMBER}'
stage('ImageBuildAndPush'){
docker.withRegistry('http://192.168.18.250:5002','registry-credentials-id'){
docker.build(imagesName).push()
}
}
//启动刚运行的容器
stage('deployiamegs'){
//需要删除旧版本的容器,否则会导致端口占用而无法启动。
try{
sh'dockerrm-fcicdDemo'
}catch(e){
//errmessage
}
docker.image(imagesName).run('-p9091:80--namecicdDemo')
}
}
复制代码
git 操作认证withCredentials([usernamePassword(credentialsId:'credentials-id',passwordVariable:'GIT_PASSWORD',usernameVariable:'GIT_USERNAME')]){
sh'''
printf"machinegithub.com\nlogin$GIT_USERNAME\npassword$GIT_PASSWORD"~/.netrc
//continuescriptasnecessaryworkingwithgitrepo...
'''
}
复制代码
git 拉取代码checkoutscm:([
$class:'GitSCM',
userRemoteConfigs:[[credentialsId:'******',url:${project_url}]],
branches:[[name:'refs/tags/${project_tag}']]
])
复制代码
阅读原文
网站开发网络凭借多年的网站建设经验,坚持以“帮助中小企业实现网络营销化”为宗旨,累计为4000多家客户提供品质建站服务,得到了客户的一致好评。如果您有网站建设、网站改版、域名注册、主机空间、手机网站建设、网站备案等方面的需求...
请立即点击咨询我们或拨打咨询热线:13245491521 13245491521 ,我们会详细为你一一解答你心中的疑难。 项目经理在线