最近开发vue项目时经常需要打包部署到测试环境,甚至每天都要部署三四次,然后便想到了使用GITLAB-CI的方式做持续集成.一来不用每次手动打包了,二来也方便管理维护(题主本地开发系统为win10, 服务器系统为CentOS 7.6.1810)
gitlab CentOS版本 - 14.6.1
gitlab-runner CentOS版本 -> 15.5.0
- 服务器安装git
- 服务器配置ssh免密登录
- 服务器配置docker
- 服务器docker配置node镜像
- 服务器安装gitlab-runner
- 服务器注册runner
- 配置gitlab-ci变量
- 编写.gitlab-ci.yml脚本文件
一. 服务器安装git
1. yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
2. yum -y install git
3. git version
二. 服务器配置ssh免密登录
本地机打开git窗口后依次运行
1. 生成秘钥对执行 ssh-keygen -t rsa -C "username@163.com" -f C:/Users/Admin/.ssh/id_rsa_129 后直接一路回车就可以
2. 在 C:/Users/Admin/.ssh/config 文件来指定需要访问服务器的用户名, IP, 端口, 秘钥(如果没有config文件,需自己创建一个)
Host 192.168.159.129
Port 22
HostName 192.168.159.129
User xiaoheihei
IdentityFile ~/.ssh/id_rsa_129
3. 使用 ssh-copy-id 192.168.159.129 命令免密登录服务器
ps: 为什么生成秘钥对的时候要加邮箱和指定秘钥名称?
因为后续我们可能会有多个项目都需要使用CI来管理, 这样可以方便区分
三. 服务器配置docker
1. 安装 yum -y install docker
2. 运行 service docker start
3. 检查是否启动成功 docker ps
四. 服务器docker配置node镜像
1. 拉取镜像 docker pull node:16.17.0
2. 查看是否安装成功 docker images
五. 服务器安装gitlab-runner
下面为 root 用户操作
1. curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
2. chmod +x /usr/local/bin/gitlab-runner
3. useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
4. gitlab-runner install --user=xiaoheihei --working-directory=/home/gitlab-runner
5. systemctl start gitlab-runner
六. 服务器注册runner
url 和 token 在 gitlab当前项目 -> settings -> CI/CD -> Runners 下
reg.png
1. Enter the GitLab instance URL (for example, https://gitlab.com/)
- http://192.168.21.26/
2. Enter the registration token:
- iF3yjzXgxjD8p6xt81oJ
3. Enter a description for the runner:
- 描述
4. Enter tags for the runner (comma-separated):
- test
ps: tag非常重要,.gitlab-ci.yml脚本的执行就是根据tag判断的
5. Enter optional maintenance note for the runner:
- 随意
6. Enter an executor: instance, kubernetes, docker, docker-ssh, parallels, virtualbox, docker-ssh+machine, custom, shell, ssh, docker+machine:
- docker
7. Enter the default Docker image (for example, ruby:2.7):
- node:16.17.0
ps: 之前 docker 安装的 node 镜像
完整步骤如下:
runner-reg.jpg
七. 配置gitlab-ci变量
配置变量的位置在 gitlab当前项目 -> settings -> CI/CD -> Variables 下
依次创建变量
ENV_129_SSH_PRIVATE_KEY 值为 C:/Users/Admin/.ssh/id_rsa_129 文件内容
ENV_129_SSH_CONFIG 值为 C:/Users/Admin/.ssh/config 文件内容
ENV_129_SSH_KNOWN_HOST 值为 C:/Users/Admin/.ssh/known_hosts 文件内容
ENV_129_USERNAME 值为 服务器用户名(eg: xiaoheihei)
ENV_129_HOST 值为 服务器IP(eg: 192.168.159.129)
ENV_129_PATH 值为 ~/html/
八. 编写.gitlab-ci.yml脚本文件
image: node:16.17.0 # 使用 docker node:16.17.0镜像
stages: # pipeline 执行步骤
- build_129
- deploy_129
cache: # 缓存
paths:
- node_modules
- dist
build_129:job: # 执行 build_129 阶段
tags:
- "test" # 对应服务器注册 runner 时输入的 tag
only:
- main # 仅在main分支有提交代码时执行该阶段
stage: build_129 # 阶段标识
before_script: # 执行命令前
- yarn -v
script: # 执行命令
- yarn
- yarn build
artifacts: # 产物
paths: # 产物生成地址
- dist
when: on_success # 生命周期钩子
expire_in: 1 days # 存储失效
deploy_129:job: # 执行 deploy_129 阶段
tags:
- "test" # 对应服务器注册 runner 时输入的 tag
only:
- main # 仅在main分支有提交代码时执行该阶段
stage: deploy_129 # 阶段标识
environment: # 环境变量
name: "129" # 环境变量名称
before_script: # 执行命令前
- mkdir -p ~/.ssh # 创建 .ssh 文件夹
- chmod 700 ~/.ssh # 权限改为 700
- eval $(ssh-agent -s) # 私钥程序
- echo "$ENV_129_SSH_KNOWN_HOST" > ~/.ssh/known_hosts # 将 ENV_129_SSH_KNOWN_HOST 变量的内容输出到 known_hosts 文件中
- chmod 644 ~/.ssh/known_hosts # 将 known_hosts 文件权限改为 644
- echo "$ENV_129_SSH_CONFIG" > ~/.ssh/config # 将 ENV_129_SSH_CONFIG 变量的内容输出到 config 文件中
- chmod 600 ~/.ssh/config # 将 config 文件权限改为 600
- echo "$ENV_129_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa_129 # 将 ENV_129_SSH_PRIVATE_KEY 变量的内容输出到 id_rsa_129 文件中
- chmod 600 ~/.ssh/id_rsa_129 # 将 id_rsa_129 文件权限改为 600
- ssh-add ~/.ssh/id_rsa_129 # 将密钥添加到 ssh-agent 的高速缓存中
script: # 执行命令
- scp -r ./dist $ENV_129_USERNAME@$ENV_129_HOST:$ENV_129_PATH # 将当前 dist 文件夹递归复制到 xiaoheihei@192.168.159.129:~/html/ 下
整理遇到的问题:
- 免密登录无响应
尝试配置服务器 /etc/ssh/sshd_config 文件中
StrictModes no
RSAAuthentication yes
PubkeyAuthentication yes - 免密登录配置后仍需要输入密码
尝试配置服务器 /etc/ssh/sshd_config 文件中
PasswordAuthentication no - 注册好runner后在gitlab中提示New runner. Has not connected yet
查看/etc/gitlab-runner/config.toml中是否有刚刚注册的runner信息,如果没有尝试切换root账户重新注册