---
title: "【Jenkins Pipeline】安装与基本使用"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/jenkins-pipeline-install-basics/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/03-软件工程与质量保障/CI-CD与质量保障/CI-CD工具链/【Jenkins Pipeline】安装与基本使用.md"
content_hash: 9d372c00d678a344422b5b75ebd0e16a0602e900285a6576cee18ffe6c79ce59
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 【Jenkins Pipeline】安装与基本使用

> 创建时间：2020/9/16 17:02

  * 【Jenkins Pipeline】安装与基本使用
    * 相关概念
      * 软件配置管理 （Software Configuration Management, SCM）
      * CI（Continuous integration, 持续集成）
    * Jenkins
      * 安装配置
        * 下载
        * 运行
    * Pipeline
      * Jenkinsfile
      * 语法
        * agent 分配节点
        * stage
        * post
        * parameters
        * options
        * environment
      * 使用步骤
      * 其他操作
      * 遇到的坑
      * 时间配置
      * 脚本示例
    * Ref

## 相关概念

### 软件配置管理 （Software Configuration Management, SCM）

参考[立师兄Linty的博文](https://www.jianshu.com/p/f8d81680a7cf)

> **IEEE定义** A discipline applying technical and administrative direction and surveillance to identify and document the functional and physical characteristics of a configuration item, control changes to those characteristics, record and report change processing and implementation status, and verify compliance with specified requirements.
>
> 软件配置管理，是关于软件资产的管理。
>  软件 = 源代码 + 文档。
>  源代码、设计文档、可以运行的程序等在软件研发过程中产生的有价值的东西，都是软件资产。

是一种应用于整个软件工程过程的标识、组织和控制修改的围绕软件资产的管理技术。

软件配置管理，又称软件形态管理、或软件建构管理，简称软件形管。界定软件的组成项目，对每个项目的变更进行管控（版本控制），并维护不同项目之间的版本关联，以使软件在开发过程中任一时间的内容都可以被追溯，包括某几个具有重要意义的数个组合。

特点：

  * 软件也是配置起来的。各个源文件、源代码和正确的文档搭配起来，编译产生正确的可以运行的程序。

  * 软件更容易发生变化，是向前演进的。

  * 软件的相关性（耦合）更高，一旦需要改动，通常不是只更改一个文件。

git就是一种SCM工具

### CI（Continuous integration, 持续集成）

指的是，频繁地（一天多次）将代码集成到主干。
持续集成工具让产品可以快速迭代，同时还能保持高质量，可以快速的发现错误，防止分支大幅偏离主干。

## Jenkins

### 安装配置

#### 下载

<http://updates.jenkins-ci.org/latest/jenkins.war>

#### 运行

两种运行方式：一种是基于tomcat、Jdk启动，一种是基于Docker启动

基本的java启动jenkins:

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

jenkins启动默认8080端口

* * *

关闭终端也不挂起：

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

> nohup 英文全称 no hang up（不挂起），用于在系统后台不挂断地运行命令，退出终端不会影响程序的运行。
>  nohup 命令，在默认情况下（非重定向时），会输出一个名叫 nohup.out 的文件到当前目录下，如果当前目录的 nohup.out 文件不可写，输出重定向到 $HOME/nohup.out 文件中。
>  via. <https://www.runoob.com/linux/linux-comm-nohup.html>

* * *

Start Jenkins and Installing Jenkins as a Windows service
<https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+as+a+Windows+service>

* * *

Use the Jenkins Windows installer

> That program will install Jenkins as a service using a 64 bit JVM chosen by the user. Keep in mind that to run Jenkins as a service, the account that runs Jenkins must have permission to login as a service.

<https://www.jenkins.io/doc/book/installing/windows/>

* * *

基于Tomcat启动：<https://www.cnblogs.com/jimmyshan-study/p/11982415.html>
将jenkins.war放到Tomcat\webapps目录下，当启动tomcat的时候，Jenkins会随着服务器一起启动 ；

## Pipeline

pipeline 是一个Jenkins插件
作用：作用：将独立运行于单个或者多个（节点的）任务连接起来，实现
单个任务难以完成的复杂流程编排与可视化。

### Jenkinsfile

Jenkinsfile是Jenkins2.x核心特性Pipeline的脚本，由Groovy语言实现。

内部记录的内容是一套Jenkins 执行的流程，支持并行执行，Jenkinsfile 文件一般存放于代码仓库的根目录，即使Jenkins 需要重装或者安装Jenkins 的机器出了问题，Jenkinsfile 所记录的流程也可以很迅速的重新部署。

Jenkinsfile 有2 种写法

  * Declarative 通常Declarative 的写法可以满足大部分场景的需要，相对来说学习成本更低

  * Groovy 写法更灵活，比如Jenkins 的Shared Library 模式就需要用到Groovy

### 语法

#### agent 分配节点

any 在任何可用的节点上执行Pipeline， 默认master
none 没有分配，子stage当中分配
label 指定节点

#### stage

一旦某个stage执行失败，所有的stage都会停止。post语句照常执行

#### post

和stages平级，构建状态的，预设的语句块
数据清理、发送邮件等

  * always: 不管构建状态怎么样，始终会运行

  * aborted { … } 当构建状态“中止”时运行（手动强行x掉）

  * success { … } 如果构建状态为“成功”或尚未设置，则运行

  * failure { … } 如果构建状态为“失败”，则运行

#### parameters

全局唯一，定义参数，在BuildWithParameters选项卡当中设置参数值。

文本参数支持换行

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

#### options

指令允许从流水线内部配置特定于流水线的选项，全局唯一

  * disableConcurrentBuilds() 禁止并发

  * timestamps() 打印时间戳

  * timeout(time:5, unit:’SECONDS’)

> timeout(5) == timeout(time:5, unit:’MINUTES’)
>  timeout(activity:true, time:5, unit:’SECONDS’)
>  activity: 此块的日志中没有活动之后的超时，而不是绝对持续时间。

#### environment

environment 指令制定一个 键-值对序列，该序列将被定义为所有步
骤的环境变量，或者是特定于阶段的步骤， 这取决于 environment
指令在流水线内的位置。

### 使用步骤

  1. 新建流水线

  2. 写Pipeline脚本

> Pipeline脚本书写位置
>
>   * 在pipeline 的配置界面中，写下pipeline语法
>
>   * 仓库中的Jenkinsfile 文件（一般放在根目录中）
>
>

>
> 将脚本写在码云或Github上的Jenkinsfile中

选择 Pipeline script from SCM, 填写仓库地址，

  3. 点击立即构建，就会执行pipeline

> Jenkins执行的步骤
>  在给定的代码仓库中检出jenkinsfile文件，读取文件并执行
>  有+号的行，代标要执行的语句

### 其他操作

启动Jenkins
java -jar jenkins.war 启动jenkins服务器

重启Jenkins
<http://localhost:8080/restart>

重载Jenkins配置信息
<http://localhost:8080/reload>

停止Jenkins服务器
方法1： Terminal and Kill Process
`bash
$ps -e | grep jenkins
56 ?? 1:22.88 /usr/bin/java -Dfile.encoding=UTF-8 -XX:PermSize=256m -XX:MaxPermSize=512m -Xms256m -Xmx512m -Djava.io.tmpdir=/Users/[redacted-user]/Jenkins/tmp -jar /Applications/Jenkins/jenkins.war --httpPort=8080
4981 ttys001 0:00.00 grep jenkins
$sudo kill 56
`
方法2：通过URL
<http://localhost:8080/exit>

### 遇到的坑

  1. 找不到sh、nopub等命令

在本地环境变量中配置git程序的地址
重启jenkins，才能更新配置信息。

### 时间配置

Jenkins采用了著名的UNIX任务调度工具CRON所使用的配置方式。用5个字段代表5个不同的时间单位（中间用空格隔开）：

分 时 日 月 星期几

实例1：每1分钟执行一次myCommand

  *     *       *         *           * myCommand
实例2：每小时的第3和第15分钟执行
3,15 * * * * myCommand
实例3：在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand
实例4：每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 _/2_ * myCommand
实例5：每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 myCommand
实例6：每晚的21:30重启smb
30 21 * * * /etc/init.d/smb restart
实例7：每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart
实例8：每周六、周日的1 : 10重启smb
10 1 * * 6,0 /etc/init.d/smb restart
实例9：每天18 : 00至23 : 00之间每隔30分钟重启smb
0,30 18-23 * * * /etc/init.d/smb restart
实例10：每星期六的晚上11 : 00 pm重启smb
0 23 * * 6 /etc/init.d/smb restart
实例11：每一小时重启smb

  * _/1_ * * /etc/init.d/smb restart
实例12：晚上11点到早上7点之间，每隔一小时重启smb
0 23-7 * * * /etc/init.d/smb restart

### 脚本示例

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

## Ref

<https://www.jianshu.com/p/f8d81680a7cf>
