Gitlab Runner 介紹
GitLab Runner是一個開源項目晚碾,用于運行您的作業(yè)并將結果發(fā)送回GitLab老客。它與GitLab CI一起使用屋剑,GitLab CI是GitLab隨附的開源持續(xù)集成服務孟抗,用于協(xié)調作業(yè)迁杨。
GitLab Runner是用Go編寫的,可以作為單個二進制文件運行凄硼,不需要語言特定的要求铅协。
它旨在運行在GNU / Linux,macOS和Windows操作系統(tǒng)上摊沉。只要您可以在其上編譯Go二進制文件狐史,其他操作系統(tǒng)就可能正常工作。
如果要使用Docker说墨,請確保v1.5.0至少安裝了版本
官網文檔:
https://docs.gitlab.com/runner/
安裝(二進制手動安裝方式)
服務器:192.168.31.31(centos7)
文檔:https://docs.gitlab.com/runner/install/linux-manually.html
步驟:
#下載二進制文件骏全,并設置可執(zhí)行權限
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
#安裝docker,可選尼斧,如果執(zhí)行器選擇docker姜贡,則需要安裝,如果可以棺棵,最好裝個docker加速器
curl -sSL https://get.docker.com/ | sh
#添加一個普通用戶權限楼咳,用來運行gitlab runner,然后運行gitlab runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
注冊gitlab runner
打開gitlab,找到runner 熄捍,以及runner的認證網址和隨機碼
gitlab runner服務器執(zhí)行
[root@localhost ~]# gitlab-runner register #注冊runner到gitlab
Runtime platform arch=amd64 os=linux pid=12351 revision=d0b76032 version=12.0.2
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): #找到上圖的gitlab服務器地址,也就是第二條
http://192.168.31.130/
Please enter the gitlab-ci token for this runner:#輸入隨機碼
yx6oVQYxrLxczyazysF9
Please enter the gitlab-ci description for this runner: #runner 服務器的描述
[localhost.localdomain]: gitlab-runner-01
Please enter the gitlab-ci tags for this runner (comma separated): #runner打tag母怜,在之后執(zhí)行ci腳本可以通過tags選擇runner
gitlab-runner-01
Registering runner... succeeded runner=yx6oVQYx
Please enter the executor: docker, docker-ssh, ssh, docker-ssh+machine, parallels, shell, virtualbox, docker+machine, kubernetes: #設置runner運行方式余耽,這里我上一步安裝了docker,我選擇docker苹熏,也可以選擇其他
docker
Please enter the default Docker image (e.g. ruby:2.6):#設置默認的鏡像碟贾,也就是在ci里面沒有寫明image默認使用的鏡像,這里我寫的是centos:7
centos:7
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@localhost ~]# gitlab-runner restart #重啟下gitlab runner
Runtime platform arch=amd64 os=linux pid=12359 revision=d0b76032 version=12.0.2
[root@localhost ~]# gitlab-runner list #查看當前gitlab runner
Runtime platform arch=amd64 os=linux pid=12379 revision=d0b76032 version=12.0.2
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
gitlab-runner-01 Executor=docker Token=KAsxjVbByKauYnNMSKHY URL=http://192.168.31.130/
備注:gitlab runner配置文件在/etc/gitlab-runner/config.toml
注冊完成后轨域,在gitlab上就能看到剛注冊的runner
測試
在項目的根目錄下新建.gitlab-ci.yaml
.gitlab-ci.yaml內容是(具體含義下次分析)
stages:
- build
- cleanup_build
- test
- deploy
- cleanup
build_job:
stage: build
tags:
- gitlab-runner-01 #這里配置的是gitlab runner注冊時候寫的tag
script:
- echo build_job
cleanup_build_job:
stage: cleanup_build
tags:
- gitlab-runner-01
script:
- echo cleanup build when failed
when: on_failure
test_job:
stage: test
tags:
- gitlab-runner-01
script:
- echo test
deploy_job:
stage: deploy
tags:
- gitlab-runner-01
script:
- echo deploy
when: manual
cleanup_job:
stage: cleanup
tags:
- gitlab-runner-01
script:
- echo cleanup_job
when: always
可以看到pipline執(zhí)行完成了具體步驟可以點擊jobs查看詳細缕陕,如果手速快,還在在runner機器上docker ps看到執(zhí)行過程