2017年4月4日 星期二

快速架一個 CI Server - Jenkins 設定

這是第一次使用CI Server,當然也是第一次使用 Jenkins
它的選項非常多,實在是無法在短時間內完全理解

本篇是慢慢摸索之中,所留下的紀錄
未來有心得時,會再持續更新吧

最初的目標是想利用 Jenkins 幫忙編譯 Maven 專案
編譯成功之後,自動佈署到 Tomcat 上

1. Install Plugins


從 Jenkins 首頁 → Manage Jenkins → Manage Plugins,可以管理 Plugins

一共需要安裝以下的 Plugin

  • Maven Integration plugin - 可以建立 Maven 專案工作
  • Deploy to container plugin - 建置完成之後,可以佈署到 Tomcat Web Server





2. Create a new job


2-1. 在 Jenkins 首頁,點選 New Item 或是 create new jobs




2-2. 選擇 Maven project




2-3. 設定 Git 路徑、帳號密碼、Branch name



如果 Git 是用 SSH 連線時,由於 Jenkins 沒有 interaction 機制,在過程中沒辦法輸入密碼。可行的解決方法為:
  1. 利用 Smart HTTP,透過 HTTP CGI 來實現
  2. 產生 SSH public/private key,把 public key 放到 git server 上
用第二種方法時,需要 git server 的權限
參考 : http://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password
  1. Don't use a password. Generate a passphraseless SSH key and push it to your VM.
  2. If you already have an SSH key, you can skip this step… Just hit Enter for the key and both passphrases:
  3.  
  4. $ ssh-keygen -t rsa -b 2048
  5. Generating public/private rsa key pair.
  6. Enter file in which to save the key (/home/username/.ssh/id_rsa):
  7. Enter passphrase (empty for no passphrase):
  8. Enter same passphrase again:
  9. Your identification has been saved in /home/username/.ssh/id_rsa.
  10. Your public key has been saved in /home/username/.ssh/id_rsa.pub.
  11. Copy your keys to the target server:
  12.  
  13. $ ssh-copy-id id@server
  14. id@server's password:
  15. Now try logging into the machine, with ssh 'id@server', and check in:
  16.  
  17. .ssh/authorized_keys
  18. to make sure we haven't added extra keys that you weren't expecting.
  19.  
  20. Finally check logging in
  21.  
  22. $ ssh id@server
  23.  
  24. id@server:~$
  25. You may also want to look into using ssh-agent if you want to try keeping your keys protected with a passphrase.

進入 Jenkins,新增使用者並選擇 SSH Username with private key
貼上 private key 內容、在 Passphrase 欄位填入 password


2-4. Build Triggers


可以多選
Poll SCM 使用上與 crontab 相同,如下圖範例為每一分鐘會做一次


2-5. Build

Build 之前的環境設定

Build 之前可以先執行 script 動作


可以填入 Maven 參數



Build 之後的動作,執行的時機

Post-build action 選擇 Deploy war/ear to a container


2-6. 寄 E-Mail 通知


3. Build Now


按下儲存之後,回到 Work 頁面
選擇 Build Now 可以立即執行



沒有留言:

張貼留言