gitlab-runner安装

1.gitlab-runner安装

1.1 自动安装

1.1.1添加GitLab repository

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

1.1.2安装

sudo apt-get install gitlab-runner

1.2手动安装

1.2.1下载安装包

# Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
# A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_${arch}.deb"

${arch}换成amd64, arm, arm64其中任意一个,根据平台决定

1.2安装

#<arch>替换同上
dpkg -i gitlab-runner_<arch>.deb

2.gitlab-runner配置

2.1注册

gitlab-runner register --non-interactive \
--executor "shell" \ #构建项目方式
--url "http://192.168.1.200/" \ #对应gitlab地址
--registration-token "uo7sXcM4F66XA-6qv6GM" \ #对应下图令牌
--description "树莓派" \ #runner描述
--tag-list "build_rasp_test" \ #runner标签
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"

默认配置runner配置文件在*~/.gitlab-runner/config.toml*下

参数说明
name 描述
url GitLab 实例 URL
token runner的的特殊令牌(不是注册令牌)
tls-ca-file 使用 HTTPS 时验证对等方的证书的文件
tls-cert-file 使用 HTTPS 时与对等方进行身份验证的证书的文件
tls-key-file 使用 HTTPS 时要与对等方进行身份验证的私钥的文件
limit 限制同时处理作业数量,0(默认)表示不限制
executor 选择应如何构建项目
shell 生成脚本的 shell 的名称,默认值取决于平台。
builds_dir 构建存储在所选执行程序上下文中的目录的绝对路径。例如,本地、Docker 或 SSH。
cache_dir 构建缓存存储在所选执行程序上下文中的目录的绝对路径。例如,本地、Docker 或 SSH。如果使用dockerexecutor,则需要在其volumes参数中包含该目录
environment 追加或覆盖环境变量。
request_concurrency 限制来自 GitLab 的新作业的并发请求数,默认为1
output_limit 最大构建日志大小,默认值为4096(4MB)
pre_clone_script 在克隆 Git 存储库之前执行的命令
pre_build_script 在克隆 Git 存储库之后但在执行构建之前执行的命令
post_build_script 在执行构建之后执行的命令
clone_url 覆盖 GitLab 实例的 URL
debug_trace_disabled 禁用CI_DEBUG_TRACE特性。当设置为true时,即使用户将CI_DEBUG_TRACE设置为true,调试日志(跟踪)也将保持禁用状态
referees 将结果作为工作工件传递给 GitLab

2.2更改runner权限

ps aux|grep gitlab-runner #可以查看到gitlab-runner的工作目录和默认用户等一系列相关信息。

1.卸载gitlab-runner默认用户

sudo gitlab-runner uninstall

2.将用户设置为root

sudo gitlab-runner install --working-directory /home/gitlab-runner --user root

3.重启服务

sudo systemctl restart gitlab-runner.service

4.查看gitlab-runner进程

ps aux|grep gitlab-runner

**注:**配置runner为root权限时,系统环境变量需要配置在/etc/profile中,runner配置需要将~/.gitlab-runner/config.toml复制到/etc/gitlab-runner/中

sudo cp ~/.gitlab-runner/config.toml /etc/gitlab-runner/

配置成功之后会在CI中看到已经运行

若出现问题查看系统日志

tail -100 /var/log/syslog

3.gitlab-runner命令

3.1启动命令

gitlab-runner --debug <command>   #调试模式排查错误特别有用。
gitlab-runner <command> --help    #获取帮助信息
gitlab-runner run       #普通用户模式  配置文件位置 ~/.gitlab-runner/config.toml
sudo gitlab-runner run  # 超级用户模式  配置文件位置/etc/gitlab-runner/config.toml

3.2注册命令

gitlab-runner register  #默认交互模式下使用,非交互模式添加 --non-interactive
gitlab-runner list      #此命令列出了保存在配置文件中的所有运行程序
gitlab-runner verify    #此命令检查注册的runner是否可以连接,但不验证GitLab服务是否正在使用runner。 --delete 删除
gitlab-runner unregister   #该命令使用GitLab取消已注册的runner。


#使用令牌注销
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n

#使用名称注销(同名删除第一个)
gitlab-runner unregister --name test-runner

