jenkins2 pipeline入门

本文通过简单的pipeline的示例和详细的讲解,能够学习基本pipeline和groovy,然后开始实现自己的pipeline job。

翻译和修改自:https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md

1. 安装java,maven,配置jenkins

安装java和maven:

#install java jdk
sudo apt-get update
sudo apt-get install default-jdk

#install maven in ubuntu
sudo apt-get update
sudo apt-get install maven

#setting for maven
~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

#testing maven
/usr/share/maven/bin/mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-24-generic", arch: "amd64", family: "unix"

在jenkins的global tool configuration里配置环境变量

2. 创建简单的pipeline job

groovy代码如下:

node {
git url: ‘https://github.com/jglick/simple-maven-project-with-tests.git‘
def mvnHome = tool ‘M3‘
sh "${mvnHome}/bin/mvn -B verify"
}

上面的代码实现了从github checkout源代码,然后通过maven来构建, 代码中包含了测试用例,有可能会随机的失败, 如果有测试用例失败,则整个pipeline job将会标记为失败。

上面的实例假设为linux系统,如果是windows系统,需要修改为

bat "${mvnHome}\\bin\\mvn -B verify"

3. 理解基本groovy用法

node,node用来选择groovy运行的机器,只要node还有可用的executor,node{}的任务将会在选中的机器上运行,且会在选中的机器上创建workspace。许多的step必须在node里执行,例如git,sh等必须在node环境里执行。

不像用户定义的函数,pipeline step总是接受命名参数,括号可以省略,也可以使用标准的groovy语法传入map作为参数,例如:

git url: ‘https://github.com/jglick/simple-maven-project-with-tests.git‘
git url: ‘https://github.com/jglick/simple-maven-project-with-tests.git‘, branch: ‘master‘
git([url: ‘https://github.com/jglick/simple-maven-project-with-tests.git‘, branch: ‘master‘])

如果只有一个强制的参数,则可以省略参数名字,如下两种等价效果:
sh ‘echo hello‘
sh([script: ‘echo hello‘])

def可以定义groovy变量,tool可以检查指定名字的工具是否存在可以访问,在双引号里使用变量,变量将会被替换为真实的值:
def mvnHome = tool ‘M3‘
"${mvnHome}/bin/mvn -B verify"

4. 环境变量的使用

最简单的使用工具的方式是将工具路径加入到PATH中,通过env可以修改node对应机器的环境变量,后面的steps可以看到环境变量的修改。

node {
  git url: ‘https://github.com/jglick/simple-maven-project-with-tests.git‘
  def mvnHome = tool ‘M3‘
  env.PATH = "${mvnHome}/bin:${env.PATH}"
  sh ‘mvn -B verify‘
}

jenkins的job默认的环境变量,可以通过http://jenkins-server/job/javahelloworld/pipeline-syntax/globals来查看job默认的环境变量。

BRANCH_NAME
For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches.
CHANGE_ID
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number.
CHANGE_URL
For a multibranch project corresponding to some kind of change request, this will be set to the change URL.
CHANGE_TITLE
For a multibranch project corresponding to some kind of change request, this will be set to the title of the change.
CHANGE_AUTHOR
For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change.
CHANGE_AUTHOR_DISPLAY_NAME
For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author.
CHANGE_AUTHOR_EMAIL
For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author.
CHANGE_TARGET
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged.
BUILD_NUMBER
The current build number, such as "153"
BUILD_ID
The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds
BUILD_DISPLAY_NAME
The display name of the current build, which is something like "#153" by default.
JOB_NAME
Name of the project of this build, such as "foo" or "foo/bar".
JOB_BASE_NAME
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".
BUILD_TAG
String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". Convenient to put into a resource file, a jar file, etc for easier identification.
EXECUTOR_NUMBER
The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
NODE_NAME
Name of the agent if the build is on an agent, or "master" if run on master
NODE_LABELS
Whitespace-separated list of labels that the node is assigned.
WORKSPACE
The absolute path of the directory assigned to the build as a workspace.
JENKINS_HOME
The absolute path of the directory assigned on the master node for Jenkins to store data.
JENKINS_URL
Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)
BUILD_URL
Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)
JOB_URL
Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)

