Skip to content

Commit 5504511

Browse files
committed
📝 Writing docs.
1 parent 7ca6d87 commit 5504511

2 files changed

Lines changed: 252 additions & 3 deletions

File tree

docs/jenkins/basics/jenkins-installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ wget -O /opt/software/jenkins/jenkins.war http://mirrors.jenkins.io/war-stable/l
3131

3232
```
3333
cd /opt/software/jenkins
34-
java -jar jenkins.war --httpPort=8080
34+
nohup java -jar jenkins.war --httpPort=8080 >> nohup.out 2>&1 &
3535
```
3636

3737
## 脚本

docs/jenkins/jenkins-quickstart.md

Lines changed: 251 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,262 @@
1+
<!-- TOC -->
2+
3+
- [Jenkins 快速指南](#jenkins-%E5%BF%AB%E9%80%9F%E6%8C%87%E5%8D%97)
4+
- [概念](#%E6%A6%82%E5%BF%B5)
5+
- [Pipeline](#pipeline)
6+
- [Jenkinsfile](#jenkinsfile)
7+
- [创建 Pipeline](#%E5%88%9B%E5%BB%BA-pipeline)
8+
- [Jenkinsfile 简单实例](#jenkinsfile-%E7%AE%80%E5%8D%95%E5%AE%9E%E4%BE%8B)
9+
- [运行多步骤](#%E8%BF%90%E8%A1%8C%E5%A4%9A%E6%AD%A5%E9%AA%A4)
10+
- [简单实例](#%E7%AE%80%E5%8D%95%E5%AE%9E%E4%BE%8B)
11+
- [Linux, BSD, and Mac OS](#linux-bsd-and-mac-os)
12+
- [Windows](#windows)
13+
- [Timeouts, retries and more](#timeouts-retries-and-more)
14+
- [整理](#%E6%95%B4%E7%90%86)
15+
- [定义执行环境](#%E5%AE%9A%E4%B9%89%E6%89%A7%E8%A1%8C%E7%8E%AF%E5%A2%83)
16+
17+
<!-- /TOC -->
18+
119
# Jenkins 快速指南
220

3-
## Pipeline
21+
## 概念
22+
23+
### Pipeline
424

525
[Pipeline](https://jenkins.io/doc/book/pipeline/) 是一套插件,用来支持在 Jenkins 中实现和集成持续交付通道。
626

727
持续交付渠道是您从软件版本控制到用户和客户流程的自动化表达。
828

929
Pipeline 提供了一组可扩展的工具,通过 [Pipeline DSL](https://jenkins.io/doc/book/pipeline/syntax) 将“简单到复杂”的交付管道“作为代码”建模。
1030

11-
## Jenkinsfile
31+
### Jenkinsfile
1232

1333
Jenkins Pipeline 的定义通常写入一个文本文件,称为 Jenkinsfile,该文件又被检入到项目的源代码控制库中。
34+
35+
## 创建 Pipeline
36+
37+
1. 在代码仓库中创建 `Jenkinsfile`,内容参考 [Jenkinsfile 简单实例](#jenkinsfile-%E7%AE%80%E5%8D%95%E5%AE%9E%E4%BE%8B)
38+
39+
2. 点击 Jenkins 菜单中的**新建(New Item)**按钮。
40+
41+
3. 输入一个任务名称并选择 **Multibranch Pipeline**
42+
43+
4. 点击**增加源(Add Source)**按钮,选择代码仓库类型。
44+
45+
5. 点击**保存(Save)**按钮,然后观察第一个 Pipeline 运行。
46+
47+
### Jenkinsfile 简单实例
48+
49+
**Java**
50+
51+
```
52+
pipeline {
53+
agent { docker { image 'maven:3.3.3' } }
54+
stages {
55+
stage('build') {
56+
steps {
57+
sh 'mvn --version'
58+
}
59+
}
60+
}
61+
}
62+
```
63+
64+
**Node.js / JavaScript**
65+
66+
```
67+
pipeline {
68+
agent { docker { image 'node:6.3' } }
69+
stages {
70+
stage('build') {
71+
steps {
72+
sh 'npm --version'
73+
}
74+
}
75+
}
76+
}
77+
```
78+
79+
**Ruby**
80+
81+
```
82+
pipeline {
83+
agent { docker { image 'ruby' } }
84+
stages {
85+
stage('build') {
86+
steps {
87+
sh 'ruby --version'
88+
}
89+
}
90+
}
91+
}
92+
```
93+
94+
**Python**
95+
96+
```
97+
pipeline {
98+
agent { docker { image 'python:3.5.1' } }
99+
stages {
100+
stage('build') {
101+
steps {
102+
sh 'python --version'
103+
}
104+
}
105+
}
106+
}
107+
```
108+
109+
**PHP**
110+
111+
```
112+
pipeline {
113+
agent { docker { image 'php' } }
114+
stages {
115+
stage('build') {
116+
steps {
117+
sh 'php --version'
118+
}
119+
}
120+
}
121+
}
122+
```
123+
124+
## 运行多步骤
125+
126+
Pipelines 由多个步骤组成,允许您构建、测试和部署应用程序。Jenkins Pipeline 允许您以简单的方式撰写多个步骤,可以帮助您对任何类型的自动化过程建模。
127+
128+
想象一个“步骤”就像执行单个动作的单个命令一样。当一个步骤成功时,它将转到下一步。当一个步骤未能正确执行时,Pipeline 将失败。
129+
130+
当 Pipeline 中的所有步骤都成功完成, Pipeline 就被视作执行成功。
131+
132+
### 简单实例
133+
134+
#### Linux, BSD, and Mac OS
135+
136+
在 Linux,BSD 和 Mac OS(Unix-like)系统中,`sh` 步骤用于在管道中执行 shell 命令。
137+
138+
```
139+
pipeline {
140+
agent any
141+
stages {
142+
stage('Build') {
143+
steps {
144+
sh 'echo "Hello World"'
145+
sh '''
146+
echo "Multiline shell steps works too"
147+
ls -lah
148+
'''
149+
}
150+
}
151+
}
152+
}
153+
```
154+
155+
#### Windows
156+
157+
基于 Windows 的系统应该使用 `bat` 步骤来执行批处理命令。
158+
159+
```
160+
pipeline {
161+
agent any
162+
stages {
163+
stage('Build') {
164+
steps {
165+
bat 'set'
166+
}
167+
}
168+
}
169+
}
170+
```
171+
172+
#### Timeouts, retries and more
173+
174+
有一些有用的步骤可以“包装”其他步骤,这些步骤可以轻松解决复杂问题,例如重试(`retry`)步骤直至成功或退出步骤需要很长时间(`timeout`)。
175+
176+
```
177+
pipeline {
178+
agent any
179+
stages {
180+
stage('Deploy') {
181+
steps {
182+
retry(3) {
183+
sh './flakey-deploy.sh'
184+
}
185+
186+
timeout(time: 3, unit: 'MINUTES') {
187+
sh './health-check.sh'
188+
}
189+
}
190+
}
191+
}
192+
}
193+
```
194+
195+
`Deploy` 阶段重试 flakey-deploy.sh 脚本 3 次,然后等待最多 3 分钟执行 health-check.sh 脚本。如果运行状况检查脚本在 3 分钟内未完成,管道将在“部署”阶段被标记为失败。
196+
197+
子步骤(如 `retry``timeout`)可能包含其他步骤,包括 `retry``timeout`
198+
199+
我们可以组合这些步骤。例如,如果我们想重试我们的部署 5 次,但从未想过总共花费超过3分钟,然后才能进入阶段:
200+
201+
```
202+
pipeline {
203+
agent any
204+
stages {
205+
stage('Deploy') {
206+
steps {
207+
timeout(time: 3, unit: 'MINUTES') {
208+
retry(5) {
209+
sh './flakey-deploy.sh'
210+
}
211+
}
212+
}
213+
}
214+
}
215+
}
216+
```
217+
218+
#### 整理
219+
220+
当管道完成执行时,您可能需要运行清理步骤或根据管道的结果执行一些操作。这些操作可以在 post 部分中执行。
221+
222+
```
223+
pipeline {
224+
agent any
225+
stages {
226+
stage('Test') {
227+
steps {
228+
sh 'echo "Fail!"; exit 1'
229+
}
230+
}
231+
}
232+
post {
233+
always {
234+
echo 'This will always run'
235+
}
236+
success {
237+
echo 'This will run only if successful'
238+
}
239+
failure {
240+
echo 'This will run only if failed'
241+
}
242+
unstable {
243+
echo 'This will run only if the run was marked as unstable'
244+
}
245+
changed {
246+
echo 'This will run only if the state of the Pipeline has changed'
247+
echo 'For example, if the Pipeline was previously failing but is now successful'
248+
}
249+
}
250+
}
251+
```
252+
253+
## 定义执行环境
254+
255+
在上一节中,您可能已经注意到每个示例中的 agent 指令。agent 指令告诉 Jenkins 在哪里以及如何执行 Pipeline 或其子集。正如您所预料的那样,所有 Pipeline 都需要 `agent`
256+
257+
在引擎盖下面,代理原因发生了一些事情:
258+
259+
* 块中包含的所有步骤均由 Jenkins 排队等待执行。只要执行者可用,这些步骤就会开始执行。
260+
* 将分配一个工作空间,该工作空间将包含从源代码管理检出的文件以及 Pipeline 的任何其他工作文件。
261+
262+
Pipeline 旨在轻松使用 Docker 镜像和容器在内部运行。这允许 Pipeline 定义所需的环境和工具,而无需手动配置各种系统工具和代理依赖关系。这种方法使您可以使用任何可以打包在 Docker 容器中的工具。

0 commit comments

Comments
 (0)