foreword
Deployment is a repetitive and boring thing. I believe everyone has heard of CI/CD continuous integration and continuous delivery. This article is based on Gitlab CI/CD and Docker to quickly realize the automatic deployment of the project. This solution can be closely integrated with Git and can realize automatic building and deployment after code submission without additional tools.
Preparation
First of all, please install the following three softwares. It is recommended to install them in order. For the software installation, please refer to the reference links given below:
- Docker: installation tutorial ;
- Gitlab: installation tutorial ;
- GitlabRunner: installation tutorial ;
main process
Our main process is to manage the code through Gitlab, and then use the Gitlab CI/CD function that comes with Gitlab to register and bind with GitlabRunner. After the binding is successful, every time the code is submitted, GitlabRunner can be triggered to execute the corresponding script program to realize automatic deployment.
GitlabRunner registration
- Open the Gitlab warehouse that needs to be automatically deployed, and select Setting-"CI/CD-"Runners.
- As shown in the figure below, you can see the two most critical information, which will be used when GitlabRunner registers later, please remember:
- GitlabRunner Registration
Command Guide:gitlab-runner register
Register,gitlab-runner list
view registered Runners,gitlab-runner unregister --all-runners
cancel all registrations.
First, we register one first, and then name a Runner (whatever), here I name it: hello, spring boot!, and then enter the tags of the runner, we can use tags to distinguish different stages of a project for different runners to execute, enter here : maven; Finally, enter the script you want to execute, here use the shell.
After the registration is successful, let's refresh the Gitlab page just now, and you can see that the registration has been successful!
production script
After submitting the code, how should we use Shell scripts to control the build and deployment? Gitlab has provided us with a template, which is to create a .gitlab-ci.yml
file named: in the root directory of the warehouse. I execute a SpringBoot project here, so the content is as follows:
stages:
- build
- deploy_test
# - clean
# 打包镜像
build:
stage: build
only:
- master
script:
- $MAVEN_HOME/bin/mvn clean package -U '-Dmaven.test.skip=true'
- cp target/GitlabRunnerTest-0.0.1-SNAPSHOT.jar docker/
- cd docker
- docker build -t test/gitlabrunner:v1.0.0 .
tags:
- maven
# 部署测试服务器
deploy_test:
stage: deploy_test
only:
- master
script:
- cd docker
- docker-compose down
- docker-compose up -d
tags:
- maven
#清理虚悬镜像
#clean:
# stage: clean
# only:
# - master
# script:
# - docker rmi $(docker images -q -f dangling=true)
# tags:
# - maven
Then create a Docker folder in the root directory, create a Dockerfile:
FROM openjdk:11
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
Create docker-compose.yml
version: '3.1'
services:
education-course:
container_name: test-gitlabrunner
restart: always
image: test/gitlabrunner:v1.0.0
network_mode: "host"
command: java -jar -Xms1024m -Xmx1024m -Duser.timezone=GMT+08 GitlabRunnerTest-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
The overall project directory structure is as follows:
verify
After it is done, we can upload the code, and then we can check whether the execution is successful in the CI/CD Pipelines.
As you can see, the build is successful, let's visit:
Congratulations, it has been successfully deployed. You can try to modify the GIt submission in the code to see the effect of automatic deployment.