Gitlab集成Gitlab_runner

环境准备

  • 创建工作目录 /usr/local/docker/runner
  • 创建构建目录 /usr/local/docker/runner/environment
  • 下载 jdk-8u152-linux-x64.tar.gz 并复制到 /usr/local/docker/runner/environment
  • 下载 apache-maven-3.5.3-bin.tar.gz 并复制到 /usr/local/docker/runner/environment

编写daemon.json

/usr/local/docker/runner/environment 目录下创建 daemon.json,用于配置加速器和仓库地址

root@ubuntu2:/usr/local/docker/runner/enviroment# vim daemon.json 

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "192.168.100.11:5000"
  ]
}
root@ubuntu2:~# docker pull gitlab/gitlab-runner:v11.1.1

编写Dockerfile

/usr/local/docker/runner/environment 目录下创建 Dockerfile

root@ubuntu2:/usr/local/docker/runner/enviroment# vim Dockerfile 

FROM gitlab/gitlab-runner
MAINTAINER andre runner

# 修改软件源
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' >> /etc/apt/sources.list && \
    apt-get update -y && \
    apt-get clean

# 安装 Docker
RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \
    apt-get update && apt-get install -y gnupg2 && \
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" && \
    apt-get update -y && \
    apt-get install -y docker-ce
COPY daemon.json /etc/docker/daemon.json


# 安装 Docker Compose
WORKDIR /usr/local/bin
RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose
RUN chmod +x docker-compose

# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk-11.0.7_linux-x64_bin.tar.gz /usr/local/java
RUN tar -zxvf jdk-11.0.7_linux-x64_bin.tar.gz && \
    rm -fr jdk-11.0.7_linux-x64_bin.tar.gz

# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
# RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz
COPY apache-maven-3.6.3-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.6.3-bin.tar.gz && \
    rm -fr apache-maven-3.6.3-bin.tar.gz
# COPY settings.xml /usr/local/maven/apache-maven-3.5.3/conf/settings.xml

# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk-11.0.7
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.6.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

WORKDIR /

编写docker-compose.yml

