目标:解脱Jenkins部署;如果前端有本人的服务器环境;能够借助gitlab-runner
实现CI
主动部署并且发送后果告诉音讯
1. 服务器装置gitlab-runner
先确认服务器环境信息之后找到对应的gitlab-runner
安装包
- 应用
uname -a
查看服务器版本信息 - 在此处找到合乎本人版本信息的安装包
应用安装包装置
gitlab-runner
程序;如果提醒sudo: curl:找不到命令
, 则应用yum
装置curl后从新安装程序yum update -y && yum install curl -y
,(因为装置curl之前会先进行yum的update所以这个工夫挺长的)sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
给定执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
创立一个用户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
装置运行服务
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runnersudo gitlab-runner start
如果这一步也提醒
sudo: gitlab-runner:找不到命令
;须要# 批改/etc/sudoers文件;找到这一行Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin # 将要执行的命令所在的目录增加到前面,即可Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
从新执行装置运行服务就能够持续了
- 验证装置信息
gitlab-runner -v
2. 配置注册Runner
- 在本人gitlab仓库找到对应的信息 > Settings -> CI/CD -> Runners -> Specific Runners
- 在服务器下执行注册命令
sudo gitlab-runner register
- 查看正在激活的Runner
gitlab-runner verify
- 也能够在
CI
处看到曾经被激活
3. 编写yml
配置文件
在本人我的项目的根门路下编写.gitlab-ci.yml
脚本文件
编写合乎本人的程序
stages: # 分阶段 - install - build - deploy - notifycache: # 缓存内容 paths: - node_modules/ - dist/install-job: tags: # 触发runner(这个就是注册的时候填写的tag) - master only: # 哪些分支触发 - master stage: install # 以后阶段 script: - echo '装置依赖阶段....' - npm installbuild-job: tags: - master only: - master stage: build script: - npm run build:devdeploy-job: tags: - master only: - master stage: deploy script: - whoami - sudo scp -r ./dist/* /www/html/kangshifu/kangshifu-vue/mars/dist# 构建胜利时的告诉音讯notifySuccessWeChat: stage: notify script: - echo $title $sucContent - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康师傅_前端_智云零碎-部署后果(ci)\",\"content\":\"胜利\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}" only: - master tags: - master when: on_success # 构建失败时的告诉音讯notifyFailWeChat: stage: notify script: - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康师傅_前端_智云零碎-部署后果(ci)\",\"content\":\"失败\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}" only: - master tags: - master when: on_failure
此脚本中蕴含了部署后果的告诉;具体配置形式能够查看对应的文档进行配置,也能够应用钉钉音讯
或者企业微信
音讯等形式
此处可能遇到的问题
git版本问题
报错:Getting source from Git repository00:01Fetching changes with git depth set to 50...Reinitialized existing Git repository in /home/gitlab-runner/builds/ErNGnaLr/0/ivm/mars/.git/fatal: git fetch-pack: expected shallow listfatal: The remote end hung up unexpectedlyCleaning up project directory and file based variables00:00ERROR: Job failed: exit status 1起因:centos7 根底仓库,提供的 git 版本只有到 1.8.3,沒方法应用 git 2 的一些新性能解决办法:装置最新版本的git就能够了
拷贝文件问题
因为没有应用sshpass
的形式操作文件;以后应用的scp
进行文件的操作;可能会提醒报错: We trust you have received the usual lecture from the local SystemAdministrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility.sudo: no tty present and no askpass program specifiedCleaning up project directory and file based variables00:00ERROR: Job failed: exit status 1起因:因为尝试应用sudo运行命令引起的 ,然而调用用户没有被受权应用 sudo解决:关上 sudoers 文件`vim /etc/sudoers` ;将以下内容增加到文件底部gitlab-runner ALL=(ALL) NOPASSWD: ALL
至此gitlab-runner
曾经能够胜利的运行了「手动撒花」