1、安装gitlab-runner

yum包安装:
yum install -y gitlab-runner

rpm包安装:
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_${arch}.rpm"
rpm -i gitlab-runner_<arch>.rpm

查看是否安装完成:
gitlab-runner --version
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

2、注册Runner服务到gitlab上

注册命令:
gitlab-runner register

注册是要输入的URL和token如下图的位置可以查到,然后还有名称 、Tag、最后是选择一个执行器目前选择的shell,不好意思这个地方没有截图。
  • 1.
  • 2.
  • 3.
  • 4.

注册是要是输入的URL和token信息

gitlab-runner安装配置_rpm包

这次完成以后修改一下配置保存

gitlab-runner安装配置_rpm包_02

3、到这里就注册成功了,开始编写.gitlab-ci.yml文件。

stages:   #指定剧本各步骤
  - build
  - deploy
  - rollback
        
build:
  stage: build
  script:
    - docker build . -t harbor.xxx.com/product/test:v0.0.1
  tags:
    - "215-test"  #指定服务运行的runner,tag注册是指定的名字
  only:
    - tags    #通过Repository下面的tags运行

deploy:
  stage: deploy
  script:
    - echo "This job compiles code."
  when: manual  #运行时添加手动触发
  only:
    - tags

rollback:
  stage: rollback
  script:
    - echo "This job compiles code."
  when: manual
  only:
    - tags

rollback2:
  stage: rollback
  script:
    - echo "This job compiles code."
  when: manual
  only:
    - tags
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

对应截图

gitlab-runner安装配置_rpm包_03

gitlab-runner安装配置_git_04

附加:runner权限问题配置

在runner执行过程中大多数是文件夹不存在,无权限。
gitlab-runner uninstall # 删除gitlab-runner
gitlab-runner install --working-directory /home/gitlab-runner --user root   # 安装并设置--user(设置为root)
service gitlab-runner restart  # 重启gitlab-runner
ps aux|grep gitlab-runner  # 查看当前runner用户
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.