原文地址:http://blog.csdn.net/aixiaoyang168/article/details/72818804

Pipeline的几个基本概念:

  • Stage: 阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。注意,Stage是一个逻辑分组的概念,可以跨多个Node。
  • Node: 节点,一个Node就是一个Jenkins节点,或者是Master,或者是Agent,是执行Step的具体运行期环境。
  • Step: 步骤,Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。

2、环境、软件准备

本次演示环境,我是在本机mac上操作,以下是我本地软件及版本:

  1. Jenkins:version 2.46.3
  2. Tomcat:version 7.0.70
  3. Jdk:version “1.8.0_91”
  4. Docker: Version 17.03.0-ce-mac1 (15583)
  5. Gitlab: GitLab Community Edition 8.17.4

注意:本次我们分别演示两种方式安装jenkins,基于Tomcat和Jdk安装,我们需要提前安装好Jdk、Tomcat服务,基于Docker安装,我们需要提前安装docker环境。这里我就忽略Tomcat、Jdk、docker、gitlab的安装过程,着重说下Jenkins安装以及如何跑Pipeline Job。

3、安装、启动并配置jenkins服务

一、Jenkins安装启动方式有两种,一种是基于tomcat、Jdk启动,一种是基于Docker启动。

1)基于Tomcat、Jdk启动

  1. 首先下载Jenkins最新的安装包,可以去官网下载最新版,点击 这里 下载。
  2. 启动Jenkins可以有两种方式
    • 进入war包所在目录,直接执行java -jar jenkins.war
    • 将war包放在Tomcat webapps目录下,启动tomcat。

2)基于Docker启动

  1. 拉取jenkins官方镜像

    1. docker pull jenkins
    • 1
  2. 启动jenkins 容器

    1. docker run -p 8080:8080 -p 50000:50000 -v /Users/wanyang3/jenkins_home:/var/jenkins_home jenkins
    • 1

    启动完成之后,浏览器访问http://localhost:8080,第一次启动初始化稍慢一些,稍等一会就可开始jenkins初始化配置。

二、Jenkins初始化配置

1)、解锁Jenkins —》 Unlock Jenkins

说明:按照弹框提示,找到该initialAdminPassword文件,我这里使用Docker启动Jenkins,并且把jenkins_home目录挂载到我磁盘指定目录,所以这里我只需要复制/Users/wanyang3/jenkins_home/initialAdminPassword即可,如果非挂载方式Docker启动,则需要进入容器内根据提示路径找到该文件。

2)定制 Jenkins Customize Jenkins

说明:这里若选择Install suggested plugins安装,那么jenkins就会给你推荐安装一些很有用的插件,若选择Select plugins to install安装,那么就需要自己根据业务需要选择性安装某些插件。

3)创建第一个管理员用户,Create first admin user

说明:这里创建第一个管理员用户,也可以不设置,直接点击“Continue as admin”,进入jenkins以后再设置。

4、新建Pipeline Job Demo

1)创建一个pipeline job

创建完成后,点击该job —》设置 —》 Pipeline,在输入框中输入script语句。

示例script:

  1. node{
  2. stage('get clone'){
  3. //check CODE
  4. git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
  5. }
  6. //定义mvn环境
  7. def mvnHome = tool 'M3'
  8. env.PATH = "${mvnHome}/bin:${env.PATH}"
  9. stage('mvn test'){
  10. //mvn 测试
  11. sh "mvn test"
  12. }
  13. stage('mvn build'){
  14. //mvn构建
  15. sh "mvn clean install -Dmaven.test.skip=true"
  16. }
  17. stage('deploy'){
  18. //执行部署脚本
  19. echo "deploy ......"
  20. }
  21. }

注意:这里job执行pipeline定义,可以有两种方式,一种直接在job填写pipeline script来执行,
一种是使用pipeline script from SCM。

  • pipeline script:直接在Script输入框里面输入pipeline script语句即可,参考说明可以点击输入框下边的Pipeline Syntax,里面有很多示例操作说明,非常好用。
  • pipeline script from SCM:需要配置SCM代码存储Git地址或SVN地址,指定script文件所在路径,每次构建job会自动去指定的目录执行script文件。

2)配置全局工具配置Maven

因为我们的项目是Maven工程,这次执行build需要使用mvn命令,所以需要配置一个全局的Maven。
进入到 系统管理 -》Global Tool —》Maven -》Maven安装,指定Name、MAVEN_HOME、选择要安装的Mavne版本,自动安装即可。

3)执行构建

点击“立即构建”,即可开始构建,右侧Stage View查看构件流程,点击每个stage,可以查看每个阶段的详细日志输出。

