1.安装Gitlab

强烈建议安装Omnibus软件包,因为它安装更快,更易于升级,并且包含增强其他方法所没有的可靠性的功能。还强烈建议至少使用4GB的可用内存来运行GitLab。

  • Centos 7 安装教程
  1. 安装依赖

在CentOS 7(和RedHat / Oracle / Scientific Linux 7)上,以下命令还将在系统防火墙中打开HTTP和SSH访问。

sudo yum install -y curl policycoreutils-python openssh-serversudo systemctl enable sshdsudo systemctl start sshdsudo firewall-cmd --permanent --add-service=httpsudo systemctl reload firewalld

接下来,安装Postfix以发送通知电子邮件。如果要使用其他解决方案发送电子邮件,请跳过此步骤并在安装GitLab后配置外部SMTP服务器。

sudo yum install postfixsudo systemctl enable postfixsudo systemctl start postfix

在Postfix安装期间,可能会出现配置屏幕。选择“Internet Site”并按Enter键。使用服务器的外部DNS作为“邮件名称”,然后按Enter键。如果出现其他屏幕,请继续按Enter键接受默认值。

2.添加GitLab软件包存储库并安装软件包

添加GitLab包存储库。

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash

接下来,安装GitLab包。更改https://gitlab.example.com为您要访问GitLab实例的URL。安装将自动配置并启动该URL的GitLab。

对于https://URL,GitLab将自动使用Let's Encrypt请求证书,该证书需要入站HTTP访问和有效的主机名。您也可以使用自己的证书或只使用http://。

sudo EXTERNAL_URL ="https://gitlab.example.com" yum install -y gitlab-ee

3.浏览到主机名并登录

在您第一次访问时,您将被重定向到密码重置屏幕。提供初始管理员帐户的密码,您将被重定向回登录屏幕。使用默认帐户的用户名root登录。

4.修改gitlab仓库位置

停止gitlab服务

[root@VM_0_5_centos git-data]# gitlab-ctl stop

修改配置文件/etc/gitlab/gitlab.rb,将git_data_dirs设置成自己的数据盘目录,如下:

git_data_dirs({ "default" => { "path" => "/data/gitlab/git-data" }})

如果是空库直接reconfigure即可,命令如下:

[root@VM_0_5_centos git-data]# gitlab-ctl reconfigure

如果gitlab已经有版本库了,需要将原有库迁移到新位置,命令如下:

[root@VM_0_5_centos git-data]# rsync -av /var/opt/gitlab/git-data/repositories /data/gitlab/git-data/ [root@VM_0_5_centos git-data]# gitlab-ctl reconfigure[root@VM_0_5_centos git-data]# gitlab-ctl start

其他操作系统安装教程可以参考官方安装教程: https://about.gitlab.com/install。

2.安装Gitlab-runner

gitlab-runner需要安装在待部署服务器,用于运行.gitlab-ci.yml中的配置。

  • Centos安装教程
  1. 添加GitLab的官方存储库
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

yum list gitlab-runner --showduplicates | sort -rsudo yum install gitlab-runner-10.0.0-1

完成上述步骤后,Runner已经安装并运行。

3.注册Gitlab-runner

  • 找到gitlab库register-url和token,如下图:



开源gitlab 禅道_git


在gitlab-runner服务器运行如下命令,填入register-url和token等信息即可完成注册。

  • 运行以下命令:
sudo gitlab-runner register
  • 输入您的GitLab实例URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) https://gitlab.com
  • 输入您获得的令牌以注册Runner:
Please enter the gitlab-ci token for this runner xxx
  • 输入Runner的tag,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci description for this runner [hostname] my-runner
  • 输入与Runner关联的标签,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci tags for this runner (comma separated): my-tag,another-tag
  • 输入Runner执行程序:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell: docker
  • 如果您选择Docker作为执行程序,则会要求您将默认镜像用于未在其中定义的项目.gitlab-ci.yml:
Please enter the Docker image (eg. ruby:2.1): alpine:latest

4.定义.gitlab-ci.yml

stages: - before - deploy - after#定义变量 variables: TYPE : "auto" MERCHANT : "toutiao"#输出变量信息echo: stage: before tags: - rest only: - master script: - echo $TYPE - echo $MERCHANT - echo $CI_PROJECT_DIR#rsync同步代码copy: stage: deploy tags: - rest only: - master script: - rsync -rapu $CI_PROJECT_DIR /data #修改目录拥有者chown: stage: after tags: - rest only: - master script: - chown -R www:www /data #添加部署日志 log: stage: after tags: - rest only: - master script: - echo "$(date "+%Y-%m-%d %H:%M:%S") [ci] build_dir:$CI_PROJECT_DIR merchant:$MERCHANT" >> /data/build.log

以上配置,只要当master分支发生merge或者commit等操作时就会自动发起部署流程,当然gitlab也提供了手动部署的功能,如下图:


开源gitlab 禅道_ci_02


手动部署的时候我们还可以对变量进行重新赋值,如下图:


开源gitlab 禅道_git_03


另外,Gitlab也提供了强大的部署统计视图,如下图:


开源gitlab 禅道_ci_04


以上只是一个部署php项目的一个案例,当然这里也可以集成phpunit单元测试,这里的每个stage的执行是按照顺序执行的,如果前一步执行失败,后面的stage会停止执行。