传统的部署无法适应互联网开发的小步快跑,快速迭代,这里使用持续集成工具 GitLab Runner 自动化部署
一般来说,构建任务都会占用很多的系统资源 (譬如编译代码),而 GitLab CI 又是 GitLab 的一部分,如果由 GitLab CI 来运行构建任务的话,在执行构建任务的时候,GitLab 的性能会大幅下降。
GitLab CI 最大的作用是管理各个项目的构建状态,因此,运行构建任务这种浪费资源的事情就交给 GitLab Runner 来做。
因为 GitLab Runner 可以安装到不同的机器上,所以在构建任务运行期间并不会影响到 GitLab 的性能。
/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
在 /usr/local/docker/runner/environment
目录下创建 daemon.json
,用于配置加速器和仓库地址
{
"registry-mirrors": [
"https://registry.docker-cn.com"
],
"insecure-registries": [
"192.168.1.23:5000"
]
}
insecure-registries 是 Docker 私服的地址
在 /usr/local/docker/runner/environment
目录下创建 Dockerfile
自动化部署需要 maven,而 maven 又依赖于 JDK
FROM gitlab/gitlab-runner
MAINTAINER Lusifer
# 修改软件源
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 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 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-8u152-linux-x64.tar.gz /usr/local/java
RUN tar -zxvf jdk-8u152-linux-x64.tar.gz && \
rm -fr jdk-8u152-linux-x64.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.5.3-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.5.3-bin.tar.gz && \
rm -fr apache-maven-3.5.3-bin.tar.gz
# COPY settings.xml /usr/local/maven/apache-maven-3.5.3/conf/settings.xml
# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_152
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.5.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
WORKDIR /
在 /usr/local/docker/runner
目录下创建 docker-compose.yml
表示从 environment 目录下寻找 Dockerfile,即在Docker 里装 Docker
version: '3.1'
services:
gitlab-runner:
build: environment
restart: always
container_name: gitlab-runner
privileged: true
volumes:
- ./config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
在 /usr/local/docker/runner
目录下执行 docker-compose build
等待安装即可,中间有个从 GitHub 拉取 docker-compose 的环节可能比较慢,可以从官网或网页端下载放入目录下,修改Dockefile 从本地安装即可
首先打开 GitLab ,进入持续集成设置界面,我这里是群组
启动容器后,用以下方式进入容器自动执行注册流程:
docker exec -it gitlab-runner gitlab-runner register
在交互式终端中填入 Git Lab 提供的 URL 和 token
如下所示:
root@environment:/usr/local/docker/runner# docker exec -it gitlab-runner gitlab-runner register
Runtime platform arch=amd64 os=linux pid=12 revision=5a147c92 version=11.11.1
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.21/
Please enter the gitlab-ci token for this runner:
kTx_PUnx_hhy63pNJtqM
Please enter the gitlab-ci description for this runner:
[c77b5e39a2a6]:
Please enter the gitlab-ci tags for this runner (comma separated):
Registering runner... succeeded runner=kTx_PUnx
Please enter the executor: docker, shell, ssh, docker-ssh+machine, docker+machine, kubernetes, docker-windows, docker-ssh, parallels, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
root@environment:/usr/local/docker/runner#
这里跳过了 description 与 tags ,tags表示什么时候触发,为空表示任何时候都触发,deploy表示部署时触发,下面是执行器,这里填入 shell