在 `/usr/local/docker/runner下创建docker-compose.yml

root@ubuntu2:/usr/local/docker/runner# vim docker-compose.yml 

version: '3.7'
services:
  gitlab-runner:
    build: enviroment
    restart: always
    container_name: gitlab-runner
    privileged: true
    volumes:
      - /usr/local/docker/runner/config:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock

root@ubuntu2:/usr/local/docker/runner# pwd
/usr/local/docker/runner

启动docker-compose

root@ubuntu2:/usr/local/docker/runner# docker-compose up -d

注册 Runner

root@ubuntu2:/usr/local/docker/runner# docker exec -it gitlab-runner gitlab-runner register
 
# 输入 GitLab 地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.10.132/
 
# 输入 GitLab Token
Please enter the gitlab-ci token for this runner:
1Lxq_f1NRfCfeNbE5WRh
 
# 输入 Runner 的说明
Please enter the gitlab-ci description for this runner:
可以为空
 
# 设置 Tag,可以用于指定在构建规定的 tag 时触发 ci
Please enter the gitlab-ci tags for this runner (comma separated):
deploy
 
# 这里选择 true ,可以用于代码上传后直接执行
Whether to run untagged builds [true/false]:
true
 
# 这里选择 false,可以直接回车,默认为 false
Whether to lock Runner to current project [true/false]:
false
 
# 选择 runner 执行器,这里我们选择的是 shell
Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh:
shell

查看config目录下的config.toml ,每次集成一个 手动设置specific Runner就会在文件中新建一串记录

root@ubuntu2:/usr/local/docker/runner/config# vim config.toml 

concurrent = 1
check_interval = 0

[[runners]]
  name = "e06085adcec1"
  url = "http://192.168.100.6/"
  token = "fe25917c9c34a45b87fa456cd269b8"
  executor = "shell"
  [runners.cache]

编写.gitlab-ci.yml文件

先测试一下

stages:
  - test

test:
  stage: test
  script:
    - echo "hello gitlab-runner1"
    - echo "hello gitlab-runner2"

正式编写

stages:
  - package
  - push
  - deploy
  - clean

package:
  stage: package
  before_script:
    - source /etc/profile
  script:
    - /usr/local/maven/apache-maven-3.6.3/bin/mvn clean package
    - cp target/liukaku-eureka-1.0.0-SNAPSHOT.jar docker
    - cd docker
    - docker build -t 192.168.100.11:5000/liukaku-eureka .

push:
  stage: push
  script:
    - echo "push springboot...."
    - docker push 192.168.100.11:5000/liukaku-eureka
#    - echo "done"

deploy:
  stage: deploy
  script:
    - cd docker
    - docker-compose down
    - docker-compose up -d

clean:
  stage: clean
  script:
    - docker rmi `docker images | grep  '<none>' | awk '{print $3}'`

实现环境判断并实现部署脚本

#!/bin/bash
## 拉取代码进行安装

########获取编译函数###########
source /SHELL/buildPack 
############必须配置############
#项目名称即git名称
projectName=$1
#git idea 拉代码的git地址
gitUrl=$2
#打包分支
branch=$3
APIPORT=$4
NGINXPORT=$5

###########初始化参数###########

###############################
if [ -z $APIPORT ];then
   APIPORT="9991"
fi
if [ -z $NGINXPORT ];then
   NGINXPORT="802"
fi
###根据分支判断环境
if [[ ${branch:0:8} = "feature/" ]];then
    env="dev"
    APIPORT=$APIPORT
    NGINXPORT=$NGINXPORT
elif [[ ${branch:0:8} = "release/" ]];then
    env="test"
    APIPORT="9999"
    NGINXPORT="801"
fi

###编译目录
buildDir="/home/gitlab-runner/$env/$projectName/$branch"

###进行部署###
###判断项目编译目录是否存在
if [ ! -d ${buildDir} ];then
    mkdir ${buildDir} -p
fi
cd ${buildDir}

###该分支目录名字已经存在则删除该目录重新拉起
if [ -e $projectName ];then
    rm -rf ${buildDir}/${projectName}
fi
## 拉取分支到本地
git  clone $gitUrl
## 判断是否拉取成功,不成功退出
if [ '0' != $? ]; then 
    echo "注意:更新发生错误!" 
    exit 1
fi
## 进入项目并且切换分支
cd $projectName && git checkout $branch
if [ '0' != $? ]; then 
    echo "注意:切换分支发生错误!"
    exit 3
fi
## 进行编译
if [ $projectName = "web" ];then
  ## 修改配置文件
  echo $NGINXPORT
  sed -i "s/\(^axios.defaults.baseURL = \).*/\1 \'http:\/\/192.168.0.xxx:$NGINXPORT\/api\\'/" src/main.js
  ## 进行web编译
  buildweb  
  ## 编译成功进行文件移动
  if [ -d /ENV/$env/web/$NGINXPORT ];then
    ## 文件存在:判断移动目录
    if [ ! -d /ENV/$env/BACKUP/web/$NGINXPORT ];then
        mkdir /ENV/$env/BACKUP/web/$NGINXPORT -p 
    fi
    ## 文件存在:移动到上面目录
    mv /ENV/$env/web/$NGINXPORT /ENV/$env/BACKUP/web/$NGINXPORT/web_`date "+%Y_%m_%d_%H_%M_%S"` && \
    mv ${buildDir}/$projectName/dist /ENV/$env/web/$NGINXPORT
  else
    echo "修改了端口先通知运维"
    exit 6
  fi
  sed -i "s/\(proxy_pass.*:\).*/\1$APIPORT;/" /etc/nginx/conf.d/$NGINXPORT.conf 
  nginx -t && nginx -s reload
  
elif [ $projectName = "java" ];then
  ## 进行java编译
  buildjava
  ## 编译成功进行文件移动
  if [ ! -d ${buildDir}/${projectName}/$branch/"$projectName"_build ];then
     mkdir ${buildDir}/$projectName"_build" -p
  fi
  cd ${buildDir}/$projectName"_build"
  unzip -oq ${buildDir}/$projectName/lw-admin/target/lw-admin.war -d ./
  sed -i "s/\(active:\).*/\1 $env/" WEB-INF/classes/application.yml  
  ## 备份以前的文件
  if [ -d /ENV/$env/tomcat/$APIPORT/ROOT ];then
    ## 文件存在:判断移动目录
    if [ ! -d /ENV/$env/BACKUP/tomcat/$APIPORT ];then
        mkdir /ENV/$env/BACKUP/tomcat/$APIPORT -p 
    fi
    ## 文件存在:移动到上面目录
    mv /ENV/$env/tomcat/$APIPORT/ROOT /ENV/$env/BACKUP/tomcat/$APIPORT/ROOT_`date "+%Y_%m_%d_%H_%M_%S"` && \
    echo "`ls ${buildDir}/"$projectName"_build`"
    echo "`pwd`"
    mv ${buildDir}/"$projectName"_build /ENV/$env/tomcat/$APIPORT/ROOT
    docker restart "$APIPORT"_tomcat
  else
    echo "修改了端口先通知运维"
    exit 6
  fi
else
  ## 没有在这次自动化规划之内
  echo "该项目$projectName没有该这样自动化部署之内"
  exit 3
fi

实现编译

[root@test2 SHELL]# vim buildPack 

##编译web
buildweb(){
    cnpm  install && \
    cnpm rebuild node-sass && cnpm run build
    if [ $? = 0 ];then
       echo "编译成功"
    else
       echo "编译失败"
       exit 4
    fi
}

##编译java
buildjava(){
    mvn clean && \
    mvn install && \
    mvn package
    if [ $? = 0 ];then
       echo "编译成功"
    else
       echo "编译失败"
       exit 4
    fi
}