Public number: fly code

The company has been using GitLab to manage code, and it just so happens that GitLab provides GitLab-CI + Runner to execute automated scripts. The combination of these two features just happens to be able to automatically build code after submission

Principle: Gitlab-runner registers with GitLab. When submitting code to GitLab, GitLab notifies runner program, runner program locally pulls the latest code and executes the script defined in. Gitlab-ci. yml file

Gitlab needs to be built first, referring to the super fast gitLab released some time ago

Gitlab-runner installation and configuration

Prepare the following documents first

  1. Apache maven - 3.6.3 - bin. Tar. GzMaven installation package, version does not have to be identical
  2. docker-composeDocker-compose executable file
  3. jdk-8u221-linux-x64.tar.gzVersions of JDK8 do not have to be identical

Create a directory

cd /usr/local
mkdir -p docker/gitlab-runner/build
cd docker/gitlab-runner/build
Copy the code

Place the three prepared files in the build directory

Create a Dockerfile in the build directory with the following contents:

This version should be compatible with the Gitlab version, otherwise there may be some bugs, which can be found on the official website of gitlab-Runner
FROM gitlab/gitlab-runner:v11.4.2

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 && \
    echo 'deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial stable' >> /etc/apt/sources.list

RUN curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
RUN apt-get update -y

RUN apt-get -y install iptables
RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common docker-ce

RUN mkdir -p /usr/local/docker
WORKDIR /usr/local/docker
COPY daemon.json /etc/docker/daemon.json
RUN service docker start

COPY docker-compose /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-compose

RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
Note that the JDK version you downloaded should be replaced by the JDK version you downloaded
COPY jdk-8u221-linux-x64.tar.gz /usr/local/java
RUNtar -zxvf jdk-8u221-linux-x64.tar.gz && \ rm -fr jdk-8u221-linux-x64.tar.gz

RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
Make sure to replace it with the maven version you downloaded. There are changes to the maven version below
COPYApache maven - 3.6.3 - bin. Tar. Gz/usr /local/maven
RUNTar -zxvf apache-maven-3.6.3-bin.tar.gz && \ rm -fr apache-maven-3.6.3-bin.tar.gz

ENV JAVA_HOME /usr/local/java/jdk1.8.0_221
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.6.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

COPY entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

WORKDIR /
Copy the code

Go ahead and create entryPoint. sh in the build directory. This is the entry script for container startup, which mainly starts Docker and Gitlab-Runner, as follows:

#! /bin/sh
service docker start
gitlab-runner run
Copy the code

Continue to create daemon.json, which is the docker configuration file, mainly set docker image source to the country, improve docker image pull speed, the content is as follows:

{
	"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}Copy the code

docker-compose.yml

cd ..
vi docker-compose.yml
Copy the code

Docker-compose.yml contains the following contents:

version: '3.1'
services:
  gitlab-runner:
    build: build
    restart: always
    container_name: gitlab-runner
    privileged: true Run as user root of the host
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - . / Settings. XML: / usr/local/maven/apache maven - 3.6.3 / conf/Settings. The XML The maven version should be the same as the version you downloaded
      - ./config:/etc/gitlab-runner/
Copy the code

Create settings.xml. This is maven’s configuration file to configure the central repository as follows:


      
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	<localRepository>/home/.m2/</localRepository>

	<pluginGroups></pluginGroups>
	<proxies></proxies>

	<servers>
    <! If you need a private server to pull the jar package to build the project, write it here.
	</servers>

	<mirrors>
		<mirror>
		    <id>aliyunmaven</id>
		    <mirrorOf>central</mirrorOf>
		    <name>Ali Cloud public warehouse</name>
		    <url>https://maven.aliyun.com/repository/public</url>
		</mirror>
	</mirrors>
</settings>
Copy the code

Finally, create an empty configuration file. If you do not create an empty configuration file, the container will keep saying that the configuration file does not exist during startup

mkdir config
touch config/config.toml
Copy the code

Docker-compose up -d will automatically build the Gitlab-Runner image and start the container

Gitlab – runner configuration

Go into the GitLab background, find the repository that you want to build automatically, and copy the addresses and tokens in Settings ->CI/CD->Runners of the repository, which you’ll use next

After the container is successfully started, the gitlab-Runner container is entered

docker exec -it gitlab-runner bash
Copy the code

Once in the container, start registering the Runner to GitLab

gitlab-ci-multi-runner register
Copy the code

After the preceding command is executed, some parameters are required as follows:

  1. Please enter the gitlab-ci coordinator URL (e.g. gitlab.com/) :
    • This is the address copied from the gitLab background above
  2. Please enter the gitlab-ci token for this runner:
    • This fills the token copied from the gitLab background above
  3. Please enter the gitlab-ci description for this runner:
    • Fill in a random caption with a description
  4. Please enter the gitlab-ci tags for this runner (comma separated):
    • In.gitlab-ci.yml, the task can specify the CI of different tags to execute
  5. Please enter the executor: docker, shell, virtualbox, docker+machine, docker-ssh+machine, kubernetes, docker-ssh, parallels, ssh:
    • In gitlab-ci.yml, the build script is executed in the specified Docker image

By now the runner has registered with Gitlab, and then we can write.gitlab-ci.yml

.gitlab-ci.yml

Create a.gitlab-ci.yml file in the root directory of the project with the following contents:

# Define build steps
stages:
  - oa

Define specific rules and scripts for each step
oa:
  stage: oa The name # is the name of the step defined above
  only: This argument is an array, indicating that only the specified branch is triggered
    - master Automatic builds are triggered only if code is submitted on the master branch
  tags: # Fill in the tag that runner filled when registering with Gitlab
    - oa
  script: # Concrete build scripts
    # maven build
    - / usr/local/maven/apache maven - 3.6.3 / bin/MVN clean package -Dmaven.test.skip=true
    # Move the jar to the docker/oa-admin/build directory of the project
    - cp api-admin/target/*.jar docker/oa-admin/build/
    Go to the docker/oa-admin directory and start docker
    - cd docker/oa-admin
    - service docker start
    Close the original docker container
    - docker-compose down
    # Rebuild docker image
    - docker-compose build
    # start container
    - docker-compose up -d
Copy the code

Docker configuration

Gitlab-ci. yml contains directories like docker/oa-admin/build, which are the files related to the docker image and container configuration of the project. Now create these directories and corresponding files

Create the docker/oa-admin/build directory in the project root directory

Create docker-comemage.yml in the docker/oa-admin directory

version: '3.1'
services:
  oa-admin:
    build: build
    container_name: oa-admin
    ports:
      - 8088: 8088 # This is the port of the project
Copy the code

Create a Dockerfile in the docker/oa-admin/build directory

FROM openjdk:8-jre

ENV APP_VERSION 1.0.0-SNAPSHOT # Project version number, which is included in the jar file name after Maven is packaged

RUN mkdir /webapps

COPY api-admin-$APP_VERSION.jar /webapps/api-admin.jar Note the name of the jar package

The command executed when the container is started
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/webapps/api-admin.jar"."--spring.profiles.active=test"]
Copy the code

< span style = “box-sizing: border-box; word-break: inherit! Important; word-break: inherit! Important;

After the construction is completed, docker PS can view a running project container on the server of Runner

Gitlab-ci can do more things after the process is clear