Skip to content

Commit 32aa315

Browse files
committed
feature: Jenkins Pipeline
1 parent 4a9de20 commit 32aa315

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# 使用Jenkins实现自动部署
2+
3+
## 创建一个简单的 Pipeline
4+
5+
- 新建任务
6+
7+
![](http://cdn.heroxu.com/20190809156532043313763.png)
8+
9+
![](http://cdn.heroxu.com/20190809156532048520214.png)
10+
11+
- 配置一个简单的 Pipeline
12+
13+
```
14+
node {
15+
stage('Clone') {
16+
echo "1.Clone Stage"
17+
}
18+
stage('Test') {
19+
echo "2.Test Stage"
20+
}
21+
stage('Build') {
22+
echo "3.Build Stage"
23+
}
24+
stage('Push') {
25+
echo "4.Push Docker Image Stage"
26+
}
27+
stage('Deploy') {
28+
echo "5.Deploy Stage"
29+
}
30+
stage('通知') {
31+
echo "6.通知 Stage"
32+
}
33+
}
34+
```
35+
36+
![](http://cdn.heroxu.com/20190809156532070971237.png)
37+
38+
> Pipeline 下面👇的连接 `Pipeline Syntax` 有具体的语法信息,其他语法相关:[Jenkins入门](https://jenkins.io/zh/doc/book/pipeline/syntax/#parameters)。
39+
40+
- 构建
41+
42+
![](http://cdn.heroxu.com/20190809156532079324660.png)
43+
44+
- 查看Output
45+
46+
![](http://cdn.heroxu.com/20190809156532086027509.png)
47+
48+
49+
## 实际使用的 Pipeline
50+
51+
- Stage 1: Clone 代码
52+
53+
```
54+
stage('Clone') {
55+
echo "1.Clone Stage"
56+
git(
57+
branch: "${BRANCH}",
58+
credentialsId: 'fcc58f08-xxxx-xxxx',
59+
url : '[email protected]:test/pipeline.git',
60+
changelog: true
61+
)
62+
}
63+
```
64+
65+
因为有很多分支,所以分支通过参数来选择。
66+
67+
![](http://cdn.heroxu.com/20190809156532338664710.png)
68+
69+
- Stage 2: 测试
70+
71+
实际上是跳过了测试的。
72+
73+
```
74+
stage('Test') {
75+
echo "2.Test Stage"
76+
}
77+
```
78+
79+
- Stage 3: 构建镜像
80+
81+
```
82+
stage('Build') {
83+
echo "3.Build Docker Image Stage"
84+
def userInput = input(
85+
id: 'userInput',
86+
message: 'Choose a build environment',
87+
parameters: [
88+
[
89+
$class: 'ChoiceParameterDefinition',
90+
choices: "QA\nPre\nProd",
91+
name: 'DeployEnv'
92+
]
93+
]
94+
)
95+
if (userInput == "QA") {
96+
echo "Build a qa image. "
97+
sh "sed -i 's/-Pprod/-Pqa/g' ./provisioning/docker-compose-maven.yml"
98+
} else if (userInput == "Pre"){
99+
echo "Build a pre image. "
100+
} else {
101+
echo "Build a prod image. "
102+
}
103+
InputEnv = userInput
104+
sh "./provisioning/docker.sh build"
105+
}
106+
```
107+
108+
项目打包的脚本已经写好在项目的 `./provisioning/docker.sh`,所以直接调用。
109+
110+
打包的 `SpringBoot Jar包` 需要选择不同的环境来编译,所以提供一个环境选择的功能,实际过程:
111+
112+
![](http://cdn.heroxu.com/20190809156532385219791.png)
113+
114+
- Stage 4: 推送镜像
115+
116+
```
117+
stage('Push') {
118+
echo "4.Push Docker Image Stage"
119+
sh "./provisioning/docker.sh push"
120+
}
121+
```
122+
123+
- Stage 5: 部署
124+
125+
```
126+
stage('Deploy') {
127+
echo "5.Deploy Stage"
128+
}
129+
```
130+
131+
- Stage 6: 通知
132+
133+
```
134+
stage('通知') {
135+
echo "5.通知 Stage"
136+
bearychatSend channel: 'x-test', color: '#439FE0', message: "项目:test 构建完成 \n 分支:${BRANCH} \n 发布环境:${InputEnv}"
137+
}
138+
```
139+
140+
使用 `bearychat` 来做通知,部署完成之后将会受到通知消息。
141+
142+
![](http://cdn.heroxu.com/20190809156532393621182.png)
143+
144+
接入教程:使用 `bearychat机器人` 直接搜索 `Jenkins`。
145+
146+
- 完整 Pipeline
147+
148+
```
149+
node() {
150+
stage('Clone') {
151+
echo "1.Clone Stage"
152+
git(
153+
branch: "${BRANCH}",
154+
credentialsId: 'fcc58f08-xxxx-xxxx',
155+
url : '[email protected]:test/pipeline.git',
156+
changelog: true
157+
)
158+
}
159+
stage('Test') {
160+
echo "2.Test Stage"
161+
}
162+
stage('Build') {
163+
echo "3.Build Docker Image Stage"
164+
def userInput = input(
165+
id: 'userInput',
166+
message: 'Choose a build environment',
167+
parameters: [
168+
[
169+
$class: 'ChoiceParameterDefinition',
170+
choices: "QA\nPre\nProd",
171+
name: 'DeployEnv'
172+
]
173+
]
174+
)
175+
if (userInput == "QA") {
176+
echo "Build a qa image. "
177+
sh "sed -i 's/-Pprod/-Pqa/g' ./provisioning/docker-compose-maven.yml"
178+
} else if (userInput == "Pre"){
179+
echo "Build a pre image. "
180+
} else {
181+
echo "Build a prod image. "
182+
}
183+
InputEnv = userInput
184+
sh "./provisioning/docker.sh build"
185+
}
186+
stage('Push') {
187+
echo "4.Push Docker Image Stage"
188+
sh "./provisioning/docker.sh push"
189+
}
190+
stage('Deploy') {
191+
echo "5.Deploy Stage"
192+
}
193+
stage('通知') {
194+
echo "6.通知 Stage"
195+
bearychatSend channel: 'x-test', color: '#439FE0', message: "项目:test 构建完成 \n 分支:${BRANCH} \n 发布环境:${InputEnv}"
196+
}
197+
}
198+
```
199+
200+
201+
- 执行结果
202+
203+
![](http://cdn.heroxu.com/2019080915653240353453.png)
204+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@
66
### 工具篇
77

88
- [使用DockerCompose构建部署Yapi](./Program/工具篇/Yapi/使用DockerCompose构建部署Yapi.md)
9+
10+
11+
### CI/CD
12+
13+
#### Jenkins
14+
15+
- [使用Jenkins实现自动部署](./Program/CI&CD/Jenkins/使用Jenkins实现自动部署.md)

0 commit comments

Comments
 (0)