6場景代碼
6.1 Svn提交後觸發Jenkins自動構建
6.1.1 方法1
*基本思路:subversion+scm poll兩個插件來監控,來監控SVN上面是否變更(是Jenkins主動監控SVN)
https://blog.csdn.net/modoo_junko/article/details/37593225
6.1.2 方法2
*基本思路:SVN代碼變動後觸動post-commit,然後觸動Jenkins腳本進行構建操作
https://blog.csdn.net/q13554515812/article/details/86651851
6.2利用tools作用域實現多版本編譯
在實際工作中,有時需要對同一份源碼使用多個版本的編譯器進行編譯。tools指令除了支持pipeline作用域,還支持stage作用域。所以,我們可以在同一個pipeline中實現多版本編譯。代碼如下:
pipeline {
agent none
stages {
stage('build with jdk-10.0.2') {
tools {
jdk "jdk-10.0.2"
}
steps {
sh "printenv"
}
}
stage('build with jdk-9.0.4') {
tools {
jdk "jdk-9.0.4"
}
steps {
sh "printenv"
}
}
}
}
[1] 《Jenkins 2.x實戰指南》
[2] https://blog.51cto.com/ygqygq2/2446717
[3] https://jenkins.io/zh/doc/book/pipeline/syntax/
[4] https://jenkins.io/zh/doc/pipeline/steps/
6.2 docker
[4] http://www.eryajf.net/3298.html
6.2.1常規用法。
日常構建部署中,常常會用到編譯工作,因此這裏的步驟可以放入到鏡像中執行,這裏利用node項目的編譯舉一個示例:
pipeline {
agent {
docker {
lable 'docker'
image 'registry.cn-hangzhou.aliyuncs.com/eryajf/node:11.15'
}
}
stages {
stage('Build') {
steps {
sh 'npm install --registry=https://registry.npm.taobao.org'
}
}
}
}
當流水線執行的時候,Jenkins會首先將代碼更新,然後將 $WORKSPACE 掛載到容器之中,這個可以通過構建日誌中看出來:
docker run -t -d -u 0:0 -w /root/.jenkins/workspace/test-pinpeline -v /root/.jenkins/workspace/test-pinpeline:/root/.jenkins/workspace/test-pinpeline:rw,z -v /root/.jenkins/workspace/test-pinpeline@tmp:/root/.jenkins/workspace/test-pinpeline@tmp:rw,z -e ******** registry.cn-hangzhou.aliyuncs.com/eryajf/node:11.15
接着執行我們定義的install編譯命令,當然,下邊可以繼續添加步驟,將代碼rsync到遠程主機,或者其他操作。
此處可用的參數如下:
- lable(可選):定義一個標籤,用處不十分大。
- image:定義構建時使用的鏡像。
- args:可以定義類似docker run命令時的一些參數。
- alwaysPull:布爾類型,是否在每次運行的時候重新拉取鏡像。
6.2.2添加參數。
日常編譯比較影響構建時間的一個因素是本地緩存,因此還可以用如下方式將本地緩存掛載到容器之中,從而加快構建速度。
pipeline {
agent {
docker {
image 'registry.cn-hangzhou.aliyuncs.com/eryajf/node:11.15'
args '-v /root/.npm:/root/.npm'
}
}
stages {
stage('Build') {
steps {
sh 'npm install --registry=https://registry.npm.taobao.org'
}
}
}
}
6.2.3多個容器。
基於如上的事實,發散我們的思維,我們可以將許多個基礎性的步驟,封裝成一個又一個鏡像,然後在每個步驟中只需調用封裝好的鏡像,傳入所需的參數,即可完成比較複雜的構建。
多個容器用法,如下簡示:
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'registry.cn-hangzhou.aliyuncs.com/eryajf/node:11.15'
args '-v /root/.npm:/root/.npm'
}
}
steps {
sh 'npm install --registry=https://registry.npm.taobao.org'
}
}
stage('deploy'){
agent {
docker {
image 'registry.cn-hangzhou.aliyuncs.com/mckj/ansible'
}
}
steps {
sh 'echo "通過ansbile進行部署"'
}
}
}
}
因爲Jenkins默認會將$WORKSPACE掛載到兩個容器中,因此各種操作直接基於當前目錄即可,這是值得注意的一點。
這裏只是做一個演示,提供一種思路,以後有需要,可以藉此發揮更多的妙用。
6.2.4基於Dockerfile。
這種方式,應用的大概比較少,僅做簡單介紹。
首先在項目根目錄創建一個Dockerfile:
FROM eryajf/centos:7.5
MAINTAINER eryajf <[email protected]>
<[email protected]># Install node
<[email protected]>ADD node-v11.15.0-linux-x64.tar.xz /usr/local/
<[email protected]>ENV NODE /usr/local/node-v11.15.0-linux-x64
<[email protected]>ENV PATH $PATH:$NODE/bin
<[email protected]>RUN npm i -g pm2 gulp yarn --registry=https://registry.npm.taobao.org
然後定義Jenkinsfile:
pipeline {
agent { dockerfile true }
stages {
stage('Build') {
steps {
sh 'npm install --registry=https://registry.npm.taobao.org'
}
}
}
}
感覺起來,這種方式引入了過多的不確定因素,對於構建穩定性,反而不是一件好事兒,所以不太推薦。