偷懒之 Gitlab-runner 基础篇

写在前面

GitLab Runner 是一个用于在 GitLab CI/CD 环境下执行自动化构建、测试和部署任务的工具。
特点:开源、持续集成/持续交付[CI/CD]、自动化流水线[Pipeline]

runner安装

gitlab-ruuner安装文档:docs.gitlab.com/runner/install/lin...

# 先检查git版本,请保证git为最新版本
  git version  
# 安装最新git
  sudo yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
  sudo yum install git -y
# 再检查 gitlab 项目地址,如若域名/ip不对请先修改,保证能正常 git 项目
  find / -name gitlab.yml   # 查找配置文件
  vim /path/***/gitlab.yml  # 修改 host

# 安装 gitlab-runner
Ubuntu:
  curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
  sudo apt-get install gitlab-runner

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

# 启动
  sudo gitlab-runner start
# 自定义 run 默认以gitlab-runner启动,这里偷懒以www用户启动避免与nginx权限冲突
  gitlab-runner run -d "/www/wwwroot" -c "/etc/gitlab-runner/config.toml" -n "gitlab-runner" -u "www"
# systemctl restart gitlab-runner 
# 路径:/etc/systemd/system/gitlab-runner.service
# 修改启动参数
  vim /etc/systemd/system/gitlab-runner.service 
  ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/www/wwwroot" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "www"

runner注册

注册文档:docs.gitlab.com/runner/register/in...

 # 交互式:
  sudo gitlab-runner register
  # 默认 url+registration-token
  sudo gitlab-runner register --url http://192.168.100.88:8099/ --registration-token GR1348941ea1h7-WAwB6LpHSpJZ6S

# 命令行:
  sudo gitlab-runner register \
  --non-interactive \
  --url "http://192.168.100.88:8099" \
  --registration-token "GR1348941ea1h7-WAwB6LpHSpJZ6S" \
  --executor "shell" \
  --description "gitlab-runner" \
  --tag-list "tag-test" \
  --run-untagged="false" \
  --locked="false" \
  --access-level="not_protected";

# 注册相关命令:
  # 可查看组成的runner基本信息(token、url)
  sudo gitlab-runner list
  # 取消注册
  sudo gitlab-runner unregister -t=runner-token
  # help 
  sudo gitlab-runner --help

# register常用命令说明:
  --non-interactive  命令行时该参数必须
  --ur1  gitlab服务器地址,如端口不是80则需要将端口加上
  --registration-token  Runner Token , gitlab平台获取
  --executor  执行器引擎,建议shell。选择其他引擎项及相关参数请自行参考文档
  --description  Runner描述信息
  --tag-1ist  Runner的Tag信息,后续作业可通过tag指定在哪个runner上运行构建Runner
  --run-untagged  运行未指定tag的作业,建议关闭:false
  --1ocked  注册成功后runner为锁定状态,关闭
  --access-level  runner的访问级别,not_protected表示不受保护

注意事项:

  1. 注册完成,生成配置文件:Configuration (with the authentication token) was saved in “/etc/gitlab-runner/config.toml”
  2. 私网/局域网时runner可能在初始连接仓库时因gitlab为公网地址导致报错(runner执行job开始阶段)。手动修改 “/etc/gitlab-runner/config.toml” 配置仓库地址 clone_url
    # config.toml => [[runners]]
      clone_url = "http://192.168.100.88:8099"
  3. gitlab-runner 注册成功后,刷新 gitlab 平台的 CI/CD-Runner 页面即可看到了,在 gitlab 平台可进行修改删除

.gitlab-ci.yml

在 gitlab 管理项目根目录下,新建文件 .gitlab-ci.yml 即可,或在“构建-流水线编辑”初始化 .gitlab-ci.yml 文件。

说明文档:docs.gitlab.com/17.0/ee/ci/yaml/
参考文档:fennay.github.io/gitlab-ci-cn/gitl...
常用参数说明:

参数 说明
script 唯一必填参数,需要执行的脚本代码
after_script 定义在每个job之后运行的命令,会在j0b执行结束后执行,可以是数组或多行字符串
before_script 定义在每个job之前运行的命令,会早于j0b执行,可以是数组或多行字符串
allow_failure 允许job失败,不影响任务job的commit状态
variables 定义构建变量(job外全局,job下当前job)
when 指定何时运行job,默认为onsuccess即所有任务都完成后运行当前任务
include 允许job加载其他的yaml文件
cache 定义一组文件列表,后续运行中使用
stages 定义流水线所有阶段,阶段按定义顺序的优先级运行(types已废弃)
stage job所处阶段,默认为test
tags 定义tags,指定选择哪个runner运行该任务(匹配runner设置的tag)
trigger 指走下游流水线的触发器
only 指定哪些分支会触发任务,可定义多个
except 指定哪些分支不触发job,即该任务在指定的分支上不执行(优先only)
coverage 作业的代码覆盖率,正则表达式是唯一有效的值,”code_coverage:d+.\d+/
dependencies job依赖关系,可通过该参数实现相互传递artifacts
retry 指定job失败时可自动执行多少次
environment 任务部署的环境变量
image docker镜像使用
services docker镜像使用
parallel 指定并行运行的job实例
pages 上传gitlab_pages的结果

以下是一个 laravel8 项目的 .gitlab-ci.yml,仅供参考:

# 配置项目基本变量信息
variables:
 PROJECT_NAME: "laravel8"
 PROJECT_PATH: "/www/wwwroot/laravel8"
 PROJECT_RUNNER_TAG_FIRST: "tag-test"
 COMPOSER_INSTALL: false

# 定义一个pipeline[流水线]不同的阶段顺序执行,阶段绑定任务:jobs
stages:          # List of stages for jobs, and their order of execution
 - build
 - test
 - deploy

# build 阶段任务
build-job:
 stage: build
 tags:
 - $PROJECT_RUNNER_TAG_FIRST
 script:
 - echo "build the code"
 - pwd
 - ls -a
 - if $COMPOSER_INSTALL; then composer install --ignore-platform-reqs --no-dev --optimize-autoloader; fi;
 - ls -a
 - echo "build complete"

# test 阶段任务, 同一阶段可多任务并行执行
unit-test-job:
 stage: test
 only:
 - test
 tags:
 - $PROJECT_RUNNER_TAG_FIRST
 script:
 - echo "Running unit tests"
 - git branch
 - git checkout test
 - git branch
 - echo "unit-test-job complete"
lint-test-job:
 stage: test
 only:
 - test
 tags:
 - $PROJECT_RUNNER_TAG_FIRST
 script:
 - echo "Linting code"
 - pwd
 - git branch
 - echo "lint-test-job complete"

# deploy 阶段任务
deploy-job:
 stage: deploy
 only:
 - master
 tags:
 - $PROJECT_RUNNER_TAG_FIRST
 before_script:
 - echo "deploy-job start"
 - git branch
 - git checkout master
 - git branch
 - pwd
 script:
 - ls -a
 - if $COMPOSER_INSTALL; then composer install --ignore-platform-reqs --no-dev --optimize-autoloader; fi;
 - ls -a
 - mkdir -p $PROJECT_PATH
 - cp -rf ./* $PROJECT_PATH
 - chmod -R 755 $PROJECT_PATH
 - cd $PROJECT_PATH
 - chmod -R 777 ./storage ./bootstrap/cache
 after_script:
 - echo "deploy-job successfully. $PROJECT_NAME"
 - pwd
 - ls -a
 - date
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!