Jenkis pipeline构建项目实践

在完成前文的jenkins server 在k8s环境部署之后,本文我们来测试在k8s集群环境中的jenkins pipeline构建项目和更新,具体环境要求如下:
1、jenkins pipeline插件安装成功
2、要更新的应用已提前部署
3、Jenkins slave中需要有kubectl、svn、mvn客户端且环境变量设置准确
4、Jenkis slave需要能和master的api-server进行正常通信(这里为了简便,前文构建jenkins server的时候直接对defalut这个service account做rabc授权。其他可选方案是使用~/.kube/config文件实现客户端的授权认证)
5、需要配置共享卷(slave编译完成之后把war包copy到共享卷位置)

一、创建构建任务

1、点击“jenkins ”——“新建 ”——“pipeline”,输入项目的名称,点击“OK”

2、下拉到“pipeline”配置,填写脚本

具体脚本内容如下:

podTemplate(name: ‘jenkins-slave‘, cloud: ‘kubernetes‘,
  namespace: ‘default‘, label: ‘jenkins-slave‘,
  serviceAccount: ‘default‘, containers: [
  containerTemplate(
      name: ‘jenkins-slave‘,
      image: ‘harbor.59iedu.com/fjhb/jenkins-slave-toolkit:2018-08-10-v1‘,
      args: ‘${computer.jnlpmac} ${computer.name}‘,
      ttyEnabled: true,
      privileged: false,
      alwaysPullImage:true,
      )
  ],
  volumes: [
    persistentVolumeClaim(mountPath: ‘/tmp/‘, claimName: ‘tomcat-jcsj-data‘)
  ]) {
  node(‘jenkins-slave‘) {
    stage(‘svn-checkout‘) {
      container(‘jnlp‘) {
          sh """
          svn checkout --username=yanglw --password=mypassword http://192.168.1.81/svn/fjhbjsb/k8s-pipeline-test --non-interactive
          """
      }
    }

    stage(‘mvn-package‘) {
      container(‘jnlp‘) {
          sh """
          mkdir -p /var/jenkins_home && cd k8s-pipeline-test && mvn clean package && cp -rpf target/*.war /tmp/
          """
      }
    }

    stage(‘restart‘) {
      container(‘jnlp‘) {
          sh """
          pod_name=`kubectl  get pods -l name=jcsj-dev -o name | cut -d"/" -f2`
          kubectl delete pod \$pod_name
          """
      }
    }

    }
}

3、脚本释义
podTemplate节指定创建pod的模板和环境

Name:为pod的名称前缀
Cloud:为构建pod的云环境,需要和前面新建的云环境名称一样
Namespace:创建pod所在的namespace
Label: 创建pod对应的标签
serviceAccount:pod使用sa,这里使用default, 所以前文创建jenkins master deployment的时候多创建了一个default-role,这样自动创建出来的的jenkins-slave具有对应的api权限

containerTemplate节

指定创建容器的模板,当云环境里面配置pod模板后,容器模板以云环境的配置为准。

Volumes节

配置jenkins-slave pod挂载的卷,当云环境里面配置pod模板后,挂载卷以云环境配置为准

stage节为具体的pipeline步骤

这里第一步进行svn代码迁出;
第二步进行编译,并把包传到共享卷上面;
第三步对pod进行删除,因为应用采用deployment方式部署,所以可以实现重新创建pod,达到更新的效果。

二、构建准备

在开始构建之前,有必要介绍一下jcsj-dev的环境,这个环境是需要提前部署好的。部署文件如下

# cat deploy.yml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jcsj-dev
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: jcsj-dev
    spec:
      containers:
      - name: jcsj-dev
        image: tomcat:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 8080
        volumeMounts:
        - mountPath: /usr/local/tomcat/webapps
          name: tomcat-jcsj-data
      volumes:
      - name: tomcat-jcsj-data
        persistentVolumeClaim:
          claimName: tomcat-jcsj-data
---
apiVersion: v1
kind: Service
metadata:
  name: jcsj-dev
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 8453
  selector:
name: jcsj-dev
# cat pv.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: tomcat-jcsj-data
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteMany
  nfs:
    path: /home/tomcat_jcsj
    server: 192.168.115.6
  persistentVolumeReclaimPolicy: Recycle 

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: tomcat-jcsj-data
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi


三、更新项目代码

更新index.jsp并提交

四、开始构建

点击“立即构建”,构建过程中可以通过控制台输出看具体的日志信息


构建过程中观察jenkins master的日志输出:

