使用Docker+Jenkins自動構建部署


環境

Windows 10 

Docker Version 18.06.1-ce-win73 (19507)

運行jenkins

 運行jenkins 容器

docker run -d --name lnmp-jenkins --user root -p 8080:8080 -p 50000:50000 jenkins/jenkins:2.165

1、請使用 root 賬戶運行,否則使用默認jenkins賬戶運行的時間將會有問題。

2、由於是Windows 環境,所以這里不建議掛在數據卷。否則將會有一些插件安裝不成功的問題出現。如果非要掛在請求使用 Kitematic.exe  

3、方便區分,請給容器給個名字

設置賬戶

通過http://you_host:8080登陸查看

在Getting Started界面會需要初始的密碼Unlock Jenkins。密碼會在輸出終端,也可根據頁面提示到容器的jenkins_home中查找

查看密碼請進入容器內查看

docker exec -it lnmp-jenkins bash

密碼文件路徑
root@52c33b1a61ba:/var/jenkins_home/secrets# 

有了密碼,輸入后安裝建議的插件。完畢后,根據提示設置登陸賬戶。

安裝Publish Over SSH插件

首頁 -> 點擊系統管理 -> 管理插件 ->可選插件 -> 過濾:ssh -> 選擇Publish Over SSH插件,點擊直接安裝。 

設置服務器SSH信息

首先在容器中生成rsa密鑰:

# 從宿主機客戶進入容器,目前容器名lnmp-jenkins,也可通過docker ps 查看
$ docker exec -it lnmp-jenkins /bin/bash
# 進入容器后建立.ssh目錄,創建密鑰文件私鑰id_rsa,公鑰id_rsa.pub
~ mkdir ~/.ssh && cd ~/.ssh
~ ssh-keygen -t rsa
# 一直回車即可

 

添加公鑰到宿主機
將id_rsa.pub中字符串添加到authorized_keys文件末尾,重啟ssh服務sudo service ssh restart
注意宿主機是否開啟ssh服務。
可以在容器終端中使用下面的命令添加到宿主機中。也可手動復制id_rsa.pub到宿主機的.ssh/authorized_keys文件中。

ssh-copy-id -i ~/.ssh/id_rsa.pub <username>@<host>

這里會要求我們輸入遠程服務器的密碼。

修改服務器的ssh配置文件

ssh-server配置文件位於:/etc/ssh/sshd_config中,需要設置ssh-server允許使用私鑰/公鑰對的方式登錄,打開配置文件:

vim /etc/ssh/sshd_config

增加設置:

RSAAuthentication yes
PubkeyAuthentication yes

然后重啟ssh-server:

sudo /etc/init.d/ssh restart

設置完成之后就可以使用命令ssh <username>@<host>直接登錄服務器了,不需要再輸入密碼了。

 

項目配置

首先,新建一個任務。填寫項目名稱。 選擇源碼管理為:Git,填寫項目庫的URL。私有項目需要添加Git賬號。

構建環境:選擇Send files or execute commands over SSH after the build runs,選擇服務器,以及添加Exec command。保存。

添加私鑰
jenkins首頁,系統管理 -> 系統設置 -> 下拉,找到Publish over SSH,填寫Key 和 SSH Server -> 保存

 

高級選項能夠配置ssh服務器端口和超時。Test可測試,顯示success配置成功。

 

配置webhook

配置webhook,實現自動部署
獲取API tonken:首頁 -> 用戶 -> 選擇當前的用戶 -> 設置 -> 在API Tonken 項中點擊Show API Token...

http://jenkins.tinywan.top/buildWithParameters?token=11216ee77000c52461ad16e5c4502427bb&cause=Cause+Text

http://jenkins.tinywan.top/job/Python%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/build?token=11216ee77000c52461ad16e5c4502427bb&cause=Cause+Text

 添加令牌:返回首頁 -> 項目 -> 配置 -> 構建觸發器 -> 選擇 "觸發遠程構建" ->粘貼"API Token"內容到"身份驗證令牌"

 注意:這里的身份驗證令牌是隨意生成的。不是非必須后台生成的token api

  

 測試結果

修改本地代碼然后提交、Jenkins自動構建部署

查看構建日志記錄

控制台輸出
Started by remote host 192.30.252.37 with note: Cause Text
Building in workspace /var/jenkins_home/workspace/Python自動化測試
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Tinywan/t_log.git # timeout=10
Fetching upstream changes from https://github.com/Tinywan/t_log.git
 > git --version # timeout=10
 > git fetch --tags --progress https://github.com/Tinywan/t_log.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 34f8b6beaf5ebc8f400e2da3432fd798c64acc83 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 34f8b6beaf5ebc8f400e2da3432fd798c64acc83
Commit message: "使用Docker+Jenkins自動構建部署【Python自動化測試】"
 > git rev-list --no-walk fd1c8499da56077ce84accfd3d0b1843c859398a # timeout=10
SSH: Connecting from host [52c33b1a61ba]
SSH: Connecting with configuration [www.tinywan.com] ...
SSH: EXEC: STDOUT/STDERR from command [pwd
cd /home/www/data/t_log
echo "jenkins before" >> jenkins.log
git pull 
echo "jenkins after" >> jenkins.log] ...
/home/www
From https://github.com/Tinywan/t_log
   fd1c849..34f8b6b  master     -> origin/master
Updating fd1c849..34f8b6b
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
SSH: EXEC: completed after 4,005 ms
SSH: Disconnecting configuration [www.tinywan.com] ...
SSH: Transferred 0 file(s)
Finished: SUCCESS

 

參考

1、使用ssh免密碼登錄Linux服務器

2、Docker+Jenkins自動構建部署

3、SpringBoot+Docker+Git+Jenkins實現簡易的持續集成和持續部署

4、JENKINS針對不同項目組對用戶進行權限分配


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM