原文地址: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官方镜像

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

    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:

node{
stage('get clone'){
//check CODE
git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
} //定义mvn环境
def mvnHome = tool 'M3'
env.PATH = "${mvnHome}/bin:${env.PATH}" stage('mvn test'){
//mvn 测试
sh "mvn test"
} stage('mvn build'){
//mvn构建
sh "mvn clean install -Dmaven.test.skip=true"
} stage('deploy'){
//执行部署脚本
echo "deploy ......"
}
}

注意:这里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:

    node{
    stage('get clone'){
    //check CODE
    git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
    } stage('mvn test'){
    withMaven(
    maven: 'M3') {
    sh "mvn test"
    }
    } stage('mvn build'){
    //mvn构建
    withMaven(
    maven: 'M3',
    mavenLocalRepo: '.repository') {
    sh "mvn clean install -Dmaven.test.skip=true"
    }
    } stage('deploy'){
    //执行部署脚本
    echo "deploy ......"
    }
    }
  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):

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

}

参考文档:
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. (12)python 标准库

    模块 如果模块和自己写的程序不在同一个目录,可以通过sys.path.append(路径)把程序引入 import sys sys.path.append('C:/abc')#注意 \ 的方向 意思是 ...

  2. poj3233(等比矩阵求和)

    poj3233 题意 给出一个 \(n \times n\) 的矩阵 \(A\) ,求 \(A + A^2 + A^3 + ... + A^k\) . 分析 构造矩阵 \[ \begin{bmatri ...

  3. hihocoder1069 最近公共祖先·三(tarjin算法)(并查集)

    #1069 : 最近公共祖先·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho使用了Tarjan算法来优化了他们的“最近公共祖先”网站,但是 ...

  4. leetcode122 Best Time to Buy and Sell Stock

    题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益. 思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二 ...

  5. CentOS 7 下Ansiable搭建命令列表 及常用监控指令

    根据文章 [  自动化运维工具Ansible详细部署 ] 搭建 ============================================================== 1.Ans ...

  6. WordPress插件扫描工具plecost

    WordPress插件扫描工具plecost   WordPress是PHP语言开发的博客平台.该平台允许用户通过插件方式扩展博客功能.由于部分插件存在漏洞,给整个网站带来安全风险.Kali Linu ...

  7. 10.3(Java学习笔记)JDBC时间操作

    一.时间分类 数据库     java类 Date  ---- java.sql.Date   表示日期 yyyy-MM--dd (年月日) Time  ----java.sql.Time    表示 ...

  8. C# 6.0可能会支持模式匹配了

    今天在CodePlex的Roslyn讨论区发现了一个帖子:Draft spec for records and pattern-matching in C#,估计MS计划在C# 6.0中支持模式匹配了 ...

  9. 在GIT中修改提交记录

    在SVN中,提交记录是无法修改的.比如说,当我们提交了某次修改后,发现该次提交中有错误时,只能将将补丁再次提交一遍.这样,就存在两次提交记录,没有保证提交的原子性. 在GIT中,由于提交是在本地进行的 ...

  10. transform使用导致元素内字体出现模糊的坑~~~

    项目中遇到的,关于居中弹出层的字体模糊问题. 先来个对比图: 清晰版: 模糊版: 这个是一个不定宽高的弹出框,居中的方式如下代码: .layerdiv { position: fixed; top: ...