1. GitLab Runner 简介
一般来说,构建任务都会占用很多的系统资源 (譬如编译代码),而 GitLab CI 又是 GitLab 的一部分,如果由 GitLab CI 来运行构建任务的话,在执行构建任务的时候,GitLab 的性能会大幅下降。
GitLab CI 最大的作用是管理各个项目的构建状态,因此,运行构建任务这种浪费资源的事情就交给 GitLab Runner 来做!
因为 GitLab Runner 可以安装到不同的机器上,所以在构建任务运行期间并不会影响到 GitLab 的性能。
本文主要内容:基于Docker按照gitlab-runner
,并集成maven/jdk/docker/docker-compose
2. 基于 Docker 安装 GitLab Runner
环境
- Ubuntu: 18.04.3 LTS
- Docker: 19.03.3, build a872fc2f86
- Docker-compose: 1.24.0, build 0aa59064
- Harbor: v1.9.0 (可用nexus或者registry替代)
- Gilab中文社区版: twang2218/gitlab-ce-zh:11.1.4
2.1 准备工作
mkdir -p /usr/local/docker/runner
mkdir -p /usr/local/docker/runner/environment
2.2 daemon.json
在 /usr/local/docker/runner/environment
目录下创建 daemon.json,用于配置镜像加速器和仓库地址
1 2 3 4 5 6 7 8 9 10
| sudo tee /usr/local/docker/runner/environment/daemon.json <<-'EOF' { "registry-mirrors": [ "https://registry.docker-cn.com" ], "insecure-registries": [ "192.168.10.133:5000" ] } EOF
|
推荐配置阿里云镜像加速器,可参考Linux安装Dokcer CE 3.5)
2.3 Dockerfile
在/usr/local/docker/runner/environment
目录下创建 Dockerfile
vim /usr/local/docker/runner/environment/Dockerfile
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| FROM gitlab/gitlab-runner MAINTAINER EricShen <ahsbt@126.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
RUN curl -fsSL get.docker.com -o get-docker.sh && \ sh get-docker.sh --mirror Aliyun COPY daemon.json /etc/docker/daemon.json
WORKDIR /usr/local/bin RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose RUN chmod +x docker-compose
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
RUN mkdir -p /usr/local/maven WORKDIR /usr/local/maven
COPY apache-maven-3.6.1-bin.tar.gz /usr/local/maven RUN tar -zxvf apache-maven-3.6.1-bin.tar.gz && \ rm -fr apache-maven-3.6.1-bin.tar.gz
ENV JAVA_HOME /usr/local/java/jdk1.8.0_152 ENV MAVEN_HOME /usr/local/maven/apache-maven-3.6.1 ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
WORKDIR /
|
2.4 docker-compose.yml
在 /usr/local/docker/runner
目录下创建 docker-compose.yml
1 2 3 4 5 6 7 8 9 10
| 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
|
2.5 构建镜像
确认所需文件
ls /usr/local/docker/runner
有
ls /usr/local/docker/runner/environment
有
构建镜像
1 2
| cd /usr/local/docker/runner docker-compose build
|
1 2 3
| ...省略... Successfully build xxxxxxx Successfully tagged runner_gitlab-runner
|
1 2 3
| docker images #查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE runner_gitlab-runner latest 74b8379cca15 41 hours ago 1.37GB
|
3. 注册 Runner
首先获取Gitlab上项目路径=>设置=>CI/CD=>Runner=>Setup a specific Runner manually
下Gitlab
地址和token
然后到服务器中执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| docker exec -it gitlab-runner gitlab-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://192.192.1.12/
Please enter the gitlab-ci token for this runner: PgC_sRjg3qZgrU4WGvjY
Please enter the gitlab-ci description for this runner:
Please enter the gitlab-ci tags for this runner (comma separated): deploy
Registering runner... succeeded runner=PgC_sRjg
Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh: shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
|
4. 使用 Runner
进入你想要进行CI/CD的项目中(FYI: 通过Spring Initializr快速构架一个基于maven构建的Spring Boot项目)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| FROM openjdk:8-jre MAINTAINER EricShen <ahsbt@126.com>
ENV APP_VERSION 1.0.0-SNAPSHOT ENV DOCKERIZE_VERSION v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
RUN mkdir /app
COPY test-$APP_VERSION.jar /app/app.jar ENTRYPOINT ["dockerize", "-timeout", "5m", "-wait", "tcp://192.192.1.12:3306", "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"]
EXPOSE 8080
|
1 2 3 4 5 6 7
| version: '3.1' services: test_java: image: 192.192.1.12:9090/test/test:v1.0.0 container_name: test_container ports: - 8080:8080
|
- 编写
.gitlab-ci.yml
文件,添加到项目根路径下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| stages: - build - push - run - clean
build: stage: build script: - /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package - cp target/test-1.0.0-SNAPSHOT.jar docker - cd docker - docker build -t 192.192.1.12:9090/test/test:v1.0.0 .
push: stage: push script: - docker login 192.192.1.12:9090 -u admin -p Harbor12345 - docker push 192.192.1.12:9090/test/test:v1.0.0
run: stage: run script: - cd docker - docker-compose down - docker-compose up -d
clean: stage: clean script: - docker image prune -f
|
5. 测试集成效果
所有操作完成后 push
代码到服务器,Gitlab
会自动进行构建,在CI/CD
的Pipeline
中查看构建的结果:
- 点击进入查看每条
Pipeline
的详情,由下图可见Pipeline
有4个Stages
,build=>push=>run=>clean
- 此时可以看到构建完成,通过访问8080端口即可查看项目已经成功启动
6. 其它命令
删除注册信息
docker exec -it gitlab-runner gitlab-runner --name "名称"
删除所有注册信息
docker exec -it gitlab-runner gitlab-runner unregister --all-runners
查看注册列表
docker exec -it gitlab-runner gitlab-runner list