gitlab-runner安裝配置


 

安裝gitlab-runner

# For RHEL/CentOS/Fedora

curl -L https://packages.gitlab.com/install/repositories/runner/gitlabrunner/script.rpm.sh | sudo bash

# For RHEL/CentOS/Fedora

yum install gitlab-ci-multi-runner

gitlab-runner注冊

首先要先獲取gitlab-ci的Token:

執行gitlab-runner register注冊,輸出URL,Token,描述,tag,執行器等

 

編寫.gitlab-ci.yml觸發器

stages:
    - 構建打包
    - 生成鏡像

variables:
    VERSION: 'latest'
    SVCNAME: 'portal'    

cache:
    paths:
        - node_modules #設置cache,不用每次構建去下載依賴

build: 
    stage: 構建打包
    tags:
        - test #設置使用gitlab-runner服務器的tag,如果有多個gitlab-runner
    only:
        - release/test
    script:
        npm run release-build
    artifacts:
        paths:
            - tar #將tar目錄傳遞給下一個job,防止被刪除

docker:
    stage: 生成鏡像
    tags:
        - test
    only:
        - release/test
    variables:
        GIT_STRATEGY: none #配合cache、artifacts使用。設置為none的job里應該依賴從cache或者artifacts來的數據,而不是從倉庫獲取數據。
    before_script: #使用命令生成Dockerfile,也可以將Dockerfile單獨存放
        - echo FROM node >Dockerfile
        - echo RUN mkdir -p /opt/portal >>Dockerfile
        - echo ADD ./tar/*.tar.gz /opt/portal >>Dockerfile
        - echo WORKDIR /opt/portal >>Dockerfile
        - echo CMD node server/server.js >>Dockerfile
    script:
        - echo "docker build -t ${SVCNAME}:${VERSION} ."
        - docker build -t ${SVCNAME}:${VERSION} .

[[.gitlab-ci.yml]] 相關參數

https://docs.gitlab.com/ee/ci/yaml/

 

Keyword Description
script Shell script which is executed by Runner.
image Use docker images. Also available: image:name and image:entrypoint.
services Use docker services images. Also available: services:nameservices:aliasservices:entrypoint, and services:command.
before_script Override a set of commands that are executed before job.
after_script Override a set of commands that are executed after job.
stage Defines a job stage (default: test).
only Limit when jobs are created. Also available: only:refsonly:kubernetesonly:variables, and only:changes.
except Limit when jobs are not created. Also available: except:refsexcept:kubernetesexcept:variables, and except:changes.
rules List of conditions to evaluate and determine selected attributes of a job, and whether or not it’s created. May not be used alongside only/except.
tags List of tags which are used to select Runner.
allow_failure Allow job to fail. Failed job does not contribute to commit status.
when When to run job. Also available: when:manual and when:delayed.
environment Name of an environment to which the job deploys. Also available: environment:nameenvironment:urlenvironment:on_stopenvironment:auto_stop_in and environment:action.
cache List of files that should be cached between subsequent runs. Also available: cache:pathscache:keycache:untracked, and cache:policy.
artifacts List of files and directories to attach to a job on success. Also available: artifacts:pathsartifacts:expose_asartifacts:nameartifacts:untrackedartifacts:whenartifacts:expire_inartifacts:reportsartifacts:reports:junitartifacts:reports:cobertura, and artifacts:reports:terraform.

In GitLab Enterprise Edition, these are available: artifacts:reports:codequalityartifacts:reports:sastartifacts:reports:dependency_scanningartifacts:reports:container_scanningartifacts:reports:dastartifacts:reports:license_managementartifacts:reports:performance and artifacts:reports:metrics.
dependencies Restrict which artifacts are passed to a specific job by providing a list of jobs to fetch artifacts from.
coverage Code coverage settings for a given job.
retry When and how many times a job can be auto-retried in case of a failure.
timeout Define a custom job-level timeout that takes precedence over the project-wide setting.
parallel How many instances of a job should be run in parallel.
trigger Defines a downstream pipeline trigger.
include Allows this job to include external YAML files. Also available: include:localinclude:fileinclude:template, and include:remote.
extends Configuration entries that this job is going to inherit from.
pages Upload the result of a job to use with GitLab Pages.
variables Define job variables on a job level.
interruptible Defines if a job can be canceled when made redundant by a newer run.
resource_group Limit job concurrency.

 

[[runners]] 相關參數

https://docs.gitlab.com/runner/configuration/advanced-configuration.html

/etc/gitlab-runner/config.toml

Setting Description
name The Runner’s description, just informatory
url GitLab URL
token The Runner’s special token (not to be confused with the registration token)
tls-ca-file File containing the certificates to verify the peer when using HTTPS
tls-cert-file File containing the certificate to authenticate with the peer when using HTTPS
tls-key-file File containing the private key to authenticate with the peer when using HTTPS
limit Limit how many jobs can be handled concurrently by this token. 0 (default) simply means don’t limit
executor Select how a project should be built, see next section
shell Name of shell to generate the script. Default value is platform dependent.
builds_dir Absolute path to a directory where builds will be stored in context of selected executor (Locally, Docker, SSH)
cache_dir Absolute path to a directory where build caches will be stored in context of selected executor (locally, Docker, SSH). If the docker executor is used, this directory needs to be included in its volumes parameter.
environment Append or overwrite environment variables
request_concurrency Limit number of concurrent requests for new jobs from GitLab (default 1)
output_limit Set maximum build log size in kilobytes, by default set to 4096 (4MB)
pre_clone_script Commands to be executed on the Runner before cloning the Git repository. this can be used to adjust the Git client configuration first, for example. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
pre_build_script Commands to be executed on the Runner after cloning the Git repository, but before executing the build. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
post_build_script Commands to be executed on the Runner just after executing the build, but before executing after_script. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
clone_url Overwrite the URL for the GitLab instance. Used if the Runner can’t connect to GitLab on the URL GitLab exposes itself.
debug_trace_disabled Disables the CI_DEBUG_TRACE feature. When set to true, then debug log (trace) will remain disabled even if CI_DEBUG_TRACE will be set to true by the user.
referees Extra job monitoring workers that pass their results as job artifacts to GitLab

 

參考:

https://www.jianshu.com/p/fab407ddfed0

https://segmentfault.com/a/1190000011890710

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM