Jenkins構建Pipeline工程
做者:星晴(當地小有名氣,小到只有本身知道的杰倫粉)java
1.添加插件
1.Maven Integration plugin 2.Publish over ssh 3.pipeline 4.git parameter
2.系統配置
Name: 服務名稱(隨便寫) Hostname:主機IP Username: 主機登陸帳戶 Remote Directory: 遠程目錄 Passphrase / Password: 主機登陸密碼
3.建立一個Pipeline工程
3.1 general配置
分支切換node
3.2 流水線
3.2.1 pipeline script git
node { environment { JENKINS_WORKSPACE = "/var/jenkins_mount/workspace" //jenkins存放文件的地址 PROJECT_NAME = "${JOB_NAME}" // 項目名 JAR_NAME = "bnb-system-*-beta.jar" // 項目生成的jar的名字 VERSION_ID = "${BUILD_ID}" } stage('拉取代碼') { echo 'this is pull code step' git branch: "${branchs}", url: 'https://github.com/joe-tong/springboot_demo2.git' } stage('編譯打包') { echo 'this is build step' sh '/var/jenkins_home/apache-maven-3.6.3/bin/mvn clean package -Dmaven.test.skip=true' } stage('推送') { echo 'transfer file to target server' sshPublisher(publishers: [ sshPublisherDesc(configName: '本地', transfers: [ sshTransfer(cleanRemote: false, excludes: '', execCommand:''' #!/bin/bash cd /usr/local/jenkins/workspace/target jarname=`ls -t | grep springboot*.jar | grep -v 'sources'| awk '{print $1}'|head -n 1` mv /usr/local/jenkins/workspace/target/${jarname} /usr/local/jenkins/workspace/${jarname} kill_id=`netstat -tunlp|grep 8443|awk \'{print $7}\'|awk -F \'/\' \'{print $1}\'` if [ -n "$kill_id" ] then echo "kill -9 的pid:" $kill_id kill -9 $kill_id fi sleep 1s kill_debug_id=`netstat -tunlp | grep 8060 | awk \'{print $7}\'|awk -F \'/\' \'{print $1}\'` if [ -n "$kill_debug_id" ] then echo "kill -9 kill_debug_id:" $kill_debug_id kill -9 $kill_debug_id fi sleep 1s echo "==============應用啓動 --> ${jarname} ==============" source /etc/profile nohup java -jar /usr/local/jenkins/workspace/${jarname} --spring.profiles.active=dev >> /usr/local/jenkins/workspace/springboot.log & sleep 1s tail -f /usr/local/jenkins/workspace/springboot.log ''' , execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/springboot_demo*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false) ]) println "==========結束===========" } }
3.2.2 piple script from scmgithub
配置 git倉庫spring
jenkinsfile 在項目根目錄下docker
node { environment { JENKINS_WORKSPACE = "/var/jenkins_mount/workspace" //jenkins存放文件的地址 PROJECT_NAME = "${JOB_NAME}" // 項目名 JAR_NAME = "bnb-system-*-beta.jar" // 項目生成的jar的名字 VERSION_ID = "${BUILD_ID}" } stage('拉取代碼') { echo 'this is pull code step' checkout scm } stage('編譯打包') { echo 'this is build step' sh '/var/jenkins_home/apache-maven-3.6.3/bin/mvn clean package -Dmaven.test.skip=true' } stage('推送') { echo 'transfer file to target server' sshPublisher(publishers: [ sshPublisherDesc(configName: '本地', transfers: [ sshTransfer(cleanRemote: false, excludes: '', execCommand:''' #!/bin/bash cd /usr/local/jenkins/workspace/target jarname=`ls -t | grep springboot*.jar | grep -v 'sources'| awk '{print $1}'|head -n 1` mv /usr/local/jenkins/workspace/target/${jarname} /usr/local/jenkins/workspace/${jarname} kill_id=`netstat -tunlp|grep 8443|awk \'{print $7}\'|awk -F \'/\' \'{print $1}\'` if [ -n "$kill_id" ] then echo "kill -9 的pid:" $kill_id kill -9 $kill_id fi sleep 1s kill_debug_id=`netstat -tunlp | grep 8060 | awk \'{print $7}\'|awk -F \'/\' \'{print $1}\'` if [ -n "$kill_debug_id" ] then echo "kill -9 kill_debug_id:" $kill_debug_id kill -9 $kill_debug_id fi sleep 1s echo "==============應用啓動 --> ${jarname} ==============" source /etc/profile nohup java -jar /usr/local/jenkins/workspace/${jarname} --spring.profiles.active=dev >> /usr/local/jenkins/workspace/springboot.log & sleep 1s tail -f /usr/local/jenkins/workspace/springboot.log ''' , execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/springboot_demo*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false) ]) println "==========結束===========" } }
3.3.3apache
#!/bin/sh #docker 鏡像/容器名字或者jar名字 這裏都命名爲這個 SERVER_NAME=springboot #容器id CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}') #鏡像id IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}') #當前日期 DATE=`date +%Y%m%d` #清除舊容器 if [ -n "$CID" ]; then echo "存在$SERVER_NAME容器,CID=$CID" echo "中止舊容器" docker stop $SERVER_NAME echo "刪除舊容器" docker rm $SERVER_NAME fi # 清楚舊鏡像 if [ -n "$IID" ]; then echo "存在$SERVER_NAME鏡像,IID=$IID" echo "刪除鏡像" docker rmi $IID fi #構建鏡像 echo "開始構建鏡像" docker build -f ./src/main/docker/Dockerfile -t $SERVER_NAME:v${DATE} ./target echo "構建鏡像成功!" # 運行docker容器 echo "建立並啓動$SERVER_NAME容器..." docker run --name $SERVER_NAME -d -p 8080:8443 $SERVER_NAME:v${DATE} echo "$SERVER_NAME容器啓動完成"
關注公衆號,有更多好玩的等着你!!!springboot