環境:(軟體部署過程省略...)
192.168.2.50 jenkins
192.168.2.33 jenkins-slave tomcat
192.168.2.51 git-server
1、建立git 倉庫 ,放入原始碼192.168.2.51 上:
[root@git ~]# su - gitLast login: Fri Jun 12 03:59:22 EDT 2020 on pts/0[git@git ~]$ lsrepos[git@git ~]$ cd repos/[git@git repos]$ lsapp.git blog.git jenkinsfile.git[git@git repos]$ mkdir test1.git[git@git repos]$ cd test1.git/[git@git test1.git]$ git --bare initInitialized empty Git repository in /home/git/repos/test1.git/[git@git test1.git]$ lsbranches config description HEAD hooks info objects refs
192.168.2.50 客戶端 上傳程式碼到master 分支上[root@jenkins ~]# git clone [email protected]:/home/git/repos/test1.gitCloning into 'test1'...warning: You appear to have cloned an empty repository.[root@jenkins ~]# git clone https://gitee.com/jflyfox/jfinal_cms.gitCloning into 'jfinal_cms'...remote: Enumerating objects: 6435, done.remote: Counting objects: 100% (6435/6435), done.remote: Compressing objects: 100% (3221/3221), done.remote: Total 6435 (delta 2868), reused 6320 (delta 2790), pack-reused 0Receiving objects: 100% (6435/6435), 39.62 MiB | 10.26 MiB/s, done.Resolving deltas: 100% (2868/2868), done.[root@jenkins ~]# lsapache-maven-3.5.3-bin.tar.gz app blog jdk-8u201-linux-x64.tar.gz jenkinsfile jfinal_cms test1[root@jenkins ~]# cd jfinal_cms/[root@jenkins jfinal_cms]# lsApache v2 License.license ChangeLog.md doc pom.xml README.md sql src[root@jenkins jfinal_cms]# mv ./* ../test1/[root@jenkins jfinal_cms]# ls ../test1Apache v2 License.license ChangeLog.md doc pom.xml README.md sql src[root@jenkins jfinal_cms]#cd ../test1[root@jenkins test1]# git add .[root@jenkins test1]# git commit -m "test1"[root@jenkins test1]# git push origin masterCounting objects: 2527, done.Delta compression using up to 2 threads.Compressing objects: 100% (2415/2415), done.Writing objects: 100% (2527/2527), 22.14 MiB | 9.08 MiB/s, done.Total 2527 (delta 375), reused 0 (delta 0)To [email protected]:/home/git/repos/test1.git* [new branch] master -> master
2、編寫pipeline 流水線[root@jenkins item-b]#cd /root/jenkinsfile/item-b
[root@jenkins item-b]# vim Jenkinsfile-java
node ("slave-192.168.2.33") {//指定節點執行stage('git checkout') {checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:/home/git/repos/test1.git']]])}//指定git 地址;拉取程式碼,使用jenkins拉下來一個repo在jenkins的workspace中stage('maven build') {sh '''export JAVA_HOME=/usr/local/jdk1.8/usr/local/maven3.5/bin/mvn clean package -Dmaven.test.skip=true'''}//maven構建程式碼stage('deploy') {sh '''JENKINS_NODE_COOKIE=dontkillmeexport JAVA_HOME=/usr/local/jdk1.8TOMCAT_NAME=tomcatTOMCAT_HOME=/usr/local/$TOMCAT_NAMEWWWROOT=$TOMCAT_HOME/webapps/ROOTif [ -d $WWWROOT ]; thenmv $WWWROOT /data/backup/${TOMCAT_NAME}-$(date +"%F_%T")fiunzip ${WORKSPACE}/target/*.war -d $WWWROOTPID=$(ps -ef |grep $TOMCAT_NAME |egrep -v "grep|$$" |awk \\'{print $2}\\')[ -n "$PID" ] && kill -9 $PID$TOMCAT_HOME/bin/startup.sh'''// JENKINS_NODE_COOKIE=dontkillme避免jenkins自動關閉衍生的子程序;}stage('test') {sh "/usr/bin/echo 'hello word'"}}
上傳pipeline 到git倉庫中
新增一個引數化構建過程,選擇字元引數
指定git 倉庫中,pepeline 的位置
寫明 指令碼路徑
儲存
構建成功,我們來測試下
5、測試
最新評論
延伸閱讀
- OpenShift 4 HOL (6) 基於Gogs+Nexus+Sonarqube的CI/CD Pipeline
- 有了這個開源 Java 專案,開發出炫酷的小遊戲好像不難?
- 精華帖:最全的程式設計自學網站介紹,白嫖
- 優雅停止 SpringBoot 服務,拒絕 kill -9 暴力停止
- mall-h5 java vue商城
- 老許,你要轉陣 Flutter不?你開了金口,面試題現在就給你送來
- 使用Jenkins一鍵打包部署SpringBoot應用,就是這麼6
- docker隨手筆記第八節 jenkins通過git自動部署java微服務
- 雲程式設計,這是我見過最優雅的Web雲端整合開發IDE——Cloud Studio