1. 程式人生 > 實用技巧 >gitlab + gitlab-runner 整合 CICD

gitlab + gitlab-runner 整合 CICD

gitlab + gitlab-runner 整合 CICD

1 gitlab-runner 安裝

  • 1.1 下載執行檔案
  1. # Linux x86-64
  2. sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
  3. # Linux x86
  4. sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
  5. # Linux arm
  6. sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
  • 1.2 設定執行許可權
sudo chmod +x /usr/local/bin/gitlab-runner
  • 1.3 建立 GitLab CI 使用者
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  • 1.4 執行服務
  1. gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
  2. gitlab-runner start

2 gitlab-runner 註冊

2.1 獲取 Gitlab註冊令牌

開啟 gitlab 專案 -> 設定 -> CI / CD -> Runners 設定

2.2 LINUX 註冊

  • 執行註冊
sudo gitlab-runner register
  • 輸入你的 GitLab URL
  1. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
  2. https://xxx.xxx
  • 輸入註冊令牌來註冊 Runner
  1. Please enter the gitlab-ci token for this runner
  2. xxx
  • 輸入 Runner 說明
  1. Please enter the gitlab-ci description for this runner
  2. [hostame] my-runner
  • 輸入 Runner 的 tags
  1. Please enter the gitlab-ci tags for this runner (comma separated):
  2. my-tag,another-tag
  • 輸入 Runner 執行方式
  1. Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
  2. shell
  • 如果是在 Docker 中執行, you'll be asked for the default image to be used for projects that do not define one in .gitlab-ci.yml:
  1. Please enter the Docker image (eg. ruby:2.1):
  2. alpine:latest

3 連結成功

3.1 runner 列表

3.2 runner 修改

4 編寫.gitlab-ci.yml整合

  1. image: java:8
  2. stages:
  3. - build
  4. - build-pro
  5. #測試環境
  6. build:
  7. stage: build
  8. script:
  9. - pwd
  10. - cd project/my_app
  11. - /usr/local/maven/bin/mvn clean package -P test
  12. - sh /usr/local/project_deploy_script/app/app_test.sh
  13. tags:
  14. - deploy-app
  15. only:
  16. - develop
  17. #正式環境
  18. stage: build-pro
  19. script:
  20. - pwd
  21. - cd project/my_app
  22. - /usr/local/maven/bin/mvn clean package -P pro
  23. - sh /usr/local/project_deploy_script/app/app.sh
  24. tags:
  25. - deploy-app
  26. only:
  27. - master

5 執行整合

  • 下次提交程式碼就會走整合任務了

  • 任務階段

  • 詳情

參考