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
相关推荐
月初,2 小时前
Git 常用操作大全(超详细教程)一文教会你完全使用Git
大数据·git·elasticsearch
李少兄3 小时前
Git 忽略文件机制:.gitignore 与 .git/info/exclude
java·git·elasticsearch
github.com/starRTC7 小时前
Claude Code中英文系列教程17:将Claude Code集成在GitLab工作流里面
git·gitlab·github
CCC:CarCrazeCurator9 小时前
git merge与rebase的区别及实操心得体会
git
草莓熊Lotso9 小时前
Qt 控件美化与交互进阶:透明度、光标、字体与 QSS 实战
android·java·开发语言·c++·人工智能·git·qt
小明同学0110 小时前
[C++进阶]深入理解二叉搜索树
开发语言·c++·git·visualstudio
muddjsv10 小时前
Git Amend 完全解析:修改最近提交的正确姿势与避坑指南
git
ssxueyi10 小时前
Git 完整安装与环境配置教程(Windows/macOS/Linux 通用)
windows·git·macos·项目管理·git教程·代码管理
摇滚侠11 小时前
在 IDEA 中,GIT 合并分支时选择远程的 dev 分支和本地的 dev 分支,有区别吗
java·git·intellij-idea
可问春风_ren11 小时前
Git命令大全
前端·javascript·git·后端