安装 gitlab CI 服务.md

安装 gitlab-ce

安装依赖包

yum install curl policycoreutils openssh-server openssh-clients
systemctl enable sshd
systemctl start sshd
yum install postfix
systemctl enable postfix
systemctl start postfix
firewall-cmd --permanent --add-service=http
systemctl reload firewalld

下载 gitlab-ce 镜像

vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
yum makecache
yum install gitlab-ce

配置

vi /etc/gitlab/gitlab.rb

external_url 'http://ip'

启动gitlab

gitlab-ctl reconfigure

遇到报错

Error executing action `run` on resource 'execute[semodule -i /opt/gitlab/embedded/selinux/rhel/7/gitlab-7.2.0-ssh-keygen.pp]
  1. stop gitlab
  2. yum install libsemanage-static libsemanage-devel

安装CI服务

下载安装CI

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/gitlab-runner-10.5.0-1.x86_64.rpm
rpm -ivh gitlab-runner-10.5.0-1.x86_64.rpm
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
gitlab-runner uninstall
gitlab-runner install --working-directory /home/gitlab-runner/ --user gitlab-runner
gitlab-runner restart
gitlab-runner status

注册 runner

sudo gitlab-ci-multi-runner register
-- 输入Gitlab CI地址, (e.g. https://gitlab.xunlei.cn/ci)
http://192.168.1.249/gitlab/
-- 输入项目CI token
fuXXXXXXXXXX
-- 输入 Runner 描述(e.g. home.xl9.xunlei.com 测试runner)
测试Runner
-- 输入 Runner 标签,可以多个,用逗号隔开(e.g. 10.10.34.91-dev)
251-dev
-- 输入 Runner 执行的语言 (e.g. shell)
shell
-- 认证是否注册成功
sudo gitlab-ci-multi-runner verify

编写脚本

在项目下面创建.gitlab-ci.yml

stages:
  - deploy

deploy_dev:
  stage: deploy
  tags:
    - 251-dev
  only:
    - develop
  script:
    - deploy_stage.sh

然后在部署服务器上的 gitlab-runner 用户路径下创建deploy_stage.sh

echo "delete old xyj-admin-api"
sudo rm -rf /raid1/www/test
sudo git clone [email protected]:test/xyj-test-api.git /raid1/www/test
sudo git fetch --all
sudo git reset --hard origin/test
echo "=============重启服务===================="
source /etc/profile
sudo supervisorctl restart test
time=`date`
echo "部署结束: $time."
echo "================================================"

其中 测试项目是被 supervisorctl 管理的

要确保几件事

  1. 改服务器能够正常从远端克隆数据
  2. 命令几点要带上sudo
  3. 用户sudo无密码及在tty下可以使用需要参考 Linux sudo 相关操作
  4. 如果克隆下来的文件放在挂载盘,需要在 ~/ 路径下执行 clone 然后放到对应路径,否则报错 fatal: could not create work tree dir

遇到无法克隆,且 gitlab 日志为

gitlab JWT::DecodeError (Nil JSON web token) 

修改 nginx 配置

upstream gitlab-workhorse {
      server unix:/var/opt/gitlab/gitlab-workhorse/socket;
 }

...
proxy_pass http://gitlab-workhorse;
...

git push develop 后触发脚本

你可能感兴趣的:(ci,持续集成,git)