公司前端大佬由于某些缘由离职了,走的比较匆忙,本身以前一直不多接触这方面的东西,一直都是只知其一;不知其二。这两天我一边学,一边动手搭建,同时记录整个搭建过程。html
这是一系列文章,从搭建 Gitlab 到 安装、注册Gitlab-runner 再到两者结合去部署一个简单的项目,经过这几篇文章,你将学会如何在 Gitlab 上自动化打包部署本身的项目。前端
系列文章一共有四篇,包括:node
因为本身一直作的是前端,对于Linux我不算熟练,若有错误的地方,请你们指出。linux
原文地址:安装GITLAB-RUNNERgit
这篇是系列的第二篇,咱们会安装 Gitlab-runner 并编写一个简单的 .gitlab-ci.yml 文件,看一下运行效果。
github
两台服务器,个人都是Linux CentOS 7.6 64位 一台用须要安装 Gitlab,关于如何安装 Gitlab ,可查看这篇文章 阿里云安装GITLAB笔记。 另外一台用于安装 Gitlab-runner。docker
# Linux x86-64
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Linux x86
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
# Linux arm
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
复制代码
sudo chmod +x /usr/local/bin/gitlab-runner
复制代码
curl -sSL https://get.docker.com/ | sh
复制代码
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
复制代码
sudo gitlab-runner register
复制代码
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
# 没有域名因此填的是IP
http://xx.xx.xxx.xx:8888
复制代码
这里的token分为两种 一种是 Shared Runner ,该 Runner 全部项目均可以使用
位置:顶部设置图标🔧 -> 左侧栏Overview -> Runner shell
另外一种是 Specific Runner ,该 Runner 指定具体某个项目才可以使用
位置:进入某个项目 -> 左侧栏Setting -> CI/CD -> 在内容区域找到Runners一项,点击展开
Please enter the gitlab-ci token for this runner
# 这里咱们使用 Shared Runner Token
xxxxxxx
复制代码
Please enter the gitlab-ci description for this runner
test-gitlab-runner-description
复制代码
Please enter the gitlab-ci tags for this runner (comma separated)
my-tag
复制代码
这里我使用 shell。ruby
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
复制代码
若是一切正常的话咱们会看到bash
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 复制代码
Please enter the Docker image (eg. ruby:2.1):
alpine:latest
复制代码
咱们回到Share Runners 就能够看到咱们添加的 runner 了
# 定义 stages(阶段,会依次执行)
stages:
- install_deps
- build_prod
- deploy_prod
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- node_modules/
- dist
# 安装构建依赖
install_deps_job:
stage: install_deps
# 在哪一个分支才会执行脚本
only:
# - dev
# - release
- master
script:
- echo '模拟安装构建依赖阶段'
tags:
- my-tag
# 构建预prod环境src目录下应用
build_prod_job:
stage: build_prod
only:
- master
script:
- echo '构建预prod环境src目录下应用阶段'
tags:
- my-tag
# 部署生产环境
deploy_prod_job:
stage: deploy_prod
only:
- master
script:
- echo '部署生产环境阶段'
tags:
- my-tag
复制代码
而后你可能会看到报错
Running with gitlab-runner 11.9.2 (fa86510e)
on desc Z1UPKJjn
Using Shell executor...
Running on iZwz98jvb8bcz40ko474qsZ...
bash: line 68: git: command not found
bash: line 66: cd: /home/gitlab-runner/builds/Z1UPKJjn/0/main-group/main-project: No such file or directory
ERROR: Job failed: exit status 1
复制代码
报错的缘由是个人服务器是一台只安装了 Gitlab-runner 的服务器,根据报错提示,须要 git 来拉取 Gitlab 服务器上的代码,因此咱们安装 git:
yum -y install git
复制代码
而后使用
git --version 查看 git 是否安装成功
复制代码
以后从新执行pipline或提交代码,能够看到一切运行正常:
This build is stuck, because the project doesn't have any runners online assigned to it. Go to Runners page
,这是由于未找到对应的 runner,缘由一:多是gitlab-runner注册失败,缘由二:多是.gitlab-ci.yml配置文件里面 tags 没有匹配到已注册可用的 runner,在 stage 中加入对应 runner 注册时输入的 tags 便可。Install GitLab Runner manually on GNU/Linux Registering Runners GitLab Runner commands GitLab CI/CD Pipeline Configuration Reference Docker搭建本身的Gitlab CI Runner