# kubectl logs -f jenkins-master-588b89c75f-ztvgm
INFO: Started provisioning Kubernetes Pod Template from kubernetes with 1 executors. Remaining excess workload: 0
Aug 13, 2018 10:14:22 AM hudson.slaves.NodeProvisioner$2 run
INFO: Kubernetes Pod Template provisioning successfully completed. We have now 2 computer(s)
Aug 13, 2018 10:14:22 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Created Pod: jenkins-slave-gjjbh in namespace default
Aug 13, 2018 10:14:22 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for Pod to be scheduled (0/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:28 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Container is waiting jenkins-slave-gjjbh [jnlp]: ContainerStateWaiting(message=null, reason=ContainerCreating, additionalProperties={})
Aug 13, 2018 10:14:28 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for Pod to be scheduled (1/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:34 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (1/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:35 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (2/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:36 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (3/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:37 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (4/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:38 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (5/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:39 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (6/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:40 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (7/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:41 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (8/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:42 AM hudson.TcpSlaveAgentListener$ConnectionHandler run
INFO: Accepted JNLP4-connect connection #1 from /172.30.66.4:47408
Aug 13, 2018 10:14:42 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (9/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:43 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (10/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:44 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (11/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:45 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (12/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:46 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (13/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:47 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (14/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:48 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (15/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:49 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (16/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:50 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (17/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:51 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (18/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:52 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (19/100): jenkins-slave-gjjbh
Aug 13, 2018 10:14:53 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesLauncher launch
INFO: Waiting for agent to connect (20/100): jenkins-slave-gjjbh
Aug 13, 2018 10:15:12 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch
INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]
Aug 13, 2018 10:15:21 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch
INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]
Aug 13, 2018 10:16:04 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1 doLaunch
INFO: Created process inside pod: [jenkins-slave-gjjbh], container: [jnlp] with pid:[-1]
Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate
INFO: Terminating Kubernetes instance for agent jenkins-slave-gjjbh
Aug 13, 2018 10:16:06 AM jenkins.slaves.DefaultJnlpSlaveReceiver channelClosed
WARNING: Computer.threadPoolForRemoting [#8] for jenkins-slave-gjjbh terminated
java.nio.channels.ClosedChannelException
        at org.jenkinsci.remoting.protocol.impl.ChannelApplicationLayer.onReadClosed(ChannelApplicationLayer.java:208)
        at org.jenkinsci.remoting.protocol.ApplicationLayer.onRecvClosed(ApplicationLayer.java:222)
        at org.jenkinsci.remoting.protocol.ProtocolStack$Ptr.onRecvClosed(ProtocolStack.java:832)
        at org.jenkinsci.remoting.protocol.FilterLayer.onRecvClosed(FilterLayer.java:287)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.onRecvClosed(SSLEngineFilterLayer.java:181)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.switchToNoSecure(SSLEngineFilterLayer.java:283)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.processWrite(SSLEngineFilterLayer.java:503)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.processQueuedWrites(SSLEngineFilterLayer.java:248)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.doSend(SSLEngineFilterLayer.java:200)
        at org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayer.doCloseSend(SSLEngineFilterLayer.java:213)
        at org.jenkinsci.remoting.protocol.ProtocolStack$Ptr.doCloseSend(ProtocolStack.java:800)
        at org.jenkinsci.remoting.protocol.ApplicationLayer.doCloseWrite(ApplicationLayer.java:173)
        at org.jenkinsci.remoting.protocol.impl.ChannelApplicationLayer$ByteBufferCommandTransport.closeWrite(ChannelApplicationLayer.java:311)
        at hudson.remoting.Channel.close(Channel.java:1295)
        at hudson.remoting.Channel.close(Channel.java:1263)
        at hudson.slaves.SlaveComputer.closeChannel(SlaveComputer.java:708)
        at hudson.slaves.SlaveComputer.access$800(SlaveComputer.java:96)
        at hudson.slaves.SlaveComputer$3.run(SlaveComputer.java:626)
        at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution$PodTemplateCallback finished
INFO: Removing pod template and deleting pod jenkins-slave-7gnwx from cloud kubernetes
Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate
INFO: Terminated Kubernetes instance for agent default/jenkins-slave-gjjbh
Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave _terminate
INFO: Disconnected computer jenkins-slave-gjjbh
Terminated Kubernetes instance for agent default/jenkins-slave-gjjbh
Aug 13, 2018 10:16:06 AM org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution$PodTemplateCallback finished
WARNING: Failed to delete pod for agent default/jenkins-slave-7gnwx: not found
Aug 13, 2018 10:16:07 AM org.jenkinsci.plugins.workflow.job.WorkflowRun finish
INFO: k8s-pipeline-test #1 completed: SUCCESS

五、验证结果

六、后记

本文仅介绍的pipeline方式构建项目的过程,实际应用中还需要解决若干问题,例如:

1、svn checkout步骤需要明文配置用户名和密码,存在安全性问题
2、 每次执行构建都需要全量迁出代码,效率不高
3、要更新的应用需要提前部署好,不能适配首次编译部署场景
4、对共享卷存在依赖,解耦问题需要解决

后续将介绍maven项目的构建和自动部署!

原文地址:http://blog.51cto.com/ylw6006/2159861

时间: 2024-10-09 03:11:06

Jenkis pipeline构建项目实践的相关文章

Jenkis pipeline构建菠菜源码搭建出售项目实践

在完成前文的jenkins server 在k8s环境部署 之后,本文我们来测试在k8s集群环境中的jenkins pipeline构建项目和更新,具体环境要求如下:1.jenkins pipeline插件安装成功2.要更新的应用已提前部署3.Jenkins slave中需要有kubectl.svn.mvn客户端且环境变量设置准确4.Jenkis slave需要能和master的api-server进行正常通信(这里为了简便,前文构建jenkins server的时候直接对defalut这个se

jenkins实现手动选择分支构建项目

需求: 由于我们的程序会有很多分支,测试或者发版会根据不同的分支来构建项目实践: 实现在构建项目的时候弹出弹窗提醒我们来构建那个分支的项目,来构建不同环境的相同目录的程序例如:我们选择构建2.2.5分支,jenkins就会自动构建2.2.5分支          我们选择构建2.2.6分支,jenkins就会自动构建2.2.6分支配置如下:先创建一个自由模式的项目上图的Name:记录一下,等下需要放到git写程序存放位置需要构建项目的分支:点击保存在构建,即可 原文地址:http://blog.

Pipeline流水线项目构建

Pipeline简介1 )概念Pipeline,简单来说,就是一套运行在 Jenkins 上的工作流框架,将原来独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排和可视化的工作.2)使用Pipeline有以下好处(来自翻译自官方文档):代码:Pipeline以代码的形式实现,通常被检入源代码控制,使团队能够编辑,审查和迭代其传送流程. 持久:无论是计划内的还是计划外的服务器重启,Pipeline都是可恢复的. 可停止:Pipeline可接收交互式输入,以确定是否继续执行

Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践)

转载:http://www.cnblogs.com/EasonJim/p/6295372.html Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践) 前提: 这一篇是继上一篇http://www.cnblogs.com/EasonJim/p/6092938.html的真实项目实践. 思路和大致实现步骤请参照上一篇文章. 注意:这篇文章涉及到两台服务器:1为测试服务器.2为正式服务器.且,两台服务器都必须安装了jenkins. 注意2:这里所说的一键部署是指从测试服务器到

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(4)-构建项目解决方案 创建EF DataBase Frist模式

进行本次文章之前,我们可能需要补充一些基本知识. 首先我们系统是基于接口编程的,我们为什么要使用借口编程,其实这是应用了一种企业应用架构模式 Repository(仓储) 一种用来封装存储,读取和查找行为的机制,它模拟了一个对象集合. 支持在领域和数据映射层之间实现彻底分离和单向依赖关系的目标. 接口编程 最主要的目的是使关注点分离,让开发人员各司其职 代码封存保密,分包开发,无需要给予逻辑结构代码,只需要给予接口文件 缺点:设计困难,比传统的三层,单类模式困难,但是接口程序员工资都很高! AO

Django项目实践2 - Django模板

http://blog.csdn.net/pipisorry/article/details/45061511 上篇:Django项目实践1 - 创建Django项目 Django模板 {视图和模板对逻辑和显示进行了分隔} 上面是使用 django.http.HttpResponse() 来输出"Hello World!".该方式将数据与视图混合在一起,不符合Django的MVC思想.或者HTML被直接硬编码在 Python 代码之中. html = "<html>

《Spring Boot 入门及前后端分离项目实践》系列介绍

课程计划 课程地址点这里 本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 个部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 项目实践开发.Spring Boot 介绍.前后端分离.API 规范等内容旨在让读者更加熟悉 SpringBoot 及企业开发中需要注意的事项并具有使用 SpringBoot 技术进行基本功能开发的能力:这最后的项目实战为课程的主要部分,我会带着大家实际的开发一个前后端分离的 Spring Boot 实践项目,

Vue2+VueRouter2+webpack 构建项目实战(二):目录以及文件结构

通过上一篇博文<Vue2+VueRouter2+webpack 构建项目实战(一):准备工作>,我们已经新建好了一个基于vue+webpack的项目.本篇文章详细介绍下项目的结构. 项目目录以及文件结构 如图所示: 如上图所示,自动构建的vue项目的结构就是这样. 目录/文件 说明 build 这个是我们最终发布的时候会把代码发布在这里,在开发阶段,我们基本不用管. config 配置目录,默认配置没有问题,所以我们也不用管 node_modules 项目开发依赖的一些模块 src 开发目录(

MyBatis源码解析【2】构建项目

有了之前的准备,今天就要开始构建学习使用的项目了.废话不多说.开始吧. 首先我用IDEA构建了一个空的maven项目,然后加入了Mybatis的依赖. <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependenc