# GitLab CI Runner 設定 ###### tags: `Infrastructure` ## Step 1: 啟動gitlab-runner 利用docker compose啟動gitlab-runner,該yml檔案內容如下: ```yaml= version: '3' services: gitlab-runner: image: gitlab/gitlab-runner:latest container_name: gitlab-runner-test restart: always volumes: - <想要放config檔案的位置> - /var/run/docker.sock:/var/run/docker.sock ``` 執行以下指令: ```bash= docker-compose up -d ``` ## Step 2: 設定gitlab-runner config 進入docker container 進行設定: ```bash= $ docker exec -ti gitlab-runner-test gitlab-runner register Runtime platform arch=amd64 os=linux pid=28 revision=7f7a4bb0 version=13.11.0 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): # URL位置,來源請參考下方 Enter the registration token: # token位置,來源請參考下方 Enter a description for the runner: [00ed7dd9fa31]: gitlab-runner-test # 參考敘述,會顯示在gitlab runner處 Enter tags for the runner (comma-separated): quai-test # tag資訊, 與之後寫的.gitlab-ci.yml中的tag相關 Registering runner... succeeded runner=GR134894 Enter an executor: shell, ssh, docker+machine, kubernetes, docker, parallels, virtualbox, docker-ssh+machine, custom, docker-ssh: docker Enter the default Docker image (for example, ruby:2.6): alpine:latest Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ``` URL 以及 token的位置於 Settings → CI/CD → Runners中 ![upload_d3a108d40857ea656f1c955cdc29455d](https://hackmd.io/_uploads/BJdeBBHY6.png) 此處的Register the runner with this URL就是需要填入Enter the GitLab instance URL的位置 此處的registration token就是需要填入Enter the registration token的位置 ![upload_d1ff9fa92dbdc704caed02447d055ccc](https://hackmd.io/_uploads/HkOn4rSFp.png) 設定完成後於下方會看到該runner,表示成功 ![upload_1ae029a122b3b6233d1643bdbde31a53](https://hackmd.io/_uploads/SyTiVBHt6.png) ## Step 3: 設定進階gitlab-runner config 由於剛剛只有初步的設定,但因為我們build的流程,幾乎都會用到本地的資料,因此須設定docker volume,以及在拉docker image有一些條件設定,可以直接進去剛剛產生出的config.toml中設定,該檔案的範例寫法如下: ```yaml= concurrent = 6 check_interval = 0 [session_server] session_timeout = 1800 [[runners]] name = "gitlab-runner-test" url = XXX token = XXX executor = "docker" [runners.custom_build_dir] [runners.cache] [runners.cache.s3] [runners.cache.gcs] [runners.cache.azure] [runners.docker] tls_verify = false image = "alpine:latest" privileged = true disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock", <使用volume讓其他docker在build CI流程時,提供對應local的檔案位置>] pull_policy = "if-not-present" shm_size = 0 ``` ## Step 4: 撰寫.gitlab-ci.yml檔案 其撰寫的模式與.drone.yml相似。但有些部份須注意: * regex的寫法須注意,尤其在某些branch才執行CI流程的部份 * tags會與想要用哪一個runner執行有關係 範例寫法如下: ```yaml= stages: - build - pkg build: image: <your docker image> stage: build tags: - quai-test script: | <需要執行的指令,如bash script> $CI_COMMIT_BRANCH only: - master - /^task-*/ pkg: image: <your docker image> stage: pkg tags: - quai-test script: | <需要執行的指令,如bash script> $CI_COMMIT_BRANCH only: - master - /^task-*/ ``` 放置該檔案於repo的最外層,Push code之後就會開始執行CI流程。 Note: * `$CI_COMMIT_BRANCH` 為push要執行CI的branch名稱,方便區別不同branch所執行的build指令。 * 這邊指定要走CI流程的branch以regexp表達時,需要注意其寫法。舉例來說,如果想要在所有開頭是`task-`的branch執行CI,可以用`/^task-*/`表示。