GitLab的部署及GitLab Runner

GitLab的部署

安装

docker-compose.yml 配置如下:

version: '3.1'
services:
    web:
      image: 'twang2218/gitlab-ce-zh:11.1'
      restart: always
      container_name: GitLab
      hostname: '192.168.111.143'
      environment:
        TZ: 'Asia/Shanghai'
        GITLAB_OMNIBUS_CONFIG: |
          external_url 'http://192.168.111.143:8080'
          gitlab_rails['gitlab_shell_ssh_port'] = 2222
          unicorn['port'] = 8888
          nginx['listen_port'] = 8080
      ports:
        - '8080:8080'
        - '8443:443'
        - '2222:22'
      volumes:
        - /usr/local/docker/gitlab/config:/etc/gitlab
        - /usr/local/docker/gitlab/data:/var/opt/gitlab
        - /usr/local/docker/gitlab/logs:/var/log/gitlab

启动

docker-compose up

SSH免密登录

tool\Git\usr\bin下输入命令

ssh-keygen -trsa -C "18845124018@163.com"

然后将生成的公钥添加到代码托管平台即可

使用GitLab Runner 完成持续继承部署

基础概念

Pipeline

一次 Pipeline 其实相当于一次构建任务,里面可以包含多个流程,如安装依赖、运行测试、编译、部署测试服务器、部署生产服务器等流程。

任何提交或者 Merge Request 的合并都可以触发 Pipeline,如下图所示:

+------------------+           +----------------+
|                  |  trigger  |                |
|   Commit / MR    +---------->+    Pipeline    |
|                  |           |                |
+------------------+           +----------------+
Stages

Stages 表示构建阶段,说白了就是上面提到的流程。我们可以在一次 Pipeline 中定义多个 Stages,这些 Stages 会有以下特点:

  • 所有 Stages 会按照顺序运行,即当一个 Stage 完成后,下一个 Stage 才会开始
  • 只有当所有 Stages 完成后,该构建任务 (Pipeline) 才会成功
  • 如果任何一个 Stage 失败,那么后面的 Stages 不会执行,该构建任务 (Pipeline) 失败

因此,Stages 和 Pipeline 的关系就是:

+--------------------------------------------------------+
|                                                        |
|  Pipeline                                              |
|                                                        |
|  +-----------+     +------------+      +------------+  |
|  |  Stage 1  |---->|   Stage 2  |----->|   Stage 3  |  |
|  +-----------+     +------------+      +------------+  |
|                                                        |
+--------------------------------------------------------+
Jobs

Jobs 表示构建工作,表示某个 Stage 里面执行的工作。我们可以在 Stages 里面定义多个 Jobs,这些 Jobs 会有以下特点:

  • 相同 Stage 中的 Jobs 会并行执行
  • 相同 Stage 中的 Jobs 都执行成功时,该 Stage 才会成功
  • 如果任何一个 Job 失败,那么该 Stage 失败,即该构建任务 (Pipeline) 失败

所以,Jobs 和 Stage 的关系图就是:

+------------------------------------------+
|                                          |
|  Stage 1                                 |
|                                          |
|  +---------+  +---------+  +---------+   |
|  |  Job 1  |  |  Job 2  |  |  Job 3  |   |
|  +---------+  +---------+  +---------+   |
|                                          |
+------------------------------------------+

GitLab Runner 的安装

  • 构建自定义gitlab-runner镜像(因为gitlab-runner不是仅仅服务Java的,需要自定义环境,安装Java,maven等)

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

FROM gitlab/gitlab-runner:v11.1.1
MAINTAINER swing <18845124018@163.com>

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

# 安装 Docker
#RUN curl -fsSL get.docker.com -o get-docker.sh && \
 #   sh get-docker.sh --mirror AzureChinaCloud && \
#COPY daemon.json /etc/docker/daemon.json
#RUN systemctl daemon-reload && \
#    systemctl restart docker

RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \
    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 curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
RUN chmod +x /usr/local/bin/docker-compose

# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk-8u151-linux-x64.tar.gz /usr/local/java
RUN tar -zxvf jdk-8u151-linux-x64.tar.gz && \
    rm -fr jdk-8u151-linux-x64.tar.gz

# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
COPY apache-maven-3.6.3-bin.tar.gz /usr/local/maven
# COPY apache-maven-3.5.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
RUN mkdir repository
RUN chmod -R 777 repository/
COPY settings.xml /usr/local/maven/apache-maven-3.6.3/conf/settings.xml

# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_151
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.6.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

WORKDIR /
  • 下载 jdk-8u151-linux-x64.tar.gzapache-maven-3.6.3-bin.tar.gz 并复制到 /usr/local/docker/runner/environment 下,并将已经配置好的maven/setting.xml放入该目录下:

  • /usr/local/docker/runner/environment/daemon.json

{
  "registry-mirrors": [
    "https://qy5lms4s.mirror.aliyuncs.com"
  ],
  "insecure-registries": [
    "192.168.1.10:8082"
  ]
}
  • 在/usr/local/docker/runner目录下创建docker-compose.yml
version: '3.1'
services:
  gitlab-runner:
    //如果没有声明镜像,则先使用environment构建为一个镜像,然后启动
    build: environment
    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
  • 构建docker并运行容器
docker-compose up
  • 注意:如果部署时提示找不到命令,说明环境变量未配置,使用docker exec -it gitlab-runner bash 进入容器,然后依次进行如下配置:
#系统环境变量
root@0b2695686926:/usr/local/maven/repository# cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export JRE_HOME=/usr/local/java/jdk1.8.0_151/jre
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

#用户环境变量
root@0b2695686926:/usr/local/maven/repository# cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export JRE_HOME=/usr/local/java/jdk1.8.0_151/jre
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
export M2_HOME=/usr/local/maven/apache-maven-3.6.3
export CLASSPATH=$CLASSPATH:$M2_HOME/lib
export PATH=$PATH:$M2_HOME/bin

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

#启用用户环境变量
root@0b2695686926:/usr/local/maven/repository# source /etc/profile

在GitLab上注册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.1.10:8080/

# 输入 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

注册成功后的标识:

项目 》设置 》CI/CD 》Runner 》当前有效的Runner变为小绿点

并且在/usr/local/docker/runner/config/config.toml中显示有注册信息

开始使用Runner

首先在仓库的根目录下新建一个 .gitlab-ci.yml 文件,该文件是出发自动化部署的核心文件

.gitlab-ci.yml

stages:
  - build
  - push
  - run
  - clean

#打包与构建
build:
  stage: build
  script:
    - cd itoken-config/
    - mvn clean package -Dmaven.test.skip=true
    - cp target/itoken-config-1.0.0-SNAPSHOT.jar docker
    - cd docker
    - docker build -t 192.168.1.10:8082/itoken-config:1.0.0-snapshot .

#docker镜像构建(每一步的stage都是从 “../root/itoken”目录下开始的
push:
  stage: push
  script:
    - docker push 192.168.1.10:8082/itoken-config:1.0.0-snapshot

run:
  stage: run
  script:
    - cd itoken-config/docker
    - docker-compose down
    - docker-compose up -d

clean:
  stage: clean
  script:
    - docker rmi $(docker images -q -f dangling=true)

在服务文件夹下新建docker文件夹,分别新建 Dockerfile 和 docker-compose.yml

FROM openjdk:8-jre

MAINTAINER swing <18845124018@163.com>

RUN mkdir itoken-config

COPY itoken-config-1.0.0-SNAPSHOT.jar itoken-config/itoken-config.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "itoken-config/itoken-config.jar", "--spring.profiles.active=prod"]

EXPOSE 8700
version: '3.1'
services:
  itoken-config:
    restart: always
    image: 192.168.1.10:8082/itoken-config:1.0.0-snapshot
    container_name: itoken-config-1.0.0-snapshot
    ports:
      - 8700:8700

当项目被推送到GitLab上时,gitlab会触发该项目绑定的Runner,继而runner会将该项目克隆到它的 /home/gitlab-runner/builds/46f7b428/0/root/itoken文件夹下(这里拿itoken项目举例),然后开始执行部署流程

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