Gitlab Runner?
GitLab Runner是一個用于運行GitLab CI/CD流水線作業的軟件包,由GitLab官方開發,完全開源。你可以在很多主流的系統環境或平臺上安裝它,如Linux、macOS、Windows和Kubernetes。如果你熟悉Jenkins 的話,你可以把它理解為Jenkins slave節點。
Gitlab Runner?架構
安裝GitLab Runner
沒有GitLab Runner,GitLab CI/CD的流水線就無法運行,現在我們就在一臺計算機上安裝GitLab Runner。GitLab Runner的安裝方式有很多,二進制安裝,docker安裝。今天我介紹一下二進制安裝。
1. 下載安裝包
sudo curl -L --output /usr/local/bin/gitlab-runner "https://s3.dualstack.us-east-1.amazonaws.com/gitlab-runner-downloads/latest/binaries/gitlab-runner-linux-amd64"
2. 添加執行權限
#sudo?chmod?+x?/usr/local/bin/gitlab-runner
3. 創建?GitLab CI 用戶
#useradd --comment 'Gitlab runner' --create-home gitlab-runner --shell /bin/bash
4. ?作為服務安裝
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
5. 注冊到Gitlab 服務器
當然你如果有自己的gitlab 服務器的話,也可以注冊到自己的gitlab 服務器。
gitlab-runner register
Runtime platform arch=amd64 os=linux pid=47871 revision=44feccdf version
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.com
Enter the registration token:
xxxxxxxxxxxxxxxx??//請替換成自己的token
Enter a description for the runner:
[gitlab01]: linux
Enter tags for the runner (comma-separated):
linux
Enter optional maintenance note for the runner:
This is only for testing.
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecatert for authentication tokens. For more information, see https://docs.gitlab.com/ee/ci/runners/new_creation_
Registering runner... succeeded runner=GR134894157Z-kVdL
Enter an executor: instance, custom, ssh, parallels, docker-windows, docker+machine, shell, virtualbox, doc
[shddd]: docker
Enter the default Docker image (for example, ruby:2.7):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
通過下面的步驟可以查看token
注冊成功后,可以登錄gitlab.com來查看該runner
運行pipeline
在你的project 中創建一個.gitlab-ci.yml文件
復制下面的代碼并提交更改,然后pipeline開始工作。
build-job:
stage: build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
test-job1:
stage: test
script:
- echo "This job tests something"
test-job2:
stage: test
script:
- echo "This job tests something, but takes more time than test-job1."
- echo "After the echo commands complete, it runs the sleep command for 20 seconds"
- echo "which simulates a test that runs 20 seconds longer than test-job1"
- sleep 20
deploy-prod:
stage: deploy
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
environment: production
選擇Build-Pipelines,你可以看到三個階段的pipepline:
點擊pipeline id,可以看到圖形化的pipeline。
選擇其中的一個job,點擊該job名稱可以看到job的詳細信息。
到此為止,你已經在gitlab 中成功運行第一個CICD pipeline。
總結
今天我們了解的 Gitlab CI/CD 中負責執行腳本的 Gitlab Runner 的設計結構,介紹了它的安裝(二進制),注冊流程。最后我們演示了如何運行一個 CICD pipeline。