gitlab cicd(三) Gitlab Runner 介紹安裝

Gitlab Runner 介紹

GitLab Runner是一個開源項目,用於運行您的作業並將結果發送回GitLab。它與GitLab CI一起使用,GitLab CI是GitLab隨附的開源持續集成服務,用於協調作業。

GitLab Runner是用Go編寫的,可以作爲單個二進制文件運行,不需要語言特定的要求。

它旨在運行在GNU / Linux,macOS和Windows操作系統上。只要您可以在其上編譯Go二進制文件,其他操作系統就可能正常工作。

如果要使用Docker,請確保v1.5.0至少安裝了版本

官網文檔:

https://docs.gitlab.com/runner/

安裝(二進制手動安裝方式)

服務器:192.168.31.31(centos7)

文檔:https://docs.gitlab.com/runner/install/linux-manually.html

步驟:

#下載二進制文件,並設置可執行權限
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,可選,如果執行器選擇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服務器執行

[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,在之後執行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執行完成了具體步驟可以點擊jobs查看詳細,如果手速快,還在在runner機器上docker ps看到執行過程


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章