[TOC]
1. Environment Variables
Environment variables can be seen as a media pipeline and Jenkins interaction. For example, you can know how to build BUILD_NUMBER variable number of tasks in the current build in the pipeline. Environment variables can be divided into Jenkins built-in variables and custom variables.
1.1 built-in variables
In the pipeline execution, Jenkins through a global variable named env, and the built environment variable Jenkins exposed. There are several methods of use thereof, for example:
pipeline {
agent any
stages {
stage('Example') {
steps {
echo "Running ${env.BUILDNUMBER} on ${env.JENKINS_URL}" # 方法1 推荐
echo "Running $env.BUILDNUMBER on $env.JENKINS_URL" # 方法2
echo "Running ${BUILDNUMBER} on ${JENKINS_URL}" # 方法3 不推荐,难排查
}
}
}
}
Through access <Jenkins master的地址>/pipeline-syntax/globals#env
to a complete list. In the list, when a variable is declared "For a multibranch project", represents the only multi-branch project will have this variable.
Following is a brief introduction of several variables in practice often used.
BUILD_ NUMBER
: Construction number, the accumulated number. When packing, which serves as part of the name of the article, for exampleserver-2.jar
.BRANCH_ NAME
: Multi-branch pipeline project support. When you need to do different things depending on the branch it will be used, for example, by publishing code will release branch into the production environment, master branch posted to the test environment.BUILD_ URL
: The current build of the page URL. If the build fails, you will need to build a link failure on the mail notification, this link will beBUILD _URL
.GIT_BRANCH
: Source project built by git pull will have this variable.
In use env
when variables need to pay attention to different types of projects, env
properties and their values of variables involved is not the same. Normal pipeline tasks such GIT_BRANCH
variable is origin/master
, in a multi-branch pipeline tasks GIT_BRANCH
is variable master
.
Therefore, in accordance with the pipeline when the branch logic processing different behaviors, need to pay attention.
1.2 custom pipeline environment variables
When the pipeline is complicated, we will have to define your own environment variables needs. Declarative pipeline provides environment
instructions for easy custom variables. such as:
pipeline {
agent any
environment {
CC = "clang"
}
stages {
stage("Example") {
environment {
DEBUG_FLAGS = "-g"
}
steps {
sh "${CC} ${DEBUG_FLAGS}"
sh "printenv"
}
}
}
}
environment
Instructions may be defined in a pipeline, the scope is the whole pipeline, as defined in the stage
phase, only the current stage
valid.
But these variables are not across the pipeline, such as pipeline a variable can not access the pipeline b. In the pipeline between the shared variable can be realized by parametric pipeline.
Environment variables refer to each other:
environment {
__server_name = 'mail-server'
__version = "${BUILD_NUMBER}"
__artifact_name = "${__server_name}-${__version}.jar"
}
Tips:
- When debugging pipeline, we can add that at the beginning of the pipeline:
sh 'printenv'
theenv
property value of the variable printed. This will help us avoid many problems. - Custom variables, in order to avoid naming conflicts, unity can add a prefix, such as according to the project or company
__server_name
,__
it is the prefix.
1.3 custom global environment variables
Define global environment variables can be cross-pipeline use.
Enter Jenkins → Manage Jenkins → Confiure System found Global properties → Check the "Environment variables" check box, and click "Add" button to input the value of variable names and input box.
Custom global environment variables will be added to env
the list of attributes, so you can directly use ${env.g_name}
references.
2. Build Tools
Construction refers to converting the source code into a binary process may be used. This process may include, but are not limited to these links: Download dependent, compile, package. The output of the build process - such as a zip package, which we call product (Some books were also called output). The warehouse management product, called product library.
2.1 build tool of choice
The choice of building tools, but also on the team acceptance of the tool itself. Advice is the same team in all projects use the same technology stack is a build tool.
2.2 tools
Instructions Introduction
tools
Command can help us to automatically download and install designated building tools, and add it to PATH
a variable. In this way, we can sh
directly use the steps in. But agent none
it does not take effect under the circumstances.
tools
The default command supports three tools: JDK, Maven, Gradle. By installing plug-in, tools
instructions can also support more tools. Next, we introduce several commonly used to build build environment.
2.3 JDK environment to build
Automatically install JDK 2.3.1
Enter Manage Jenkins → Global Tool Configuration → JDK page, click "Add JDK":
note
- It should oracle account verification.
- Jenkins will not immediately download the JDK, but will perform direct download operation to use when the pipeline.
2.3.2 custom JDK path
For security reasons, the company's network machines may not be able to access the Internet directly, using automatic download will fail. Then you need to prepare JDK on Jenkins agent, then specify the name and in the Manage Jenkins → Global Tool Configuration → JDK page, JAVA_HOME
path:
note
- When using the agent or Docker kubernetes agent, the agent may be used to install the base image JDK custom tailored to the mirror.
- Scripts can be automated, ie the use of
tools
pre-installed JDK, this script automatically ready ahead ofJAVA_HOME
the JDK.
2.4 Maven
2.4.1 Use Maven to build
Jenkins pipeline of tools to support instruction default Maven. Therefore, the use Maven only requires two steps.
-
Enter Manage Jenkins → Global Tool Configuration → Maven page, click "Add Maven":
- Maven version specified in Jenkinsfile and use the
mvn
command.
2.4.2 使用Managed files设置Maven
Maven默认使用的是其官方仓库,国内下载速度很慢。所以,我们通常会使用国内的Maven镜像仓库。这时就需要修改 Maven 的配置文件 settings.xml
。settings.xml
文件的默认路径为${M2_HOME}/conf/settings.xml
。但是,我们是不可能登录上Jenkins的机器,然后手动修改这个文件的。
Config File Provider插件能很好地解决这个问题。只需要在Jenkins的界面上填入settings.xml
的内容,然后在pipeline中指定settings.xml
就可以了。也就是说,对于不同的pipeline,可以使用不同的settings.xml
。
具体实现方法如下:
- 安装Config File Provider插件。
- 进入Manage Jenkins页面,就可以看到多出一个“Managed files”菜单。
-
单击“Managed files”进入,在左侧菜单栏中选择“Add a new Config”,就会看到该插件支持很多种配置文件的格式及方式,
- 选择“Global Maven settings.xml”选项。因为我们的设置是全局的。填写“ID”字段,Jenkins pipeline会引用此变量名。假如使用的ID为maven-global-settings。
- 在编辑页将自定义的Maven
settings.xml
的内容粘贴到“Content”字段中,
- 在Jenkins pipeline中使用的方法如下:
configFileProvider([configFile(fileId: "maven-global-settings", variable: "MAVEN_GLOBAL_ENV")]) {
sh "mvn -s $MAVEN_GLOBAL_ENV clean install"
}
2.5 Go语言环境搭建
Jenkins支持Golang的构建,只需要以下几步。
- 安装Go插件。
- 进入Manage Jenkins→Global Tool Configuration→Go页
- 在pipeline中加入
tools
部分。
pipeline {
agent none
environment {
GOPATH = "${env.WORKSPACE}/"
}
tools {
go 'go1.10'
}
stages {
stage('build') {
steps {
sh "go build"
}
}
}
}
此时,在环境变量中会增加一个GOROOT
变量。
- 设置
GOPATH
。了解Go语言开发的读者都会知道,编译时需要设置GOPATH
环境变量。直接在environment
指令中添加就可以了。
2.6 Python环境搭建
Python环境很容易产生Python版本冲突、第三方库冲突等问题。所以,Python开发通常会进行工程级别的环境隔离,也就是每个Python工程使用一个Python环境。
在Jenkins环境下,我们使用Pyenv Pipeline插件可以轻松地实现。
首先,准备Python基础环境。
- 在Jenkins机器上安装python、pip、virtualenv。
- pip:Python的包管理工具。
- virtualenv:Python中的虚拟环境管理工具。
- 安装Pyenv Pipeline插件。
然后,在pipeline中使用Pyenv Pipeline插件提供的withPythonEnv
方法。
withPythonEnv("/usr/bin/python") {
sh "python --version"
}
withPythonEnv
方法会根据第一个参数——可执行python路径——在当前工作空间下创建一个virtualenv环境。withPythonEnv
方法的第二个参数是一个闭包。闭包内的代码就执行在新建的virtualenv环境下。
3. 利用环境变量支持更多的构建工具
Not all build tools are required to install the appropriate plug-Jenkins can use.
Usually, developers do When setting up the development environment is: First installed on the machine tool building, then the building tools directory in your PATH environment variable.
If you want to support more Jenkins build tool, it is the same approach: build tools installed on Jenkins agent, and under its command executable directory record, and then adding the PATH environment variable need to use this command Jenkins pipeline in Contents of the executable commands. Examples are as follows:
pipeline {
agent none
environment {
PATH = "/usr/local/customtool/bin:$PATH"
}
stages {
stage('build') {
steps {
sh "customtool build"
}
}
}
}
There may also be another way:
pipeline {
agent none
environment {
CUSTOM_TOOL_HOME = "/usr/local/customtool/bin"
}
stages {
stage('build') {
steps {
sh "${CUSTOM_TOOL_HOME}/customtool build"
}
}
}
}
4. Use tools multi-scope version of the compiler
In practice, sometimes we need to be compiled with a source of multiple versions of the compiler. tools
In addition to supporting the instruction pipeline scopes, also supports stage
scope. So, we can achieve more in the compiled version of the same pipeline. code show as below:
pipeline {
agent none
stages {
stage('build with jdk-10.0.2') {
tools {
jdk "jdk-10.0.2"
}
steps {
sh "printenv"
}
}
stage('build with jdk-9.0.4') {
tools {
jdk "jdk-9.0.4"
}
steps {
sh "printenv"
}
}
}
}
In print out the log, you will find at each stage of the JAVA_HOME
value of the variable is different.
References:
[1] "Jenkins 2.x combat Guide"
[2] https://jenkins.io/zh/doc/book/pipeline/syntax/
[3] https://jenkins.io/zh/doc/ pipeline / steps /