目前持續集成的生態越來越完善,工具也有很多,開源的或商業的。如:
- 最最流行的,也是使用最多的 Jenkins
- 有着持續集成DNA的ThoughtWorks GO。理念:"Deployment as pipeline" (華為容器平台應該是基於GO做的二次開發實現)
- Atlassian工具鏈之一的Bamboo (數人雲應該是基於Banboo實現的CI/CD)
- 與Gitlab緊密集成的Gitlab CI
- 專為開源打造的Travis CI,與Github緊密集成
- 使用 python 語言實現的Buildbot,相信 pythoner 看到會喜歡
jenkins安裝
設置jenkins目錄
在catalina.sh 中定義jenkins
$ export CATALINA_OPTS="-DJENKINS_HOME=/path/to/jenkins_home/ -Xmx512m"
$ catalina.sh start
在linux環境變量中定義jenkins
$ export JENKINS_HOME=/path/to/jenkins_home/
$ catalina.sh start
在 context中定義jenkins-home
<Context ...>
<Environment name="JENKINS_HOME" value="/path/to/jenkins_home/" type="java.lang.String"/>
</Context>
安裝及初始化
wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
java -jar jenkins.war
http://localhost:8080
安裝常用插件
開始安裝
插件安裝完成后,開始配置admin的用戶名密碼。
開始使用jenkins
更改jenkins的家目錄
jenkins的家目錄默認路徑在/root/.jenkins/
路徑。根據啟動方式的不同,修改方式略有不同。
- 你是直接命令行啟動
java -jar jenkins.war
。
cat >>/etc/profile<<EOF
export JENKINS_HOME=/data/db/jenkins/
EOF
source /etc/profile
- 使用tomcat容器啟動
vim /data/app/tomcat/bin/catalina.sh
export JENKINS_HOME=/data/db/jenkins/
# OS specific support. $var _must_ be set to either true or false.
- 你也可以修改jenkins.war包(不推薦)
vim jenkins /web.xml
<!-- if specified, this value is used as the Hudson home directory -->
<env-entry>
<env-entry-name>HUDSON_HOME</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>/data/db/jenkins/</env-entry-value> #填入路徑
</env-entry>
開始安裝插件
jenkins最常用的就是插件,所有我們從安裝插件開始。路徑:系統管理-->管理插件,開始安裝插件。
- Build Pipeline Plugin:build 流程配置插件。
- Gitlab Plugin :gitlab pull 插件。
- Gitlab Hook Plugin:gitlab 鈎子插件。
- Build Authorization Token Root Plugin :用戶權限驗證插件。
- SonarQube Scanner for Jenkins :代碼質量管理插件。
- Parameterized Remote Trigger Plugin :遠程觸發插件。
- AnsiColor(可選):這個插件可以讓Jenkins的控制台輸出的log帶有顏色(就和linux控制台那樣)
- Maven Integration plugin
- Extended Choice Parameter Plug-In: 圖像界面配置多選參數
方法二
上傳插件
Jenkins-插件管理-高級-上傳插件
方法三
直接上傳到文件目錄(根據上文中密碼文件的路徑,可以知道jenkins的目錄在/root/.jenkins/中)
/root/.jenkins/plugins
重啟Jenkins
創建一個構建過程
輸入項目名稱--選擇構建一個自由風格的軟件項目
配置源碼下載地址
添加gitlab的認證key,這里配置ssh的私鑰。
gitlab中添加ssh-key的公鑰
配置構建過程
這里有個需要注意的地方,比如我們想要在遠端的機器上執行相關的腳本怎么辦?
一個原理: jenkins 在執行過程中,使用的是jenkins的用戶在執行。
兩種方法:
- 所有服務器跟jenkins做無密鑰登錄。
- 所有服務器的root做無密鑰登錄。
推薦使用第二種,因為發布的腳本,可能涉及權限的問題,如果使用jenkins可能會出現權限不足的情況。
最佳方案
sudo ssh -p 52113 root@192.168.56.13 "/data/scripts/web-deploy.sh"
執行立即構建-查看控制台輸出
解釋說明:
- jenkins 會
git clone
到jenkins的/workspace上。
[root@linux-node1 web-build16:29:46]#pwd
/root/.jenkins/workspace/web-build
[root@linux-node1 web-build16:29:56]#ls -a
. .. .git index.html README.md
[root@linux-node1 web-build16:29:58]#
[root@linux-node1 web-build16:31:49]#cat /tmp/1.txt
2017-03-01
Sonar 代碼質量管理
安裝sonar
cd /usr/local/src/
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.6.zip
mv sonarqube-5.6.6 /data/app/
ln -s /data/app/sonarqube-5.6.6/ /data/app/sonarqube
安裝數據庫
# 下載mysql二進制包
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
# 創建mysql用戶
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 解壓mysql二進制包
cd /usr/local/src
tar zxf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local/
chown -R mysql:mysql /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64
# 初始化mysql
ln -s /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64/ /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
# 上傳壓縮包中的my.cnf到/usr/local/mysql目錄下
#初始化 mysql數據庫
/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/usr/local/mysql/my.cnf \
--user=mysql --basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data
# 啟動mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64/
/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf &
# 連接mysql
/usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock
登錄mysql創建相關的數據庫
# mysql -uroot -p12345678
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@qw';
FLUSH PRIVILEGES;
sonar好像不支持MySQL 5.5,請安裝mysql5.6 或者更高版本
2017.03.01 18:52:01 ERROR web[o.a.c.c.C.[.[.[/]] Exception sending context initialized event to listener instance of class org.sonar.ser
ver.platform.PlatformServletContextListener
org.sonar.api.utils.MessageException: Unsupported mysql version: 5.5. Minimal supported version is 5.6.
2017.03.01 18:52:01 ERROR web[o.a.c.c.StandardContext] One or more listeners failed to start. Full details will be found in the appropri
ate container log file
2017.03.01 18:52:01 ERROR web[o.a.c.c.StandardContext] Context [] startup failed due to previous errors
2017.03.01 18:52:01 WARN web[o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [Abandone
d connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2017.03.01 18:52:01 WARN web[o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [Timer-0]
but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.util.TimerThread.mainLoop(Timer.java:552)
java.util.TimerThread.run(Timer.java:505)
編輯sonar的配置文件
編輯sonar連接數據庫的方式
vim /data/app/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@qw
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerf
ormance
配置sonar的監聽ip和端口
vim /data/app/sonarqube/conf/sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
啟動sonar服務
/data/app/sonarqube/bin/linux-x86-64/sonar.sh start
配置sonar
瀏覽器登錄sonar,用戶名admin,密碼:admin
第一步安裝中文插件
第二步安裝相關的語言插件(使用什么語言,安裝什么選擇器)
我們安裝一個python的插件
接着把php,java的插件也安裝上,然后重啟。
SonarQube Scanner 安裝
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
unzip sonar-scanner-2.8.zip
mv sonar-scanner-2.8 /data/app/
ln -s /data/app/sonar-scanner-2.8/ /data/app/sonar-scanner
編輯sonar scanner的配置文件
cat >>/data/app/sonar-scanner/conf/sonar-scanner.properties <<EOF
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@qw
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
EOF
soncar-scanner 在2.8版本的時候,可以不用配置,
soncar.jdbc.username
,sonar.jdbc.password
,sonar.jdbc.url
。只需要配置soncar.host.url
。
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
下載官方測試包 Sonar-examples
cd /data/db/
git clone https://github.com/SonarSource/sonar-examples.git
需要scanner掃描的代碼必須包含 sorna-project.properties
cd /data/db/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests
vim sorna-project.properties
sonar.projectKey=org.sonarqube:php-ut-sq-scanner # sonar中的key,必須唯一。
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner ##在sonar中展示的名稱
sonar.projectVersion=1.0 ##項目版本
sonar.sources=src ##源碼路徑
sonar.tests=tests
sonar.language=php ##源碼語言
sonar.sourceEncoding=UTF-8 ##源碼編譯方式
# Reusing PHPUnit reports
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml
sonar.projectKey=org.sonarqube:example-it-jacoco-sonar-scanner
sonar.projectName=Java :: IT Coverage with JaCoCo :: SonarQube Scanner
sonar.projectVersion=1.0
sonar.sources=src
sonar.binaries=classes
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.jacoco.itReportPath=reports/jacoco.exec
開始掃描
我們什么都不指定就會在當面目錄下掃描sonar-project.properties文件,根據配置文件進行掃描工作。掃描之后我們在web界面上就可以看到
pwd
# /data/db/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests
/data/app/sonar-scanner/bin/sonar-scanner
登錄sonar-在儀表盤中看到了我們剛剛運行的檢查。
點開可以看到詳細的信息
sonar和jenkins結合
安裝Jenkins - sonar 插件
系統管理-系統配置中 添加sonar的信息
在系統管理--> Global Tool Configuration 中配置sonar Scanner的路徑
開始構建相應的步驟
選擇立即構建,構建完成后,就可以在控制台輸出中看見內容了。
鈎子腳本配置
jenkins和gitlab聯合
配置身份驗證令牌
openssl rand -hex 10
9c8fe5c5bbb56b909259
配置gitlab的鈎子
官方的例子
Trigger the RevolutionTest job with the token TacoTuesday
buildByToken/build?job=RevolutionTest&token=TacoTuesday
Trigger the RevolutionTest job with the token TacoTuesday and parameter Type supplied with the value Mexican
buildByToken/buildWithParameters?job=RevolutionTest&token=TacoTuesday&Type=Mexican
根據官方的例子拼接URL
第一步:jenkins的URL:http://192.168.56.11:8080/jenkins/
第二步:拼接后端的URI:buildByToken/build?job=web-buildo&token=9c8fe5c5bbb56b909259
http://192.168.56.11:8080/jenkins/buildByToken/build?job=web-build&token=9c8fe5c5bbb56b909259
更新gitlab的內容,查看jenkins是否能夠自動更新。
配置gitlab流水線操作
安裝pipline的插件
jenkins pipline 設置
真實工作場景可能會有很多的job要執行。
編譯-->單元測試-->從集群中下線服務器--環境部署--重啟服務器--預熱--上線測試--上線集群。
創建一個pipline。
根據剛才設置的第一個pipline,配置后續的構建過程。
選擇【構建后操作】,接着選擇【Trigger parameterized build on other projects】
查看pipline執行的結果。
在這里可以查看各個job的執行情況,綠色是表示執行通過的,黃色是正在執行的,藍色是未執行的,還有紅色是執行失敗的。
交互式執行構建過程
jenkins配置slave
最近了解到Jenkins的節點功能,對於分布式構建非常方便!
Jenkins啟動在Windows上,如果想要直接操作Linux上的東西,那么比較波折,Jenkins節點大大的方便了不同系統之間的調用構建;
創建節點方式如下:
1.系統管理-管理節點-新建節點
2.輸入創建的節點名稱,並勾選“Dumb Slave”
3.配置 1)Name需要填寫
2)遠程工作目錄:slave.jar和job等目錄
3)用法:只允許運行綁定到這台機器的Job
4)啟動方法:Launch slave agents on Unix machines via SSH
5)高級:填寫Host Credentials(用戶名密碼,需要通過Add添加)
4.其他默認即可,配置完畢保存后,進入此節點,通過點擊Launch slave agent運行
此時Windows為master,Linux為slave,節點運行后,會在遠程工作目錄設定的路徑下生成slave.jar,用於jenkins調用;
需要注意的是:job建立需要勾選Restrict where this project can be run選項,並在Label Expression處填寫節點的名稱。
報錯匯總
http://10.10.0.176:8080/threadDump
參考
參考文檔
Jenkins進階系列之——16一個完整的JENKINS下的ANT BUILD.XML文件
build authorization token root plugin
jenkins自帶的環境變量
BRANCH_NAME
For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).
CHANGE_ID
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.
CHANGE_URL
For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.
CHANGE_TITLE
For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset.
CHANGE_AUTHOR
For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset.
CHANGE_AUTHOR_DISPLAY_NAME
For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset.
CHANGE_AUTHOR_EMAIL
For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset.
CHANGE_TARGET
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.
BUILD_NUMBER
The current build number, such as "153"
BUILD_ID
The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds
BUILD_DISPLAY_NAME
The display name of the current build, which is something like "#153" by default.
JOB_NAME
Name of the project of this build, such as "foo" or "foo/bar".
JOB_BASE_NAME
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".
BUILD_TAG
String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". All forward slashes (/) in the JOB_NAME are replaced with dashes (-). Convenient to put into a resource file, a jar file, etc for easier identification.
EXECUTOR_NUMBER
The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
NODE_NAME
Name of the agent if the build is on an agent, or "master" if run on master
NODE_LABELS
Whitespace-separated list of labels that the node is assigned.
WORKSPACE
The absolute path of the directory assigned to the build as a workspace.
JENKINS_HOME
The absolute path of the directory assigned on the master node for Jenkins to store data.
JENKINS_URL
Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)
BUILD_URL
Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)
JOB_URL
Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)
SVN_REVISION
Subversion revision number that's currently checked out to the workspace, such as "12345"
SVN_URL
Subversion URL that's currently checked out to the workspace.