最近項目上開始實行自動化測試,避免不了與jenkins等持續集成工具打交道,今天就給大家分享一下有關jenkins的簡單安裝和使用
1,准備環境
(1)ubuntu系統
(2)docker
(3)jenkins
2,安裝docker
(1)安裝 sudo apt-get install -y docker.io
(2)啟動docker systemctl start docker 可以設置永久開機自啟動 systemctl enable docker
(3)docker version 或 docker -v 可以查看版本,說明docker安裝成功了
(4)安裝完成后,當你執行docker run hello-world,你可以發現docker先從本地找hello-world鏡像,沒有的話,就從官方倉庫找
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
(5)在倉庫中搜索鏡像,比如搜索ubuntu鏡像,docker search ubuntu 找到之后(並不一定有,所以可能需要換倉庫),進行拉取鏡像docker pull ubuntu
有的鏡像很大,倉庫又在國外,速度很慢。我們可以自己搭建私服,也可以用一些其他地址。
目前世界上最大最知名的公共倉庫是Docker官方的Docker Hub,國內比較知名的有:Docker Pool、阿里雲倉庫等
(6)阿里雲倉庫鏡像服務配置,首先在阿里雲https://www.aliyun.com/上注冊賬戶,登錄之后,進入控制台-選擇docker容器鏡像服務-進入鏡像加速器-按照上面說的方法在ubuntu上配置即可;
配置完成執行docker info | grep http就可以看到剛才配置的倉庫地址了
關於docker的具體使用方法,不在此贅述,可以參考
菜鳥教程https://www.runoob.com/docker/docker-tutorial.html
官方英文文檔https://docs.docker.com/
中文文檔http://www.dockerinfo.net/document
3,安裝jenkins
相關參考
https://jenkins.io/zh/doc/book/installing/
https://hub.docker.com/r/jenkins/jenkins
(1)准備本地文件,用來保存重要內容,以防容器被意外破壞和刪除
mkdir /usr/local/workspace/jenkins;chmod 777 jenkins
(2)執行docker pull和docker run命令,啟動容器,這個命令如果本地沒有鏡像,會自動的從倉庫拉取
docker pull jenkins/jenkins:lts
lts: Pulling from jenkins/jenkins
9cc2ad81d40d: Pull complete
e6cb98e32a52: Pull complete
ae1b8d879bad: Pull complete
42cfa3699b05: Pull complete
8d27062ef0ea: Pull complete
9b91647396e3: Pull complete
7498c1055ea3: Pull complete
7649b1010bf2: Pull complete
1c3f5e14c681: Pull complete
a204aa63f757: Pull complete
9b57f731dd16: Pull complete
d21f82c5429a: Pull complete
440e5fb06ea6: Pull complete
5d43ca048df2: Pull complete
1421ef7f2b0c: Pull complete
0c2b6150f92f: Pull complete
9e0d2e7a610a: Pull complete
36bd756f5339: Pull complete
e27e28c0b503: Pull complete
Digest: sha256:7cfe34701992434cc08bfd40e80e04ab406522214cf9bbefa57a5432a123b340
Status: Downloaded newer image for jenkins/jenkins:lts
啟動一個名為jenkins的容器,並且將容器的8080和50000端口直接映射到本機的8080和50000端口,文件對應本地的/usr/local/workspace/jenkins
docker run -p 8080:8080 -p 50000:50000 -v /usr/local/workspace/jenkins:/var/jenkins_home --name jenkins -idt jenkins/jenkins:lts
Unable to find image 'jenkins:latest' locally
latest: Pulling from library/jenkins
55cbf04beb70: Pull complete
1607093a898c: Pull complete
9a8ea045c926: Pull complete
d4eee24d4dac: Pull complete
c58988e753d7: Pull complete
794a04897db9: Pull complete
70fcfa476f73: Pull complete
0539c80a02be: Pull complete
54fefc6dcf80: Pull complete
911bc90e47a8: Pull complete
38430d93efed: Pull complete
7e46ccda148a: Pull complete
c0cbcb5ac747: Pull complete
35ade7a86a8e: Pull complete
aa433a6a56b1: Pull complete
841c1dd38d62: Pull complete
b865dcb08714: Pull complete
5a3779030005: Pull complete
12b47c68955c: Pull complete
1322ea3e7bfd: Pull complete
Digest: sha256:eeb4850eb65f2d92500e421b430ed1ec58a7ac909e91f518926e02473904f668
Status: Downloaded newer image for jenkins:latest
7c0c83b83707e40c879156449ea783a24a984fab9a3105e62b298d3a00ffdfaa
(3)驗證是否安裝成功
執行命令docker logs -f jenkins查看容器的控制台日志,看到如下信息表示jenkins服務啟動成功
Aug 29, 2019 5:49:10 AM hudson.model.UpdateSite updateData
INFO: Obtained the latest update center data file for UpdateSource default
Aug 29, 2019 5:49:10 AM hudson.WebAppMain$3 run
INFO: Jenkins is fully up and running
Aug 29, 2019 5:49:39 AM hudson.model.DownloadService$Downloadable load
INFO: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
Aug 29, 2019 5:49:41 AM hudson.model.DownloadService$Downloadable load
INFO: Obtained the updated data file for hudson.tools.JDKInstaller
Aug 29, 2019 5:49:41 AM hudson.model.AsyncPeriodicWork$1 run
INFO: Finished Download metadata. 45,776 ms
或者docker ps -a | grep jenkins 看到up,表示運行狀態
docker ps -a | grep jenkins
95e49fb019ce jenkins "/bin/tini -- /usr/l…" 6 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins
如果想要開機自動重啟容器,可以設置 docker update --restart=always jenkins
4,使用jenkins
(1)訪問jenkins
http://192.168.1.119:8080,按照界面提示配置密碼登錄
頁面提示密碼文件的位置:/var/jenkins_home/secrets/initialAdminPassword,由於我們已經將/var/jenkins_home目錄映射到本機的/usr/local/workspace/jenkins目錄,所以密碼文件在這個目錄
(2)安裝推薦插件,根據界面提示進行即可,耐心等待吧
問題:我遇到了,在安裝完插件,如果重新刷新進入jenkins的時候,輸入完第一步的密碼之后,頁面一直處於空白加載中,無法繼續訪問,搜查了網上資料解決了
在$JENKINS_HOME/hudson.model.UpdateCenter.xml文件中,也就是/usr/local/workspace/jenkins/hudson.model.UpdateCenter.xml默認內容如下
<?xml version='1.1' encoding='UTF-8'?> <sites> <site> <id>default</id> <url>https://updates.jenkins-ci.org/update-center.json</url> </site> </sites>
把url改成http://mirror.xmission.com/jenkins/updates/update-center.json
或者直接訪問http://192.168.1.119:8080/pluginManager/advanced
然后在下面更改一下
(3)創建一個管理員賬戶,根據界面提示進行即可
問題:登錄之后,可能顯示空白頁
訪問http://192.168.1.119:8080/restart 登錄管理員賬戶就可以了