部署GitLab-CI
2018-04-17 12:14
211 查看
部署GitLab-CI
简介
GitLab_CI(gitlab continuous integration)是Gitlab提供的持续集成服务。主要功能是每一次push到gitlab的时候,触发一次脚本执行,脚本内容包括编译、测试、部署等自定义内容。持续集成在自动化工作流程中扮演着十分重要的角色,GitLab内置持续集成,持续部署,持续交付以支持编译,测试和部署应用程序
本文主要利用GitLab-CI实现自动部署
原理
自动部署涉及了若干个角色,主要介绍如下:GitLab-CI
GitLab自带的持续集成系统,你装的GitLab的那台服务器上就有,无需自行安装。GitLab-CI负责解析.gitlab-ci.yml
.gitlab-ci.yml
GitLab-CI使用YAML文件来管理项目配置,在git项目的根目录下创建此文件,文件中包含一系列的阶段和执行规则。GitLab-CI在push后解析它,根据里面的内容调用runner来执行。YAML配置语法
GitLab-Runner
这个是脚本执行的承载者, .gitlab-ci.yml的script部分就是由runner来负责的。GitLab-CI解析项目里的.gitlab-ci.yml文件之后,根据里面的规则,分配到各个Runner来运行相应的脚本script。
步骤
安装GitLab-CIGitLab自带,无需单独安装
安装GitLab-Runner
在centOS系统安装gitlab-ci-multi-runner,在其它系统安装runner,请参考安装GitLab Runner
添加GitLab官方仓库 # For RHEL/CentOS/Fedora curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash 安装最新版本的GitLab Runner # For RHEL/CentOS/Fedora sudo yum install gitlab-ci-multi-runner
向GitLab-CI注册Runner,将Runner与git项目绑定起来
先介绍下如何获取项目token,因为注册Runner时会用到,git项目–settings–CI/CD–General pipelines settings–Runner token,如图
注册Runner
sudo gitlab-ci-multi-runner register Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://git.intra.weibo.com Please enter the gitlab-ci token for this runner: ***(获取git项目token值) Please enter the gitlab-ci description for this runner: Please enter the gitlab-ci tags for this runner (comma separated): tag名字(很重要,在.gitlab-ci.yml文件里面指定tag,匹配使用哪个tag的runner) Whether to run untagged builds [true/false]: Whether to lock the Runner to current project [true/false]: Registering runner... succeeded runner=m15ahKPr Please enter the executor: docker-ssh, shell, ssh, virtualbox, kubernetes, docker, parallels, docker+machine, docker-ssh+machine: shell(执行脚本的方式) Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
在git项目中查看或编辑runner状态:Settiongs-CI/CD-Runner settings
编写.gitlab-ci.yml
git项目根目录下编写.gitlab-ci.yml
stages: -deploy deploy: stage: deploy script: - deploy Dorylus_QA CITest only: - master tags: - runner26
这里我们只有一个stage:deploy,only指定了只有在master分支push的时候才会被执行,tags是runner26,此tag对应刚才注册runner时候的tags
最重要的script部分:deploy Dorylus_QA CITest 这里是一条shell命令,deploy是runner服务器上编写的一个脚本,传入参数是项目组名和项目名。这样这条指令就能在/***/Dorylus_QA/CITest目录下自动部署
编写deploy脚本
#!/bin/bash if [ $# -ne 2 ] then echo "arguments error" exit 1 else deploy_path="/data0/gitlab-runner/$1/$2" if [ ! -d "$deploy_path" ] then project_path="git@git.intra.weibo.com:"$1/$2".git" git clone $project_path $deploy_path else cd $deploy_path git pull fi
脚本大意:如果目录不存在就git clone一个,如果存在就git pull,这样就达到了自动部署的目的。
对deploy加上执行权限
chmod 755 deploy
并把depoy所在目录加到PATH中,将
PATH="/data0/gitlab-runner/bin:$PATH"加入到/etc/profile中。
配置ssh登录
上面deploy脚本是用ssh方式和gitlab联系的,首先在Runner机器上创建gitlab-runner用户
useradd gitlab-runner, 需要将gitlab-runner公钥添加到自己账号的user_profile里面。
同时给gitlab-runner加上sudo权限,编辑
vim /etc/sudoers,添加
gitlab-runner ALL=(ALL) NOPASSWD: ALL
Runner所在机器生成公钥的方法
$ mkdir ~/.ssh $ cd ~/.ssh $ ssh-keygen # 提示输入一直按回车默认就可以了 $ cat id_rsa.pub
为避免权限问题,将deploy_path的所属权交给gitlab-runner用户
chown -R gitlab-runner:gitlab-runner /data0/gitlab-runner/
执行自动部署脚本
先在runner机器上手动执行下deploy 参数1 参数2,如果执行成功,脚本没有问题
push代码到git项目,自动执行部署脚本
GitLab–>CI/CD–>Piplines–>Run Pipeline即可执行自动部署脚本
相关文章推荐
- 【补充】Gitlab 部署 CI 持续集成
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1)
- gitlab之gitlab-ci自动部署
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1)
- asp.net core结合Gitlab-CI实现自动化部署
- CI持续集成系统环境---部署Gitlab环境完整记录
- 劈荆斩棘:Gitlab 部署 CI 持续集成
- gitlab-ci实现前端自动化部署(步骤全面)
- 利用docker搭建gitlab;实现git-ci 集成自动化部署
- Gitlab部署CI并使用.gitlab-ci.yml配置您的作业(中文翻译)
- CI持续集成系统环境---部署Gitlab环境完整记录
- Gitlab CI 自动部署 asp.net core web api 到Docker容器
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)
- [原创]CI持续集成系统环境---部署Gitlab环境完整记录
- gitlab CI
- [原创]CI持续集成系统环境---部署Jenkins完整记录
- 基于Jenkins+Gitlab的自动化部署实战
- centos 7 部署 汉化版 gitlab
- GitLab部署