Try   HackMD

Jenkins 串接 Gitlab

tags: 版本控制, Git, version control, DevOps

前置作業

  1. 建立Git專案
  2. 設置SSH(公私鑰)
  3. 上傳source code

建立作業

  1. 建立新作業 (在 192.168.3.19:50001)
    Jenkins主畫面 -> 新增作業 -> Free-Style軟體專案

專案配置設定

  1. 原始碼管理 設定要連接的Git Repository

    • Repository URL
      要給Jenkins連接的專案Git位址,跟Clone位置一樣。
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • Credentials
      由於Jenkins需要去pull新的source code, 因此需要給Jenkins對應的權限去操作
      下圖是點add的畫面, 如果SSH有設定正確的話可以直接選SSH Username and private key, 並在下方輸入Private key與Passphrase
      如果有錯誤畫面會提示錯誤訊息 ex. access denied
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • Branches to build
      設定要將Git Repository哪個Branch Build
  2. 建置觸發程序 設定什麼事件會觸發Jenkins

    • 這裡有很多事件可以選, 目前需要的是push後觸發先勾選

      • Build when a change is pushed to GitLab.
    • 後面有一串網址需要記起來, 等等會用到

      1. 剛剛複製的那段網址, 需要到Gitlab專案中
      2. Setting->Integrations->Webhooks
      3. URL填入剛剛的網址, Secret Token是下面Generate產生的Key
      4. 完成後點add webhook, 點Test -> Push event 回傳200表示成功
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
    • Allow Branch
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • Secret token
      需要配置到Gitlab上, 點Generate產生
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  3. 建置

    • 因為是在Jenkins本地建置的關係, 直接選執行Shell就可以
    • 本篇只討論Jenkins本地Build的情況, 如果需要在另外一台機器上Build則需要額外設定另一台機器的登入資訊。
    • Jenkins本地程式碼位置會在 /var/lib/jenkins/workspace/{project name}
    ​​​​docker stop $(docker ps -a -q  --filter ancestor=project1) || true
    ​​​​docker rm $(docker ps -a -q  --filter ancestor=project1) || true
    ​​​​docker rmi project1 || true
    ​​​​cd /var/lib/jenkins/workspace/demo_project
    ​​​​./gradlew build
    ​​​​docker build --build-arg JAR_FILE=build/libs/*.jar -t project1 .
    ​​​​BUILD_ID=dontKillMe docker run -p 8050:8050 project1 &
    
  4. 測試

    • push前網頁
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 修改程式碼後push
      ​​​​​​​​@RequestMapping("/")
      ​​​​​​​​@ResponseBody
      ​​​​​​​​public String home() {
      ​​​​​​​​    return "TEST FOR JENKINS DEMO!!";//返回結果為字串
      ​​​​​​​​}
      
    • 修改後網頁顯示
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

參考連結