centos7.5安装gitlab-runner,配置CI/CD流水线

一般不建议gitlab-server和gitlab-runner装在同一台服务器

第一步:安装gitlab-runner,最好和gitlab实例版本一致

bash 复制代码
# 下载官方gitlab-runner安装脚本
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash

# 安装gitlab-runner
sudo yum install gitlab-runner

第二步:创建runner实例

一般分为共享型runner和项目型runner

共享型的,任何项目都可使用它来执行流水线

项目型的,只有特定项目可以使用它来执行流水线

共享型的创建,在管理员的管理面板:

项目型的创建,在项目的设置-ci/cd 管理面板:

第三步:注册runner

bash 复制代码
sudo gitlab-runner register
# 交互式注册,填入生成的token等等...


gitlab-runner在运行时默认使用"gitlab-runner"用户,该用户没有root访问权限,所以对于运行脚本时,某些操作会没有权限,导致流水线失败,需要手动解决。

第四步:项目根目录加入gitlab-ci.yml配置文件

bash 复制代码
default:
  artifacts:
    paths:
      - dist # 打包产物相对路径(相对于当前项目)
# 定义步骤
stages:
    - build
    - test
    - deploy

# 每个job运行前执行
before_script:  
  - echo "前"  
  - echo $(whoami) # gitlab-runner在运行时默认使用"gitlab-runner"用户,该用户没有root访问权限,所以对于运行脚本时,某些操作会没有权限,导致流水线失败,需要手动解决。
  - pwd # 每个runner执行时都有自己的工作目录,在gitlab-runner的home空间中 /home/gitlab-runner/builds/xxx
  - ls -al

# 每个job运行后执行
after_script:  
  - echo "后" 
  - ls -al

# 打包阶段
build-job:
    stage: build
    tags:
        - runner1 # 指定具体runner去执行
    script:
        - echo "Building the application"
        - npm install
        - npm run prod
    only:
        - main

# 测试阶段
test-job:
    stage: test
    tags:
        - runner1
    script:
        - echo "Testing the application" 
    only:
        - main

# 部署阶段
deploy-job:
    stage: deploy
    tags:
        - runner1
    script:
        - echo "Deploying the application" 
        - mv dist/* /usr/share/nginx/html # 移动文件到html文件夹(用到之前定义的打包产物相对路径)
    only:
        - main
相关推荐
星释5 小时前
如何自动部署GitLab项目
gitlab
keson要进步6 小时前
CICD实战(一) -----Jenkins的下载与安装
运维·ci/cd·centos·自动化·jenkins
keson要进步6 小时前
CICD实战(二)-----gitlab的安装与配置
linux·运维·gitlab
码农黛兮_466 小时前
Git 常用命令大全
git
一弓虽6 小时前
git 学习
git·学习
猫头虎11 小时前
[特殊字符]解决 “IDEA 登录失败。不支持早于 14.0 的 GitLab 版本” 问题的几种方法
java·ide·网络协议·http·https·gitlab·intellij-idea
风早君13 小时前
jenkins集成gitlab发布到远程服务器
服务器·gitlab·jenkins
爱宇阳14 小时前
使用 Docker Compose 部署 Jenkins(LTS 版)持续集成环境
ci/cd·docker·jenkins
疯狂的沙粒14 小时前
如何通过git命令查看项目连接的仓库地址?
大数据·git·elasticsearch
qq_2546177715 小时前
Gerrit+repo管理git仓库,如果本地有新分支不能执行repo sync来同步远程所有修改,会报错
git