Jenkins2 Pipeline Getting Started

Source: Internet
Author: User

This article, with a simple pipeline example and a detailed explanation, can learn basic pipeline and groovy, and then start implementing your own pipeline job.

Translated and modified from: https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md

1. Install Java,maven, configure Jenkins

Install Java and 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"

Configuring environment variables in Jenkins's global tool configuration

2. Create a simple pipeline job

The groovy code is as follows:

Node {
git url: ' https://github.com/jglick/simple-maven-project-with-tests.git '
def mvnhome = Tool ' M3 '
SH "${mvnhome}/bin/mvn-b Verify"
}

The code above implements the source code from GitHub Checkout, then builds with MAVEN, the code contains the test case, it is possible to randomly fail, if there is a test case failure, the entire pipeline job will be marked as failed.

The above example assumes that the Linux system, if it is a Windows system, needs to be modified to

"${mvnhome}\\bin\\mvn-B Verify"    

3. Understanding Basic Groovy Usage

Node,node is used to select the machine that the groovy runs on, as long as node has a usable executor,node{} task will be run on the selected machine, and will be created on the selected machine workspace. Many of the step must be executed in node, such as Git,sh, which must be executed in the node environment.

Unlike user-defined functions, pipeline step always accepts named arguments, the parentheses can be omitted, or you can use standard groovy syntax to pass in a map as a parameter, for example:

' Https://github.com/jglick/simple-maven-project-with-tests.git'
' Https://github.com/jglick/simple-maven-project-with-tests.git 'master' 
Git ([' https://github.com/jglick/simple-maven-project-with-tests.git' Master'])

If there is only one mandatory parameter, you can omit the parameter name, as follows two equivalent effects:
' Echo Hello'
SH ([' echo Hello'])

Def can define a groovy variable, the tool can check if a given name's tools are accessible, use a variable in double quotes, and the variable will be replaced with the actual value:
' M3'
"${mvnhome}/bin/mvn-b Verify"  

4. Use of environment variables

The simplest way to use the tool is to add the tool path to the path, and Env can modify the environment variables of the node corresponding machine, and the steps can see the changes of the environment variables later.

Node {  ' https://github.com/jglick/simple-maven-project-with-tests.git  '     ' M3' env.    "${mvnhome}/bin:${env. PATH}' mvn-b verify'}

Jenkins's job default environment variable, you can view the job's default environment variables through http://jenkins-server/job/javahelloworld/pipeline-syntax/globals.

Branch_name
For a multibranch project, this would be set to the name of the branch being built, for example in case you wish to deploy To production from Master and not from feature branches.
change_id
For a multibranch project corresponding to some kind of change request, this would 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 is the set to the change URL.
Change_title
For a multibranch project corresponding to some kind of change request, this is set to the title of the change.
Change_author
For a multibranch project corresponding to some kind of change request, this would 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 is the set to the human name of the author.
Change_author_email
For a multibranch project corresponding to some kind of change request, this would be set to the email address of the Autho R.
Change_target
For a multibranch project corresponding to some kind of change request, this would be set to the target or base branch to W Hich the change could is 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 olde R Builds
Build_display_name
The display name of the current build, which are 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 in 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 Buil D. This is the number of the "Build executor status", except that the number starts from 0, not 1.
Node_name
Name of the agent if the the build is on an agent, or "master" if run on master
Node_labels
whitespace-separated List of labels that's 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 the Jenkins, like http://server:port/jenkins/(note:only available if Jenkins URL set in System configuration)
Build_url
Full URL of the This build, like http://server:port/jenkins/job/foo/15/(Jenkins URL must is set)
Job_url
Full URL of the This job, like http://server:port/jenkins/job/foo/(Jenkins URL must is set)

For example, you can use env in step in node. Build_tag.

Jenkins2 Pipeline Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.