1.在电脑上配置gitlab-runner
参考文档 https://docs.gitlab.com/runner/install/osx.html
安装:
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
添加权限
sudo chmod +x /usr/local/bin/gitlab-runner
启动
cd ~
gitlab-runner install
gitlab-runner start
2.gitlab-runner和代码库关联,也就是注册阶段
参考文档 https://docs.gitlab.com/runner/register/index.html
sudo gitlab-runner register
注意:以下两条地址和token输入这个:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
Please enter the gitlab-ci token for this runner
后面这些可以自行输入,比如我输入的是plus-h5-static-release-dev
Please enter the gitlab-ci description for this runner
[hostname] plus-h5-static-release-dev
tag我输入的是dev
Please enter the gitlab-ci tags for this runner (comma separated):
dev
这里我选的是shell脚本
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
配置完成之后,在git上查看,如果出现以下runner说明成功
然后在项目根目录下配置.gitlab-ci.yml
例子如下:其中,/Users/zhaileilei/code/server-code/是我自定义的目录,可修改为自己想要存放的路径
before_script:
- echo "this is before script"
stages:
- build
job:
stage: build
script:
- echo "building"
- path=`pwd`
- echo $path
tags:
- dev
after_script:
- echo "this is after script"
至此,可以在代码库提交代码,查看pipelines查看是否成功
可点击statges下面的成功或失败按钮查看具体命令执行情况或者报错信息
至此,提交到服务器配置完成。
升级配置,注意,此处我配置了只有dev-0522,dev,test分支生效,master不生效
before_script:
- echo "this is before script"
# 开始执行命令前清空预发服务器要访问的文件夹下的内容,没有文件就新建一个
- cd /Users/zhaileilei/code/server-code/ && /bin/rm -rf *
stages:
- build
job:
stage: build
only:
# 只有这些分支的代码才会自动部署
- dev-0522
- dev
- test
except:
- master
script:
- echo "building"
- path=`pwd`
- echo $path
- cp -rf $path/* /Users/zhaileilei/code/server-code/
tags:
- dev
after_script:
- echo $path
# 执行命令后清空默认读取位置的文件内容
- cd $path && /bin/rm -rf *