FAQ

  1. 使用插件 mvn 命令,在script语句里面,我们使用的tool工具来获取全局Maven配置M3,这里我们也可以使用Pipeline Maven Integration Plugin插件来完成。
    点击插件管理 —》可选插件 —》Pipeline Maven Integration Plugin —》立即安装,安装完成之后,就可以使用该插件使用mvn命令了。

    示例script:

    1. node{
    2. stage('get clone'){
    3. //check CODE
    4. git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
    5. }
    6. stage('mvn test'){
    7. withMaven(
    8. maven: 'M3') {
    9. sh "mvn test"
    10. }
    11. }
    12. stage('mvn build'){
    13. //mvn构建
    14. withMaven(
    15. maven: 'M3',
    16. mavenLocalRepo: '.repository') {
    17. sh "mvn clean install -Dmaven.test.skip=true"
    18. }
    19. }
    20. stage('deploy'){
    21. //执行部署脚本
    22. echo "deploy ......"
    23. }
    24. }
  2. 这里check code检出代码操作,jenkins默认集成github,这里我们使用自己的gitlab,clone项目需要用户名密码登录,这里我们可以使用jenkins的credentials创建证书,生成证书以后,在clone代码时,指定git credentialsId,即可完成认证工作。 若不知道生成的证书id是多少,这里有个好办法,去每个项目的pipeline-syntax,默认进入到Snippet Generator(代码段生成器),我们选择git: Git,然后输入Repository URL、Branch、选择Credentials,点击Generate Pipeline Script,在下方输入框里面,就可以生成对应的流程的脚本语句,是不是很方便。