例如你可以在node中的step中使用env.BUILD_TAG。

时间: 2024-08-30 05:55:16

jenkins2 pipeline入门的相关文章

Jenkins pipeline 入门到精通系列文章

Jenkins2 入门到精通系列文章. Jenkins2 下载与启动jenkins2 插件安装jenkins2 hellopipelinejenkins2 pipeline介绍jenkins2 javahelloworldjenkins2 groovy入门jenkins2 pipeline入门jenkins2 pipeline高级jenkins2 Jenkinsfilejenkins2 multibranchjenkins2 Jenkinsfile和loadjenkins2 groovy脚本参考

jenkins2 pipeline高级

jenkins2 pipeline里groovy的高级用法.翻译自:https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd 1. 在groovy里使用函数,条件控制,循环,异常捕获等 node('remote') { git url: 'https://github.c

jenkins2 pipeline介绍

文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd 什么是jenkins2的pipeline? jenkins的实现是标准的master/slave模式,用户与master交互,master将job分布到slave上运行. jenkins的基本概念: 1. master, 也就是jenkins的server,是jenkins的核心,主要负责job的定时运行,将job分发到agent运行,和对job运

jenkins2 -pipeline 常用groovy脚本

jenkins2的核心是pipeline,pipeline的核心是groovy. 那有一些基础的groovy是必须经常使用的,如变量赋值,变量引用,打印变量,输出字符,任务调用,循环判断等. Groovy变量 注意:在jenkins里面配置Groovy变量时,注意进行测试,不要同Groovy自带的变量冲突!!! 既然是脚本语言,Groovy的变量也被设计成为了类似的弱类型,实际上Groovy同时支持强类型变量和"弱"类型变量,强类型变量拥有自己的类型,而"弱"类型变

jenkins2 groovy入门

文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd 安装: wget https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.7.zipunzip apache-groovy-binary-2.4.7.zipsudo ln -s /home/osboxes/Downloads/groovy-2.4.7/bin/groovy /usr/

Jenkins2 入门到精通(学习资料)

原文:https://www.zhihu.com/question/29163932/answer/138366614 Jenkins2 入门到精通 Jenkins2 下载与启动jenkins2 插件安装jenkins2 hellopipelinejenkins2 pipeline介绍jenkins2 javahelloworldjenkins2 groovy入门jenkins2 pipeline入门jenkins2 pipeline高级jenkins2 Jenkinsfilejenkins2

Jenkins pipeline 语法详解

原文地址http://www.cnblogs.com/fengjian2016/p/8227532.html pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. pipeline 是jenkins2.X 最核心的特性, 帮助jenkins 实现从CI 到 CD与 DevOps的转变 pipeline 提供一组可扩展的工具, 通过 pipeline domain specific language

devops-jenkins-Pipeline基础语法

1. jenkins-Pipeline基础语法  1) jenkins-Pipeline总体介绍 • Pipeline,简而言之,就是一套运行与jenkins上的工作流框架,将原本独立运行于单个或多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. • Pipeline是jenkins2.x最核心的特性,帮助jenkins实现从CI到CD与devops的转变 • https://jenkins.io/2.0/  2) 什么是jenkins Pipeline • jenkins P

Jenkins的项目管理

新建Item 使用Jenkins最重要的是能够创建一些工作流,除了部署,还能做很多流程上的事情.同样,一条条项目建起来需要做一定的管理,在Jenkins首页Jenkins->新建可以按自己的需要新建条目  Pipeline是最一般的流程,基本上什么都没有,完全通过写脚本的方式完成你需要的操作 Folder仅仅是进行组织管理 Multibranch Pipeline是结合了Git管理的流程,运行的脚本需要放在Git项目中,不过验证脚本正确性比较麻烦,还会在git中暴露Jenkins中的一些操作 目