前端 Gitlab 自动部署

前端 Gitlab 自动部署

根据自己的系统,下载对应二进制安装文件

# Linux x86-64
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"

# Linux x86
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386"

# Linux arm
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm"

# Linux arm64
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64"

# Linux s390x
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-s390x"

授予执行权限

sudo chmod +x /usr/local/bin/gitlab-runner

创建一个 Gitlab CI 账户

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

安装、运行

# 安装
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# 启动
sudo gitlab-runner start

此时, gitlab-runner 已经安装完毕了。

然后注册一个 runner:

gitlab-runner register

输入上述命令后,会出现以下内容

# 要输入你的 git 地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
# 输入你项目的 token, 这个 token 在项目的 settings -> CI/CD -> Runner 中可以找到
Enter the registration token:
# 输入你 runner 的简介,主要用于多个 runner 的区分,可不填
Enter a description for the runner:
# 输入你用于部署的 tags
Enter tags for the runner (comma-separated):
# 根据你的环境和部署方式自行选择,我这里使用的是 shell
Enter the executor: parallels, shell, ssh, virtualbox, docker+machine, docker, docker-ssh, docker-ssh+machine, kubernetes:

到此,会出现 Runner registered successfully 说明你已经成功注册一个 runner。
进入项目中找到 Runner settings -> CI/CD -> Runner 你就会看到一个已经整装待发的 runner 了。

项目部署

到项目的个目录创建一个名为 .gitlab-ci.yml 的 jobs 文件,文件内的内容大概如下

image: node  # 选用docker镜像

stages: # Stages 表示构建阶段,这里有两个阶段 install, deploy
  - install
  - deploy

cache:
  key: "$CI_COMMIT_REF_SLUG"
  paths:
    - node_modules

# 开发环境
install-staging: 
  stage: install
  tags:
    - primary
  only: # 定义了只有在被merge到了如下分支上 才会执行部署脚本。
    - dev
    - release
    - master
  script:
    - echo "===== start install ======"
    - npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
    - npm install --registry=https://registry.npm.taobao.org  #安装依赖
    - echo "===== end install ======"


deploy-staging:
  stage: deploy
  tags:
    - primary
  only:
    - dev
    - release
    - master
  script:
    - echo "===== start build ======"
    - npm run build:beta # 项目打包
    - echo "===== end build ======"
    - echo "===== start deploy ======"
    - npm run deploy # 上传至部署服务器
    - echo "===== end deploy ======"

当我们编辑好文件后,push 我们的文件至 dev、release、master 的时候,就会进行自动部署了

遇到的问题

  1. git fetch-pack: expected shallow list
# 这是因为服务器版本过低的原因,通过升级 `git` 版本来解决
yum install -y http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
# 然后
yum update git
  1. npm command not found
# 虽然我们已经安装了 node,但是当前用户的目录下找不到
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
  1. gitlab-runner: command not found
    当我使用sudo gitlab-runner install 时, 反馈我的是, gitlab-runner: command not found找了很久的文档没有解决方案, 后来我是进到 gitlab-runner 安装目录直接使用 ./gitlab-runner install 上面的安装命令 来进行安装的。

其他

如果你的服务器还没有安装 git 和 node 可参考如下文章进行安装

安装 git

yum install -y git

安装完成之后, 使用 git --version 查看是否已成功安装。

安装 nvm、node

至于为什么不直接安装 node,因为我这里对版本有需求,如果没有需求的,可自行百度直接安装即可。

两种安装办法:
1.curl

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

不过这个方法,可能会出现 443连接拒绝。可先通过https://www.ipaddress.com/
查询一下 raw.githubusercontent.com 对应的IP 地址,然后在 /etc/hosts 中添加对应的解析即可

2.git

# 下载 nvm
git clone git://github.com/creationix/nvm.git ~/nvm
# 然后进入到相应目录,执行 install.sh 进行安装 nvm
cd ~/nvm
./install.sh

等待安装结束后, 加入系统环境

source   ~/.bashrc

查看所有的 node 版本

nvm list-remote

安装需要的版本

nvm install v10.24.0

查看所有已安装的版本

nvm list

查看 node npm 版本

node -v
npm -v

至此, 已经全部安装完成

你可能感兴趣的:(前端 Gitlab 自动部署)