筆記:Gitlab-CI部署流程

阿賀呀發表於2019-03-28

Gitlab-CI部署流程

  • ssh連線伺服器

    • 觸發條件

      當使用者推送最新程式碼至master分支時,trigger gitlab-ci

    • 原理

      利用gitlab自動化在gitlab打包

      打包後的檔案利用ssh推送至伺服器

      • 讀取gitlab-ci.yml檔案

      • .gitlab-ci.yml配置檔案詳解

        image: node:8 # 拉取node version:8 的docker映象
        cache: # 快取資料夾 or 檔案
          paths:
            - node_modules/
        
        before_script: # 執行指令碼前的工作
         - apt-get update -qq && apt-get install -y -qq sshpass # 更新apt-get工具並安裝sshpass
        
        deploy_stage: # deploy_stage指令碼
          stage: deploy
          environment: Staging
          only:
            - master # 配置觸發條件
          script:
            - npm install # 步驟1: 安裝前端依賴包
            - npm run build # 步驟2: 打包前端資料夾
            - export SSHPASS=$USER_PASS # 步驟3: 匯出 USER_PASS環境變數供sshpass使用
            - sshpass -e scp -o stricthostkeychecking=no -r dist/ $USER_NAME@$SERVER_IP:$SERVER_PATH # 步驟4: ssh連線伺服器並推送dist/資料夾至SERVER_PATH下
        
        複製程式碼

        $USER_NAME: ssh連線伺服器的使用者名稱

        $USER_PASS: ssh連線伺服器的密碼

        $SERVER_IP: 伺服器IP地址

        $SERVER_PATH: 推送伺服器絕對路徑

        以上變數皆在Gitlab專案 setting => CI => Environment Variables裡配置

  • 推送完成後Gitlab=> CI => Pipeline檢視日誌

    日誌詳解 (帶藍標的為gitlab中配置好的script步驟)

    Running with gitlab-runner 11.9.0-rc2 (227934c0)
      on docker-auto-scale 0277ea0f
    Using Docker executor with image node:8 ...
    Pulling docker image node:8 ...
    Using docker image sha256:8c51cec97ebfc94d85daf702377acb73afcfc6cfd5a85e4fe2816732e25ec229 for node:8 ...
    Running on runner-0277ea0f-project-11375217-concurrent-0 via runner-0277ea0f-srm-1553699604-3058a32d...
    Initialized empty Git repository in /builds/xxx/doer/.git/
    Fetching changes...
    Created fresh repository.
    From https://gitlab.com/xxx/doer
     * [new branch]      develop    -> origin/develop
     * [new branch]      master     -> origin/master
    Checking out 017588b7 as master...
    
    Skipping Git submodules setup
    Checking cache for default-2...
    Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/11375217/default-2 
    Successfully extracted cache
    $ apt-get update -qq && apt-get install -y -qq sshpass
    debconf: delaying package configuration, since apt-utils is not installed
    Selecting previously unselected package sshpass.
    (Reading database ... 
    (Reading database ... 5%
    (Reading database ... 10%
    (Reading database ... 15%
    (Reading database ... 20%
    (Reading database ... 25%
    (Reading database ... 30%
    (Reading database ... 35%
    (Reading database ... 40%
    (Reading database ... 45%
    (Reading database ... 50%
    (Reading database ... 55%
    (Reading database ... 60%
    (Reading database ... 65%
    (Reading database ... 70%
    (Reading database ... 75%
    (Reading database ... 80%
    (Reading database ... 85%
    (Reading database ... 90%
    (Reading database ... 95%
    (Reading database ... 100%
    (Reading database ... 29978 files and directories currently installed.)
    Preparing to unpack .../sshpass_1.06-1_amd64.deb ...
    Unpacking sshpass (1.06-1) ...
    Setting up sshpass (1.06-1) ...
    $ npm install
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
    
    audited 27888 packages in 10.616s
    found 0 vulnerabilities
    
    $ npm run build
    
    > doer@0.1.0 build /builds/xxx/doer
    > vue-cli-service build
    
    
    -  Building for production...
    Starting type checking and linting service...
    Using 1 worker with 2048MB memory limit
     WARNING  Compiled with 2 warnings15:15:42
    
     warning  
    
    entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
    Entrypoints:
      app (247 KiB)
          js/chunk-vendors.419d8636.js
          css/app.20dafc28.css
          js/app.40b96980.js
    
    
     warning  
    
    webpack performance recommendations: 
    You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
    For more info visit https://webpack.js.org/guides/code-splitting/
    
      File                                 Size               Gzipped
    
      dist/js/chunk-vendors.419d8636.js    240.07 KiB         82.82 KiB
      dist/js/app.40b96980.js              6.46 KiB           2.51 KiB
      dist/css/app.20dafc28.css            0.35 KiB           0.24 KiB
    
      Images and other types of assets omitted.
    
     DONE  Build complete. The dist directory is ready to be deployed.
     INFO  Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
          
    $ export SSHPASS=$USER_PASS
    $ sshpass -e scp -o stricthostkeychecking=no -r dist/ $USER_NAME@$SERVER_IP:$SERVER_PATH
    Warning: Permanently added '139.196.98.25' (ECDSA) to the list of known hosts.
    Creating cache default-2...
    node_modules/: found 25362 matching files          
    Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/11375217/default-2 
    Created cache
    Job succeeded
    複製程式碼
  • ssh登入伺服器檢視檔案,成功~

筆記:Gitlab-CI部署流程

這樣就完成自動部署的流程啦

後續有待使用docker去操作~

相關文章