从零开始 实现Gitlab CI/CD 自动化部署项目

216 阅读2分钟

前言

服务器一定要cpu2核内存4gb以上 否则跑不起来

我用的是centos7系统

安装gitlab

**前提你的服务上啥都没装比如nginx,redis,pgsql... 这种方法可以直接使用如果装了等等就要自己看配置我会贴出来 **

第一步 在安装 GitLab 之前,需要安装一些依赖项。在终端中运行以下命令

yum install -y  policycoreutils policycoreutils-python openssh-server openssh-clients postfix

第二步 开启相关配置

systemctl enable sshd && sudo systemctl start sshd
systemctl enable postfix && systemctl start postfix

第三步 下载安装包以及安装GitLab

# 下载安装包
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 安装gitlab
sudo yum install -y gitlab-ce

第四步 修改配置文件

 # 修改配置文件
 vim /etc/gitlab/gitlab.rb
 
# 找到这一项 external_url 将这个修改你的服务器域名或者ip地址
external_url 'http://172.20.10.66:8888' # 换成你自己的
nginx['listen_port'] =8888 #你的端口号


# 如果你的地址是https的需要配置一下
nginx['enable'] = true
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 8888
nginx['ssl_certificate'] = "crt证书位置"
nginx['ssl_certificate_key'] = "证书密钥位置"

编译运行

# 执行重载命令
sudo gitlab-ctl reconfigure

# 启动服务端
sudo gitlab-ctl start


# ----------------------其他命令-----------------------
# 停止服务端
sudo gitlab-ctl stop

# 重启服务端
sudo gitlab-ctl restart

# 重载配置
sudo gitlab-ctl reconfigure

# 查看状态
sudo gitlab-ctl status

访问

172.20.10.66:8888 默认账号是root 密码需要查看/etc/gitlab/initial_root_password

# 查看密码 将密码复制过来去登录
cat /etc/gitlab/initial_root_password  

记得登录之后去修改密码因为他这个密码一天之后就会消失的

image.png

image.png

image.png

image.png

设置中文

点击头像

image.png

image.png

gitlab 安装的插件位置

/var/opt/gitlab

如果你本地安装了nginx 以及 redis , pssql 看这里

# 修改配置文件
vim /etc/gitlab/gitlab.rb

禁用自带nginx使用本地nginx

修改配置文件

nginx['enable'] = false
gitlab_workhorse['listen_network'] = "tcp" # 允许监听TCP
gitlab_workhorse['listen_addr'] = "127.0.0.1:8021" # 监听本地8021 端口

配置nginx

# 修改配置文件
vim /etc/nginx/config.d/gitlab.conf

 server {
     listen       80;  
     client_max_body_size 0; #一定要加上这个参数,gitlab push大小限制,0表示没有大小限制
     server_name _;
     server_tokens off;
     location / {
          proxy_pass http://127.0.0.1:8021; #这里与前面设置过的端口一致
          proxy_read_timeout      3600;
          proxy_connect_timeout   300;
          proxy_redirect          off;
          proxy_http_version 1.1;
          proxy_set_header Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto http;
     }
 }

最后重启 systemctl restart nginx 重载 sudo gitlab-ctl reconfigure

禁用自带redis使用本地redis

redis['enable'] = false  # 禁用自带的redis
gitlab_rails['redis_host'] = "127.0.0.1" # 本地
gitlab_rails['redis_port'] = 6379 # 端口号
gitlab_rails['redis_password'] = "12345678" # redis的密码 
gitlab_rails['redis_database'] = 10 # 数据库 默认0

禁用自带的pgsql 使用本地 pgsql

gitlab_rails['db_adapter'] = "postgresql" 
gitlab_rails['db_host'] = "127.0.0.1" 
gitlab_rails['db_port'] = 5432
gitlab_rails['db_username'] = 'gitlabuser' 
gitlab_rails['db_password'] = 'psql' 
gitlab_rails['db_database'] = 'gitlabhq_production'

安装 gitlab-runner

第一步 下载gitlab-runner安装包

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

第二步 安装

sudo yum install gitlab-ci-multi-runner -y

第三步 实现自动化

去创建一个runner

image.png

image.png

image.png

复制步骤一和步骤三去服务器上执行

前面两步都回车 最后一步的时候记得自己手动输入shell 然后在回车 输入

image.png

# 复制步骤一去执行
gitlab-runner register --url http://172.20.10.66:8888 --token glrt-uJv4WYPyoCzJyQgdkQri

# 步骤一结束之后执行步骤3
gitlab-runner run &

# 执行完毕会告诉你/etc/gitlab-runner/config.toml 编写到了这个文件里 
# 如果要删除这个注册的先停止 sudo gitlab-runner stop
# 然后去删除/etc/gitlab-runner/config.toml对应id的一项就在 gitlab-runner run & 启动就ok了

第四步 演示自动化

创建一个仓库 image.png

// 创建项目
yarn create vite test --template react-ts

// 初始化项目 添加链接
cd test
git init --initial-branch=main
git remote add origin http://172.20.10.66:8888/xwya/test_11.git


// 在test文件夹下新建一个.gitlab-ci.yml的文件
// 配置如下:
# 阶段
stages:
  - install
  - build
  - deploy

cache:
  paths:
    - node_modules/

# 安装依赖
install:
  stage: install
  # 规定仅在package.json提交时才触发此阶段
  only:
    changes:
      - package.json
  # 执行脚本
  script:
    npm install

# 打包项目
build:
  stage: build
  script: 
    - npm install
    - npm run build
  # 将此阶段产物传递至下一阶段 
  artifacts: 
    paths:
        - dist/

# 部署项目
deploy:
  stage: deploy
  script: 
    # 清空网站根目录,目录请根据服务器实际情况填写
    - rm -rf /data/test/*
    # 复制打包后的文件至网站根目录,目录请根据服务器实际情况填写
    - cp -rf ${CI_PROJECT_DIR}/dist/* /data/test/

最后提交代码

git add .
git commit -m "Initial commit"
git push --set-upstream origin main

那个失败是因为没有写.gitlab-ci.yml文件直接提交了 所以失败了

image.png image.png

你每次修改代码 提交一下就会 自动去打包和部署

注意注意!!

我这里是提前配置好了nginx 以及 安装好了node环境 你们自己去安装一下然后就可以部署项目了