GitLab部署自动化集成GitLab-CI
安装&注册
- GitLab-CI需要安装Gitlab-runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash ;
yum install gitlab-runner ;
配置Runner
默认情况,Runner是通过gitlab-runner的这个用户来执行一系列操作,其工作目录也是在gitlab-runner的用户目录下面。如果使用默认gitlab-runner用户操作一些文件时经常会遇到权限问题,就需要给gitlab-runner赋权。我们通过以下方式修改
useradd gitlab-ci -s /sbin/nologin -M ; ##创建gitlab-ci用户
gitlab-runner uninstall ; ##删除默认服务
gitlab-runner install --working-directory /data/builds --user gitlab-ci; ##重新注册服务
gitlab-runner restart;
注册Runner
先打开GitLab上需要自动部署的项目界面,找到该项目的Settings –> CI/CD –> Runners settings 在gitlab上可以看到自己的token信息,用来注册runner(也可以前往系统设置注册,再分配)
项目
——设置
——CI/CD
——runner
gitlab-runner register ## 开始注册
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): ##输入URL
http://192.168.1.251/
Please enter the gitlab-ci token for this runner: ##项目对应的token
SURPSbXCMek8kkx7AWE6
Please enter the gitlab-ci description for this runner: ##runner说明
[localhost.localdomain]: oms_runner
Please enter the gitlab-ci tags for this runner (comma separated): ##标签与yml中对应
web
Registering runner... succeeded runner=SURPSbXC
Please enter the executor: docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine, kubernetes, custom, docker, shell, ssh: ##运行方式
shell
注册完成后会在网页处显示
- 在对应的仓库中添加
.gitlab-ci.yml
文件
oms_dev:
stage: build
script:
- /usr/local/sbin/sh/git_dev.sh
environment:
name: oms_test
url: http://test.ashsh.com.cn
only:
- dev
tags:
- test
oms_test:
stage: build
script:
- /usr/local/sbin/sh/git_test.sh
environment:
name: oms_test
url: http://test.ashsh.com.cn
only:
- test
tags:
- test
oms_master:
stage: build
script:
- /usr/local/sbin/sh/git_master.sh
environment:
name: oms_master
url: http://master.ashsh.com.cn
only:
- master
tags:
- test
提交yml文件后,所有的push操作都会触发改文件定义的命令,脚本。