#注销所有
gitlab-runner unregister --all-runners

3.3服务管理

gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

# --user指定将用于执行构建的用户
#`--working-directory  指定将使用**Shell** executor 运行构建时所有数据将存储在其中的根目录

gitlab-runner uninstall #该命令停止运行并从服务中卸载GitLab Runner。

gitlab-runner start     #该命令启动GitLab Runner服务。

gitlab-runner stop      #该命令停止GitLab Runner服务。

gitlab-runner restart   #该命令将停止,然后启动GitLab Runner服务。

gitlab-runner status #此命令显示GitLab Runner服务的状态。当服务正在运行时,退出代码为零;而当服务未运行时,退出代码为非零。```


4.GitLab CI

1.样例

项目中添加.gitlab-ci.yml文件

样例:

stages:
  - build
  #- publish
stages:
  - build

build_linux_rasp:
  stage: build
  script:
    - sh ./make.sh #对应执行脚本
  only:
    - /^JamSystem_Tiny.#当符合JamSystem_Tiny开头为tag添加时候触发job
  tags:
    - build_rasp_test #runner tag标签,指定执行job服务器

2.部分关键字说明

2.1 stage

stage 是阶段的意思,用于归档一部分的job,按照定义的stage顺序来执行。 默认的stage有build,test,deploy, 此外还有两个特殊的.pre 和 .post job执行的顺序不是按照编写的顺序,大体上是按照stage定义的顺序来执行的,注意是大体,也有例外的情况。

stages:
  - build
  - test
  - deploy

job 0:
  stage: test
  script: echo 'tets'

job 1:
  stage: build
  script: echo 'build'

由于buildtest之前所有会指向job1这个任务,后指向job0任务

2.2 script

任务要执行的shell脚本内容,内容会被runner执行,在这里,你不需要使用git clone ....克隆当前的项目,来进行操作,因为在流水线中,每一个的job的执行都会将项目下载,恢复缓存这些流程,不需要你再使用脚本恢复。你只需要在这里写你的项目安装,编译执行,如 npm install 另外值得一提的是,脚本的工作目录就是当前项目的根目录,所有可以就像在本地开发一样。此外script可以是单行或者多行

单行脚本

job:
  script: npm install

多行脚本

job:
  script:
    - npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
    - npm install --registry=http://registry.npm.taobao.org

script是一个job的必填内容,不可或缺。一个job最少有二个属性,一个是job name, 任务名称, 一个就是script。

2.3 only/except

only/except 是规定当前job的可见状态,一个项目有很多分支,tag,我们的流水线,为了对特定的分支,特定的tag执行不同的job,这里就要使用only和except 在任务上加上这一段代码,就表明当前任务只有在master分支可以运行

only:
  - master

也可以根据当前的代码变动是合并,还是推送,还是使用API来触发的。 如果一个任务没有only属性,那默认就是 only: ['branches', 'tags'] 操作分支或者tags都会触发流水线。

2.4 其他关键字说明

关键字是否必须描述
script必须定义Runner需要执行的脚本或命令
image非必须需要使用的docker镜像,请查阅该文档
services非必须定义了所需的docker服务,请查阅该文档
stage非必须定义了工作的场景阶段,默认是test
type非必须stage的别名,不赞成使用
variables非必须在job级别上定义的变量
only非必须定义哪些git引用(分支)适用该job
except非必须定义了哪些git引用(分支)不适用该job
tags非必须定义了哪些runner适用该job(runner在创建时会要求用户输入标签名来代表该runner)
allow_failure非必须允许任务失败,但是如果失败,将不会改变提交状态
when非必须定义job什么时候能被执行,可以是on_success,on_failure,always或者manual
dependencies非必须定义了该job依赖哪一个job,如果设置该项,你可以通过artifacts设置
artifacts非必须所谓工件。。就是在依赖项之间传递的东西,类似cache,但原理与cache不同
cache非必须定义需要被缓存的文件、文件夹列表
before_script非必须覆盖在根元素上定义的before_script
after_script非必须覆盖在根元素上定义的after_script
environment非必须定义让job完成部署的环境名称
retry非必须定义job失败后的自动重试次数

备注:.gitlab-ci.yml配置详细说明:The .gitlab-ci.yml file | GitLab

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值