持续集成交互(英文指导:https://www.mindtheproduct.com/2016/02/what-the-hell-are-ci-cd-and-devops-a-cheatsheet-for-the-rest-of-us/)

例子script (From http://www.ciandcd.com/?p=155):

  1. // Run this on the master node:
  2. node {
  3. // The JDK is configured as a tool with the name 'jdk-8u77' in the Jenkins 'Global Tool Configuration'
  4. env.JAVA_HOME="${tool 'jdk-8u77'}"
  5. env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
  6.  
  7. // Maven is configured as a tool with the name 'M3' in the Jenkins 'Global Tool Configuration'.
  8. def mvnHome = tool 'M3'
  9.  
  10. stage 'Checkout'
  11. // Configure the credential in Jenkins, and use the credential's ID in the following step:
  12. git url: 'ssh://git@gitrepo.computas.com/fs/fs-knowledge-editor.git', credentialsId: '8dbfb6d2-2549-4c6e-9a6e-994ae8797efc'
  13.  
  14. stage 'Build and tag'
  15. // Define the version of this build.
  16. // BASE_VERSION is defined as a build parameter in the UI definition of the job.
  17. // Note how Groovy code is used to format the number of the current build.
  18. def version = "${BASE_VERSION}-J2TEST-" + currentBuild.number.toString().padLeft(4,'0')
  19. // Execute the maven command as a shell command step. On Windows, a 'bat'-step would be used instead.
  20. sh "${mvnHome}/bin/mvn clean verify -f KnowledgeEditor/pom.xml -Dfs.version=${version}"
  21. // Archive the zip file for access in through the Jenkins UI, or for other uses.
  22. archive 'KnowledgeEditor/com.computas.fs.ke.products/target/products/*.zip'
  23.  
  24. // Each build is tagged with an annotated tag.
  25. // There is no pipeline plugin for this (the Git Publisher plugin is not compatible),
  26. // so everything has to be implemented using shell commands.
  27. // First, we have to configure git with the mandatory user information:
  28. sh "git config user.name \"Jenkins Pipeline\""
  29. sh "git config user.email bob@computas.com"
  30. // Next, tag this commit.
  31. def msg = "\"Automatically created tag ${version}\""
  32. sh "git tag -a -m ${msg} ${version}"
  33. // Finally, push to the repo.
  34. // For this to work, the ssh keys must be available in Jenkins' ~/.ssh folder
  35. sh "git push origin ${version}"
  36.  
  37. // Send a mail to the person responsible for manual testing and release.
  38. mail subject: 'A new version of KEIII is available for testing.',
  39. body: 'A new version of KEIII is available for testing and approval of release.',
  40. charset: 'utf-8',
  41. from: 'bob@computas.com',
  42. mimeType: 'text/plain',
  43. to: 'fs-tester@computas.com'
  44.  
  45. stage 'Release'
  46. // User input showing up in the Jenkins UI.
  47. // If the timeout is reached, an exception is thrown and the build aborted.
  48. timeout(time: 120, unit: 'SECONDS') {
  49. input message: 'Do you want to release this version, ' + version + ', of KEIII?', ok: 'Release'
  50. }
  51. // A catch block could deal with the exception.
  52.  
  53. // In order to release to Nexus, deploy and access information needs to be made available in
  54. // a maven settings file. This configuration is copied into the file defined as
  55. // mavenSettingsFile from the corresponding managed file in Jenkins...
  56. def mavenSettingsFile = "${pwd()}/.m2/settings.xml"
  57.  
  58. // ... using the configuration file build wrapper:
  59. wrap([$class: 'ConfigFileBuildWrapper',
  60. managedFiles: [
  61. [fileId: '85adba0c-908b-4dbf-b3aa-65fe823e8984',
  62. targetLocation: "${mavenSettingsFile}"]]]) {
  63. // deploy to Nexus
  64. sh "${mvnHome}/bin/mvn deploy -s ${mavenSettingsFile} -f KnowledgeEditor/pom-deploy.xml -Dfs.version=${version}"
  65. }

}

参考文档:
pipeline hello-world
pipeline-plugin
using-pipeline-plugin-accelerate-continuous-delivery-part-1
using-pipeline-plugin-accelerate-continuous-delivery-part-2
using-pipeline-plugin-accelerate-continuous-delivery-part-3

Jenkins2.x Pipeline持续集成交互的更多相关文章

  1. 初试Jenkins2.0 Pipeline持续集成

    转载自:https://cloud.tencent.com/developer/article/1010628 1.Jenkins 2.0介绍 先介绍下什么是Jenkins 2.0,Jenkins 2 ...

  2. Jenkins Pipeline 持续集成

    Jenkins Pipeline 持续集成 Pipeline Script 执行流程 在使用Pipeline之前请确保Jenkins是2.x版本以上,并且安装了Pipeline插件. Jenkins提 ...

  3. (转)Jenkins2.0 Pipeline 插件执行持续集成发布流程 - git -资料 - 不错的文档

    1.Jenkins 2.0 的精髓是 Pipeline as Code Jenkins 2.0 的精髓是 Pipeline as Code,是帮助 Jenkins 实现 CI 到 CD 转变的重要角色 ...

  4. 使用beanstalkd实现定制化持续集成过程中pipeline

    持续集成是一种项目管理和流程模型,依赖于团队中各个角色的配合.各个角色的意识和配合不是一朝一夕能练就的,我们的工作只是提供一种方案和能力,这就是持续集成能力的服务化.而在做持续集成能力服务化的过程中, ...

  5. DevOps之持续集成Pipeline(一)

    一.Pipeline介绍     Jenkins2.0中最大的一个特性就是Pipeline,实际使用中Pipeline已经超越了我们对jenkins本身的理解,可能在之前我们大多数把Jenkins当做 ...

  6. devops持续集成,Centos7.6下gitlab+jenkins(pipeline)实现代码自动上线

    持续集成 gitlab+jenkins(pipeline)实现代码自动上线 环境准备:Centos7.6版本ip:192.168.0.13 主机名:gitip:192.168.0.23 主机名:jen ...

  7. 基于Jenkins Pipeline的ASP.NET Core持续集成实践

    最近在公司实践持续集成,使用到了Jenkins的Pipeline来提高团队基于ASP.NET Core API服务的集成与部署效率,因此这里总结一下. 一.关于持续集成与Jenkins Pipelin ...

  8. 持续集成之jenkins2

    ip 什么是持续集成 没有持续集成 持续集成最佳实践 持续集成概览 什么是Jenkins Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开 ...

  9. jenkins持续集成、插件以及凭据

    Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins功能包括: ...

随机推荐

  1. codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】

    B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. 数据排序 第三讲( 各种排序方法 结合noi题库1.10)

    说了那么多种排序方法了,下面就来先做几个题吧 06:整数奇偶排序 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给定10个整数的序列,要求对其重新排序.排序 ...

  3. cogs 2554. [福利]可持久化线段树

    题目链接 cogs 2554. [福利]可持久化线段树 题解 没有 代码 #include<cstdio> #include<cstring> #include<algo ...

  4. [CTSC2018]混合果汁(二分答案+主席树)

    考场上写了60分的二分答案,又写了15分的主席树,然后就弃了.. 合起来就A了啊!主席树忘了开20倍空间最后还炸掉了. 最水的签到题被我扔了,主要还是不会用线段树求前缀和. 做法应该是比较显然的,首先 ...

  5. Entity Framework part1

    First Demo实体框架Entity Framework,简称EFEF是微软推出的基于Ado.Net的数据库访问技术,是一套ORM框架底层访问数据库的实质依然是ado.net是一套orm框架,即框 ...

  6. Debian6 安装Kscope(也适用于Ubuntu)

    参考:http://soft.chinabyte.com/os/134/12307634.shtml kscope1.6.2在这里下载,下载后解压出kscope-1.6.2.tar.gz. 在ubun ...

  7. npm的简单应用

    1.安装node 2.旧版node升级 (1)linux系统 sudo npm install npm -g (2)window系统 npm install npm -g 3.安装淘宝镜像cnpm(以 ...

  8. Docker实践4: 基于nginx对后端的weblogic负载均衡

    为什么要用Nginx(抄了一段) 1.nginx相对于apache的优点: 轻量级,同样起web服务,比apache占用更少的内存及资源 抗并发,nginx处理请求是异步非阻塞的,而apache则是阻 ...

  9. webstorm9 License Key

    用户名 oschina 注册码 ===== LICENSE BEGIN ===== 7362-D18089T 00000xmyY1VfVxjkElWULKcA5XHbfN 5qjOh3fgGZvNXH ...

  10. Delphi 7下最小化到系统托盘

    在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789   _NOTIFY ...