[TOC]
前言:上篇文章讲解了如何安装一个本地runner,然后用本地runner发布本地包到Linux,但这会有一个问题,在本地runner用scp向Linux发送文件,会造成服务器上的文件越来越大,这是因为scp采用到是递归复制文件到服务器,并不能实现替换或者删除。
我的解决办法是直接在在服务器上安装一个runner,而且不用担心本地runner的宿主机器关闭,任务就执行不了了
1、登录你的服务器,下载最新runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
2、安装GitLab Runner
sudo yum install gitlab-runner
3、绑定gitlab、注册runner
后面的就和上篇文章写到第二步2.2 注册runner一样了
到这里完成之后,就可以去配置我们前端所需要到环境了(git,node....)
ps:
1.当我们注册完runner后,会自动帮我们生成一个runner用户
2.我们runner在执行时,所需要的环境都需要你切换到gitlab-runner用户下去进行安装,如果只在root下装了 环境,就会有下图:
su gitlab-runner //切换用户
如果你第一次切换过来可能会提示你需要输入gitlab-runner的用户密码,如果不知道,就直接重置吧:
sudo passwd gitlab-runner //会提示你输入两次密码
网上有很多 linux下安装node的教程,我这里就偷个懒~~
下载完之后
node -v //查看node版本
然后你需要配置一下全局路径
vi /etc/profile
添加路径
export NODE_HOME=/home/gitlab-runner/node-v10.9.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
如图
保存(
在英文状态下
1.esc退出编辑状态,2.’shift+;‘ ,3.’wq‘保存退出)然后source一下,让配置生效
source /etc/profile
4.将gitlab-runner用户下打好的包复制到root用户下前端需要访问到目录
此时gitlab-runner是普通用户,需要你给gitlab-runner设置一个root权限
sudo vi /etc/sudoers
添加上gitlab-runner ALL=(ALL) NOPASSWD: ALL
保存(
在英文状态下
1.esc退出编辑状态,2.’shift+;‘ ,3.’wq!‘保存退出!代表强制退出
)
5、修改前端.sh
6.完成
这里涉及一些linux基本操作,可能没有详细解释,遇到linux提示相关问题,就去熟悉下Linux吧
最后提醒前端伙伴,window和mac没有区分路径的大小写,Linux上是区分的,所以在文件明明的时候,最好都保持一致
踩的一些坑
一、有些时候你可能会遇到在自己linux明明已经安装好的node,yarn的环境,也已经 node -v
可以查到版本了,结果下次查不到了
解决方案:切换到root用户,退出账户,然后重新登录,造成到原因个人猜测是linux没有在切换用户su 用户名称
的时候,配置没有加载完全,纯属瞎猜,哈哈哈~~
二、yarn install 遇到的网络错误There appears to be trouble with your network connection. Retrying...
[gitlab-runner@localhost zhzg-sc-analysis-mobile]$ yarn install
yarn install v1.16.0
warning npm-shrinkwrap.json found. This will not be updated or respected. See https://yarnpkg.com/en/docs/migrating-from-npm for more information.
[1/4] Resolving packages...
[2/4] Fetching packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error An unexpected error occurred: "https://registry.npm.taobao.org/antd-mobile/download/antd-mobile-2.2.14.tgz: ETIMEDOUT".
info If you think this is a bug, please open a bug report with the information provided in "/home/gitlab-runner/builds/zV8CSC9T/0/develope/zhzg/sc/zhzg_yy_fxxt/code/web/zhzg-sc-analysis-mobile/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
查看了https://github.com/yarnpkg/yarn/issues/5865
删除yarn.lock就可以了
最后的一些说明:
一台机器上可以安装多个runner(不论是指定的runner
,还是共享runner
),安装好之后你可以登录上你机器去查看你所安装runner的位置
1、切换到gitlab-runner 用户sudo su gitlab-runner
2、切换到/home/gitlab-runner/builds
目录下,你就可以看到你安装的runner了,如图我安装了两个(一个指定runner,一个共享runner)
3、如果你想调试是否能在ci跑成功,不用每次去修改然后提交ci,直接进入到你runner的目录下去执行你的测试操作就行,如我的:
cd /home/gitlab-runner/builds/zV8CSC9T/0/develope/zhzg/sc/zhzg_yy_fxxt/code/web/zhzg-sc-analysis/
在此目录下你就可以执行一些你的命令,如
npm install
,yarn -v
等等