GitLab CI : 从零到一 自建仓库、自动构建发布

327 阅读1分钟

前言

纯小白操作,以下内容都是经过本人实践的  
服务器要求最好是4G或以上的,系统镜像我用的是CentOs 7.6

1.新建文件 /etc/yum.repos.d/gitlab-ce.repo

[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

2.建立缓存&安装

yum makecache
yum install -y gitlab-ce

3.安装成功后修改gitlab.rb文件的external_url

image.png

改为自己的服务器IP地址或者域名

cd /etc/gitlab/
vi gitlab.rb

4.更新配置

gitlab-ctl reconfigure

5.配置成功&修改初始密码

gitlab自带nginx,默认80端口,配置成功后直接访问服务器ip 第一次登陆使用初始密码,位置在 /etc/gitlab/initial_root_password

image.png

6.修改密码

image.png

此时就在自己的服务器上搭建好了gitlab

接下来配置runner

7.新建文件 /etc/yum.repos.d/gitlab-runner.repo

[gitlab-runner]
name=gitlab-runner
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el$releasever-$basearch/
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

8.建立缓存&安装

yum makecache
yum install -y gitlab-runner

9.注册runner

10.新建一个项目,在设置-ci中找到runner配置需要的url和token

image.png

11.开始注册
sudo gitlab-runner register

image.png

12.初始化一个vite项目,体验完整构建流程

npm init @vitejs/app
13.添加.gitlab-ci.yml文件
stages:
  - install
  - build
  - deploy

cache:
  paths:
    - node_modules
    - dist

install-job:
  stage: install
  only:
    - main
  script:
    - npm install
    - echo "install success"

build-job:
  stage: build
  only:
    - main
  script:
    - npm run build
    - echo "build success"

deploy-job:
  stage: deploy
  only:
    - main
  before_script:
  script:
    -sudo cp -r ./dist/* /html/individual-project
  after_script:
    - echo "deploy success"

14.安装node

cd /usr/local/src
wget https://nodejs.org/dist/v12.21.0/node-v12.21.0-linux-x64.tar.xz
解压
tar -xvf node-v12.21.0-linux-x64.tar.xz
重命名
mv node-v12.21.0-linux-x64 node-v12.21.0
建立软链
ln -s /usr/local/src/node-v12.21.0/bin/node /usr/bin/node
node -v
ln -s /usr/local/src/node-v12.21.0/bin/npm /usr/bin/npm
npm -v
复制代码

15.部署当前服务器

部署到当前服务器可以用gitlab默认安装的nginx

16.修改nginx配置

/var/opt/gitlab/nginx/conf/nginx.conf

  server {
    listen       8081;
    server_name  81.70.105.238;
        
    location / {
        root   /html/individual-project;
        index  index.html index.htm;
    }
        
    error_page   500 502 503 504  /50x.html;
        
    location = /50x.html {
        root   html;
    }
  }
复制代码

17.重启nginx

gitlab-ctl restart nginx
复制代码

给自己的runner添加sudo权限 编辑文件,/etc/sudoers 文件,然后在如下 root 的下一行增加如下一行

root ALL=(ALL) ALL 
gitlab-runner ALL=(ALL) NOPASSWD:ALL

将代码推到gitlab上之后访问自己的ip:8089(记得开启自己的安全组) 此文章借鉴juejin.cn/post/703082… 结合我自己踩的坑来做的。。