天天看点

Gitlab安装和使用gitlab-runner(一)

一、

Gitlab安装和使用gitlab-runner(一)
本服务器1核2G,不够用 我先加 swap空间

[root@sannian /]# cd /mnt/swap/
[root@sannian swap]# dd if=/dev/zero of=swapfile bs=1M count=9999 
[root@sannian swap]# mkswap swapfile
[root@sannian swap]# swapon swapfile
           

添加开机自动挂

[root@sannian swap]# vim /etc/fstab 
/mnt/swap/swapfile swap swap defaults 0 0
           
[root@sannian swap]# top -c
查看           
Gitlab安装和使用gitlab-runner(一)

设置让系统积极使用swap空间

[root@sannian swap]# echo 100 > /proc/sys/vm/swappiness
[root@sannian swap]# vim /etc/sysctl.conf
加入
vm.swappiness=100
           

sysctl -p 或者重启生效

[root@sannian swap]# sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
vm.swappiness = 100
           

二、下载安装gitlab

配置yum

[root@sannian swap]# 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
           

更新

[root@sannian swap]# yum makecache
           

安装GitLab社区版

[root@sannian swap]# yum install gitlab-ce      #自动安装最新版
[root@sannian swap]# yum install gitlab-ce-x.x.x    #安装指定版本
           

GitLab命令

gitlab-ctl start # 启动所有 gitlab 组件;

gitlab-ctl stop # 停止所有 gitlab 组件;

gitlab-ctl restart # 重启所有 gitlab 组件;

gitlab-ctl status # 查看服务状态;

gitlab-ctl reconfigure # 启动服务;

vim /etc/gitlab/gitlab.rb # 修改默认的配置文件;

/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml# 修改默认的配置文件;

gitlab-rake gitlab:check SANITIZE=true --trace # 检查gitlab;

gitlab-ctl tail # 查看日志;

运行并启动:

[root@sannian swap]# gitlab-ctl  start
[root@sannian swap]# gitlab-ctl reconfigure//初始化设置
           

打开 url 47.99.145.156

Gitlab安装和使用gitlab-runner(一)

更改端口为9999

[root@sannian embedded]# cd /var/opt/gitlab/nginx/conf/
[root@sannian conf]# vim gitlab-http.conf 
[root@sannian conf]# gitlab-ctl  restart
           

打开url

47.99.145.156:9999

修改地址

[email protected]:front/front-erp.git
[root@sannian conf]# vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
[root@sannian conf]# gitlab-ctl restart
[root@sannian opt]# yum -y install git
           
[root@sannian opt]# git config --global user.name "root"
[root@sannian opt]# git config --global user.email "[email protected]"
[root@sannian opt]# ssh-keygen -t rsa -C [email protected]
           

复制密钥到gitlab上

[root@sannian front-erp]# vim /root/.ssh/id_rsa.pub 
[root@sannian opt]# cd front-erp/
[root@sannian front-erp]# git init
Initialized empty Git repository in /opt/front-erp/.git/
[root@sannian front-erp]# git remote add origin [email protected]:front/front-erp.git
[root@sannian front-erp]# git add .
[root@sannian front-erp]# git commit -m "commit"
[root@sannian front-erp]# git push -u origin master           
Gitlab安装和使用gitlab-runner(一)

三、安装gitlab-runner

安装docker

[root@sannian front-erp]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
           
配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户
           

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'

{

"registry-mirrors": ["https://v2ltjwbg.mirror.aliyuncs.com"]

}

EOF

[root@sannian front-erp]# systemctl daemon-reload
[root@sannian front-erp]# systemctl restart docker
[root@sannian front-erp]# docker search gitlab-runner
           

下载gitlab-runner

[root@sannian front-erp]# docker pull gitlab/gitlab-runner
           

运行gitlab-runner

[root@sannian front-erp]# docker images
[root@sannian front-erp]# docker run -d -v /data-ci:/data-ci -v /home:/home --name gitlab-runner gitlab/gitlab-runner 
246408751beaf0baaf0d0231bbdea8d5ec5f3c78f32e723a9f6d2312e8802a74
[root@sannian front-erp]# docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS               NAMES
246408751bea        gitlab/gitlab-runner   "/usr/bin/dumb-init …"   33 seconds ago      Up 31 seconds                           gitlab-runner
           
Gitlab安装和使用gitlab-runner(一)

注册 runner

[root@sannian front-erp]# docker exec -it 246408751bea gitlab-runner --help//帮助
           
[root@sannian front-erp]# docker exec -it 246408751bea gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=20 revision=cf91d5e1 version=11.4.2
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://47.99.145.156:9999/
Please enter the gitlab-ci token for this runner:
XFomoe1rfzCPx7e6Ng_y
Please enter the gitlab-ci description for this runner:
[246408751bea]: front-erp-ci
Please enter the gitlab-ci tags for this runner (comma separated):
front-erp-ci
Registering runner... succeeded                     runner=XFomoe1r
Please enter the executor: shell, docker+machine, docker-ssh+machine, kubernetes, docker-ssh, parallels, ssh, virtualbox, docker:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
           

然后查看:

Gitlab安装和使用gitlab-runner(一)

四、配置 .gitlab-ci.yml

stages:
  - test
  - deploy

构建开发环境:
  stage: test
  script:
    - /data-ci/front-erp.sh
  only:
    - develop
           

vim front-erp.sh

#!/bin/sh


#env

        if [[ $CI_BUILD_REF_SLUG == 'develop' ]]; then

                hostlist=""
                if [[ $CI_JOB_STAGE == 'test' ]]; then
                        hostlist="47.99.89.247"
                fi
                if [[ $CI_JOB_STAGE == 'master' ]]; then
                        hostlist="47.99.89.247"
                fi

                projectdir="/work/front/${CI_PROJECT_NAME}"
                for i in $hostlist
                do
                        echo
                        echo 
                        echo "-----------------------------------------------------------------------"
                        echo "发布 主机: $i  项目: $CI_PROJECT_NAME "
                        cd $CI_PROJECT_DIR
                        yarn
                        if [ $? = 0 ];then
                             npm run build
                             echo scp -r $CI_PROJECT_DIR/dist/\* root@${i}:${projectdir}/
                             rsync -avztH --delete $CI_PROJECT_DIR/dist/ root@${i}:${projectdir}/
                        else
                               echo "mvn 构建失败...."
                               exit 100
                        fi

                        echo
                        echo
                done
fi
           

继续阅读