gitlab-runner 安裝使用

gitlab-runner 安裝使用

  • gitlab-runner 是一個開源的與 gitlab CI 配合使用的項目,用於運行任務,並將結果返回 gitlab
  • 本文經過docker in docker的方式,即在具備特權模式的 Docker 中使用 Docker,經過 gitlab-runner 使用 docker 的來構建項目,完成打包,測試,發佈等任務。這樣 gitlab-runner 只是須要 Dockerfile 便可,可方便往後使用其餘 CI/CD 工具。同時本地調試也十分方便
  • 文檔地址:https://docs.gitlab.com/runner

安裝

docker run -d --name gitlab-runner --restart always \
   -v /srv/gitlab-runner/config:/etc/gitlab-runner \
   -v /var/run/docker.sock:/var/run/docker.sock \
   gitlab/gitlab-runner:latest

註冊

  • 安裝好 gitlab-runner 後,須要進行註冊,gitlab 纔會出現 runner 列表。註冊前須要準備好地址和 token,管理員進入管理中心>概覽>Runner可查看 runner 列表、url 和註冊令牌
  • 若是想要爲某羣組或者項目設置 runner,可在對應羣組或者項目的設置>CI/CD>Runner獲取 url 和 token
  • 首先進入容器交互模式
docker exec -it gitlab-runner bash
  • 輸入命令gitlab-runner register進行註冊,按照提示和文檔說明一步步填寫便可
  • 也可一條命令完成註冊
sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --executor "docker" \
  --docker-image alpine:latest \
  --description "docker-runner" \
  --tag-list "docker,aws" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected"

配置

[[runners]]
  executor = "docker"
  [runners.docker]
    privileged = true
  • 完整的例子
[[runners]]
  name = "docker-runner"
  url = "http://192.168.99.100:8900/"
  token = "zLBu8yXEFPGKaaasZopn"
  executor = "docker"
  clone_url = "http://192.168.99.100:8900/"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "apline:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0

環境變量

  • 經過設置環境變量,讓 runner 執行的時候讀取,避免在源碼中記錄敏感信息
  • 在羣組或者項目的設置中依次點擊設置>CI/CD>變量
  • .gitlab-ci.yml文件中使用方式html

    Shell Usage
    bash/sh $variable
    windows batch %variable%
    PowerShell $env:variable
  • 參考https://docs.gitlab.com/ce/ci/variables/README.html#via-the-ui
  • Dockerfile 中使用變量參考:https://docs.docker.com/engine/reference/builder/#arggit

添加 Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch
ARG AppKey
ARG Source
WORKDIR /src
COPY ["A1/A1.csproj", "A1/"]
RUN dotnet restore "A1/A1.csproj"
COPY . .
WORKDIR /src/A1
RUN dotnet pack "A1" -c Release -o /app

WORKDIR /app
RUN dotnet nuget push *.nupkg -k ${AppKey} -s ${Source}

添加 .gitlab-ci.yml

# This file is a template, and might need editing before it works on your project.
build-master:
  # Official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    # - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker --version
    - echo $NugetAppKey
    - echo $NugetSource
  script:
    - docker build --build-arg AppKey=$NugetAppKey --build-arg Source=$NugetSource -t A1:latest .
  only:
    - master

問題

[[runners]]
  name = "ruby-2.1-docker"
  url = "https://CI/"
  token = "TOKEN"
  limit = 0
  executor = "docker"
  builds_dir = ""
  shell = ""
  environment = ["ENV=value", "LC_ALL=en_US.UTF-8"]
  clone_url = "http://gitlab.example.local"

總結

  • 安裝過程也沒什麼難點,有什麼不對的多試幾回便可
  • gitlab 的文檔仍是挺齊全的,若是可以把相關的文檔先大概瀏覽一遍,將會有事半功倍的效果,省去很多麻煩
相關文章
相關標籤/搜索