@@ -227,8 +227,8 @@ pipeline {
227
227
environment {
228
228
gitUrl = "https://github.com/satan31415/heh_umi_template.git"
229
229
branchName = "master"
230
- projectBuildPath = "${env.WORKSPACE}/dist/ "
231
- nginxHtmlRoot = "/usr/share/nginx/react/ "
230
+ projectBuildPath = "${env.WORKSPACE}/dist"
231
+ nginxHtmlRoot = "/usr/share/nginx/react"
232
232
}
233
233
234
234
/*=======================================常修改变量-end=======================================*/
@@ -266,8 +266,8 @@ pipeline {
266
266
267
267
stage('Nginx Deploy') {
268
268
steps {
269
- sh "rm -rf ${nginxHtmlRoot}"
270
- sh "cp -r ${projectBuildPath} ${nginxHtmlRoot}"
269
+ sh "rm -rf ${nginxHtmlRoot}/ "
270
+ sh "cp -r ${projectBuildPath}/ ${nginxHtmlRoot}/ "
271
271
}
272
272
}
273
273
@@ -305,9 +305,9 @@ pipeline {
305
305
environment {
306
306
gitUrl = "https://gitee.com/youmeek/react-demo.git"
307
307
branchName = "master"
308
- projectBuildPath = "${env.WORKSPACE}/dist/"
309
- nginxHtmlRoot = "/usr/share/nginx/react/"
310
308
giteeCredentialsId = "上面全局凭据填写的 ID"
309
+ projectBuildPath = "${env.WORKSPACE}/dist"
310
+ nginxHtmlRoot = "/usr/share/nginx/react"
311
311
}
312
312
313
313
/*=======================================常修改变量-end=======================================*/
@@ -347,8 +347,8 @@ pipeline {
347
347
348
348
stage('Nginx Deploy') {
349
349
steps {
350
- sh "rm -rf ${nginxHtmlRoot}"
351
- sh "cp -r ${projectBuildPath} ${nginxHtmlRoot}"
350
+ sh "rm -rf ${nginxHtmlRoot}/ "
351
+ sh "cp -r ${projectBuildPath}/ ${nginxHtmlRoot}/ "
352
352
}
353
353
}
354
354
@@ -358,6 +358,194 @@ pipeline {
358
358
```
359
359
360
360
361
+ -------------------------------------------------------------------
362
+
363
+ ## Jenkins 后端 Spring Boot 项目构建
364
+
365
+ #### 安装 Maven
366
+
367
+ - [ 参考该文章] ( Maven-Install-And-Settings.md )
368
+
369
+ #### 配置工具
370
+
371
+ - 访问:< http://192.168.0.105:8080/configureTools/ >
372
+ - 我习惯自己安装,所以这里修改配置:
373
+ - **需要注意**:配置里面的 `别名` 不要随便取名字,后面 Pipeline 要用到的。在 tool 标签里面会用到。
374
+
375
+ ![ screencaptu] ( https://upload-images.jianshu.io/upload_images/12159-ef61595aebaa4244.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 )
376
+
377
+
378
+ #### 简单的 pipeline 写法(Jar 方式运行)(闭源项目 -- 码云为例)
379
+
380
+ ###### 先写一个控制 jar 脚本
381
+
382
+ - 来源:[ 鹏磊] ( https://segmentfault.com/a/1190000011504208 )
383
+ - 创建脚本:` vim /etc/rc.d/init.d/spring-boot.sh `
384
+ - 设置权限:` chmod 777 /etc/rc.d/init.d/spring-boot.sh `
385
+ - 脚本内容:
386
+
387
+
388
+ ```
389
+ #!/bin/bash
390
+
391
+ SpringBoot=$2
392
+
393
+ if [ "$1" = "" ];
394
+ then
395
+ echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
396
+ exit 1
397
+ fi
398
+
399
+ if [ "$SpringBoot" = "" ];
400
+ then
401
+ echo -e "\033[0;31m 未输入应用名 \033[0m"
402
+ exit 1
403
+ fi
404
+
405
+ function start()
406
+ {
407
+ count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
408
+ if [ $count != 0 ];then
409
+ echo "$SpringBoot is running..."
410
+ else
411
+ echo "Start $SpringBoot success..."
412
+ BUILD_ID=dontKillMe nohup java -jar $SpringBoot > /dev/null 2>&1 &
413
+ fi
414
+ }
415
+
416
+ function stop()
417
+ {
418
+ echo "Stop $SpringBoot"
419
+ boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
420
+ count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
421
+
422
+ if [ $count != 0 ];then
423
+ kill $boot_id
424
+ count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
425
+
426
+ boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
427
+ kill -9 $boot_id
428
+ fi
429
+ }
430
+
431
+ function restart()
432
+ {
433
+ stop
434
+ sleep 2
435
+ start
436
+ }
437
+
438
+ function status()
439
+ {
440
+ count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
441
+ if [ $count != 0 ];then
442
+ echo "$SpringBoot is running..."
443
+ else
444
+ echo "$SpringBoot is not running..."
445
+ fi
446
+ }
447
+
448
+ case $1 in
449
+ start)
450
+ start;;
451
+ stop)
452
+ stop;;
453
+ restart)
454
+ restart;;
455
+ status)
456
+ status;;
457
+ *)
458
+
459
+ echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {start|stop|restart|status} {SpringBootJarName} \033[0m\033[0;31m Example: \033[0m\033[0;33m sh $0 start esmart-test.jar \033[0m"
460
+ esac
461
+ ```
462
+
463
+
464
+ ###### 配置 Jenkins
465
+
466
+ - ** 必须** :新增一个全局凭据,方法参考前端部分
467
+
468
+ ```
469
+ pipeline {
470
+ agent any
471
+
472
+ /*=======================================工具环境修改-start=======================================*/
473
+ tools {
474
+ jdk 'JDK8'
475
+ maven 'MAVEN3'
476
+ }
477
+ /*=======================================工具环境修改-end=======================================*/
478
+
479
+ options {
480
+ timestamps()
481
+ disableConcurrentBuilds()
482
+ buildDiscarder(logRotator(
483
+ numToKeepStr: '20',
484
+ daysToKeepStr: '30',
485
+ ))
486
+ }
487
+
488
+ /*=======================================常修改变量-start=======================================*/
489
+
490
+ environment {
491
+ gitUrl = "https://gitee.com/youmeek/springboot-jenkins-demo.git"
492
+ branchName = "master"
493
+ giteeCredentialsId = "Gitee"
494
+ projectWorkSpacePath = "${env.WORKSPACE}"
495
+ projectBuildTargetPath = "${env.WORKSPACE}/target"
496
+ projectJarNewName = "${env.JOB_NAME}.jar"
497
+
498
+ }
499
+
500
+ /*=======================================常修改变量-end=======================================*/
501
+
502
+ stages {
503
+
504
+ stage('Pre Env') {
505
+ steps {
506
+ echo "======================================项目名称 = ${env.JOB_NAME}"
507
+ echo "======================================项目 URL = ${gitUrl}"
508
+ echo "======================================项目分支 = ${branchName}"
509
+ echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
510
+ echo "======================================项目空间文件夹路径 = ${projectWorkSpacePath}"
511
+ echo "======================================项目 build 后 jar 路径 = ${projectBuildTargetPath}"
512
+ }
513
+ }
514
+
515
+ stage('Git Clone'){
516
+ steps {
517
+ git branch: "${branchName}",
518
+ credentialsId: "${giteeCredentialsId}",
519
+ url: "${gitUrl}"
520
+ }
521
+ }
522
+
523
+ stage('Maven Clean') {
524
+ steps {
525
+ sh "mvn clean"
526
+ }
527
+ }
528
+
529
+ stage('Maven Package') {
530
+ steps {
531
+ sh "mvn package -DskipTests"
532
+ }
533
+ }
534
+
535
+ stage('Spring Boot Run') {
536
+ steps {
537
+ sh "mv ${projectBuildTargetPath}/*.jar ${projectBuildTargetPath}/${projectJarNewName}"
538
+ sh "cp ${projectBuildTargetPath}/${projectJarNewName} /opt/"
539
+ sh "cp /etc/rc.d/init.d/spring-boot.sh /opt/"
540
+ sh "chmod 777 /opt/spring-boot.sh"
541
+ sh "bash /opt/spring-boot.sh restart ${projectJarNewName}"
542
+ }
543
+ }
544
+
545
+ }
546
+ }
547
+ ```
548
+
361
549
362
550
-------------------------------------------------------------------
363
551
0 commit comments