码迷,mamicode.com
首页 > 其他好文 > 详细

jenkins2 pipeline入门

时间:2016-07-05 15:31:21      阅读:854      评论:0      收藏:0      [点我收藏+]

标签:

 

本文通过简单的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.gitdef 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。

 

jenkins2 pipeline入门

标签:

原文地址:http://www.cnblogs.com/itech/p/5633948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!