Jenkins2.x Pipeline持续集成交互
原文地址: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上操作,以下是我本地软件及版本:
- Jenkins:version 2.46.3
- Tomcat:version 7.0.70
- Jdk:version “1.8.0_91”
- Docker: Version 17.03.0-ce-mac1 (15583)
- 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启动
- 首先下载Jenkins最新的安装包,可以去官网下载最新版,点击 这里 下载。
- 启动Jenkins可以有两种方式
- 进入war包所在目录,直接执行
java -jar jenkins.war - 将war包放在Tomcat webapps目录下,启动tomcat。
- 进入war包所在目录,直接执行
2)基于Docker启动
拉取jenkins官方镜像
docker pull jenkins- 1
启动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
使用插件 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 ......"
}
}- 这里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持续集成交互的更多相关文章
- 初试Jenkins2.0 Pipeline持续集成
转载自:https://cloud.tencent.com/developer/article/1010628 1.Jenkins 2.0介绍 先介绍下什么是Jenkins 2.0,Jenkins 2 ...
- Jenkins Pipeline 持续集成
Jenkins Pipeline 持续集成 Pipeline Script 执行流程 在使用Pipeline之前请确保Jenkins是2.x版本以上,并且安装了Pipeline插件. Jenkins提 ...
- (转)Jenkins2.0 Pipeline 插件执行持续集成发布流程 - git -资料 - 不错的文档
1.Jenkins 2.0 的精髓是 Pipeline as Code Jenkins 2.0 的精髓是 Pipeline as Code,是帮助 Jenkins 实现 CI 到 CD 转变的重要角色 ...
- 使用beanstalkd实现定制化持续集成过程中pipeline
持续集成是一种项目管理和流程模型,依赖于团队中各个角色的配合.各个角色的意识和配合不是一朝一夕能练就的,我们的工作只是提供一种方案和能力,这就是持续集成能力的服务化.而在做持续集成能力服务化的过程中, ...
- DevOps之持续集成Pipeline(一)
一.Pipeline介绍 Jenkins2.0中最大的一个特性就是Pipeline,实际使用中Pipeline已经超越了我们对jenkins本身的理解,可能在之前我们大多数把Jenkins当做 ...
- devops持续集成,Centos7.6下gitlab+jenkins(pipeline)实现代码自动上线
持续集成 gitlab+jenkins(pipeline)实现代码自动上线 环境准备:Centos7.6版本ip:192.168.0.13 主机名:gitip:192.168.0.23 主机名:jen ...
- 基于Jenkins Pipeline的ASP.NET Core持续集成实践
最近在公司实践持续集成,使用到了Jenkins的Pipeline来提高团队基于ASP.NET Core API服务的集成与部署效率,因此这里总结一下. 一.关于持续集成与Jenkins Pipelin ...
- 持续集成之jenkins2
ip 什么是持续集成 没有持续集成 持续集成最佳实践 持续集成概览 什么是Jenkins Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开 ...
- jenkins持续集成、插件以及凭据
Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins功能包括: ...
随机推荐
- HDU 2689.Sort it-冒泡排序
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】
[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...
- 洛谷 P1068 分数线划定【结构体排序】
题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据计划录取人数的150%划 ...
- Ubuntu 安装 tensorflow-gpu + keras
转载请注明: 仰望大牛的小清新:http://www.cnblogs.com/luruiyuan/ 本文原网址:http://www.cnblogs.com/luruiyuan/p/6660142.h ...
- 推荐下载CentOS下各种组件的网址
http://vault.centos.org/ ftp://mirrors.sohu.com/ http://mirror.webtatic.com/
- ASP.NET Core 2.2 基础知识(十三) WebAPI 概述
我们先创建一个 WebAPI 项目,看看官方给的模板到底有哪些东西 官方给出的模板: [Route("api/[controller]")] [ApiController] pub ...
- 微服务之SpringCloud实战(三):SpringCloud Eureka高可用
高可用Eureka 高可用我就不再过多解释了,Eureka Server的设计一开始就考虑了高可用的问题,在Eureka的服务治理设计中,所有的节点即是服务提供方也是消费方,注册中心也不例外,上一章中 ...
- Scala实战高手****第8课:零基础实战Scala最常用数据结构Map和Tuple及Spark源码鉴赏
本课内容1.Map和Tuple在Spark源码中的鉴赏2.Map和Tuple代码操作实战 ------------------------------------------------------- ...
- [Java基础] Java如何实现条件编译
条件编译绝对是一个好东西.如在C或CPP中,可以通过预处理语句来实现条件编译.但是在JAVA中却没有预处理,宏定义这些东西,而有时在一些项目中,我们又需要条件编译.那么,在JAVA中,该如何实现条件编 ...
- win7系统下ping不是内部或外部命令
win7下ping不是内部或外部命令,在使用java设置变量环境时,可能会遇到这样的问题,出现win7下ping不是内部或外部命令,是在设置变量环境是把一些设置删掉了的原因,请看在win7下怎么还原. ...