Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy SpringBoot projects automatically by Gitlab-runner+Docker

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Gitlab-runner+Docker how to achieve automatic deployment of SpringBoot project", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Gitlab-runner+Docker how to achieve automatic deployment of SpringBoot project" article.

1. Environmental requirements

The operating systems of the following servers are all Centos7

Server A:Gitlab

Server B:GitlabRunner, Docker, docker-compose, Java1.8, maven3.6.3, git

Ps: the GitlabRunner, Java1.8, maven3.6.3 and git of server B can be proposed and deployed separately. The reason why java is needed is that maven,maven is used for packaging.

Application server B only needs docker and docker-compose, which are put together here for demonstration convenience.

Related service building tutorials refer to official documents or other third-party reliable blog tutorials!

Current version of my service

Gitlab:14.9

GitlabRunner:14.10.0

Docker:20.10.14

Docker-compose:1.29.2

Git:2.31.1, check in advance to see if Git is the latest version, otherwise there will be errors later.

two。 Main process

Our main process is to manage the code through Gitlab, and then use the Gitlab CI/CD feature of Gitlab to register and bind with GitlabRunner.

After the binding is successful, each time the code is submitted, the GitlabRunner can be triggered and the corresponding script can be executed for automatic deployment.

3.GitlabRunner installation and registration 3.1 installation summary a total of 2 steps: 1. Add the gitlab official library:

For Debian/Ubuntu/Mint

Curl-L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash

For RHEL/CentOS/Fedora

Curl-L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash2. Command installation:

For Debian/Ubuntu/Mint

Sudo apt-get install gitlab-ci-multi-runner

For RHEL/CentOS/Fedora

Sudo yum-y install gitlab-ci-multi-runner3.2 starts registration

1. Open the warehouse where Gitlab needs to be deployed automatically, and select Setting- "CI / CD -" Runners.

two。 As shown in the figure below, you can see the two most critical messages that will be used when GitlabRunner registers later.

3.GitlabRunner Registration:

Basic commands:

Registration: gitlab-runner register

View registered Runner:gitlab-runner list

Cancel all registrations: gitlab-runner unregister-- all-runners

Go back to server B and execute the command gitlab-runner register registration process for details:

Enter the GitLab instance URL (for example, https://gitlab.com/):

Enter the service URL for gitlab

Enter the registration token:

Enter the token, refer to the figure above

Enter a description for the runner:

Enter Runner description

Enter tags for the runner (comma-separated)

Enter a tag for the gitlab-runner, this tag is very important, and you need to use this tag to specify the gitlab-runner in subsequent use (yml file, job selects the specified Runner by setting the tags tag)

Enter optional maintenance note for the runner:

Enter optional maintenance description

Enter an executor: docker+machine, docker-ssh+machine, custom, docker-windows, docker-ssh, ssh, kubernetes, docker, parallels, shell, virtualbox:

Enter the end, the installation of the gitlab-runner folder will automatically generate config.

Go back to the Runner settings at the gitlab backend, and refresh the page to see a new Runner:

4. Make a script! Important!

When the code is submitted, how should we use Shell scripts to control the build and deployment? Gitlab provides us with a template to create a file named: .gitlab-ci.yml in the root directory of the repository.

Here, take a springboot project as an example. Three files (.gitlab-ci.yml, Dockerfile, docker-compose.yml) have been added to the original project. The location and content of the file are shown below, and the content can be modified according to your needs. You need to be familiar with some basic file structure and syntax of Dockerfile and docker-compose.yml:

3 file location maps

. gitlab-ci.ymlstages:-build-deploy_dev-clean# packaged image build: stage: build only:-dev script:-$MAVEN_HOME/bin/mvn clean package'- Dmaven.test.skip=true'-cp. / XXX/target/XXX.jar docker/-cd docker-docker build-t dev/XXX:v1.0.0. Tags:-maven# deployment development server deploy_dev: stage: deploy_dev only:-dev script:-cd docker- docker-compose down-docker-compose up-d tags:-maven# cleans up virtual suspension images clean: stage: clean only:-dev script:-docker rmi $(docker images-Q-f dangling=true) tags:-maven

Create a Docker folder in the root directory

Create a Dockerfile:

The image volume of # FROM java:8 # java:8 has 643MB. The size of the packaged image is too large. It is not recommended to use # openjdk:8-jre-alpine with the smallest size. Only the following 2 RUN of 85MBFROM openjdk:8-jre-alpine# can solve the error problem of java CAPTCHA interface. RUN echo-e "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main\n\https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/community" > / etc/apk/repositoriesRUN apk-- update add curl bash ttf-dejavu & &\ rm-rf / var/cache/apk/*COPY. / opt/appWORKDIR / opt/app

Create a docker-compose.yml:

Version: '3.1'services: education-course: container_name: dev-XXX-server restart: always image: dev/XXX:v1.0.0 network_mode: "host" volumes:-/ opt/XXX-server/logs:/XXX/logs command: java-jar-Xms1024m-Xmx1024m-Duser.timezone=GMT+08 xxx.jar-- spring.profiles.active=dev5. Test

The local idea submits the code to the dev branch once, and it is already running.

Click in to have a look, there is a problem

Here is a hint that our git version is too low.

Current git version:

[root@localhost local] # git-- versiongit version 1.8.3.1

After upgrading the latest version of git:

[root@localhost yum.repos.d] # git-- versiongit version 2.24.4

Go back to the gitlab backend and execute it manually again:

The latest one has passed passed:

The way to view the log, go to the first build to view the log, and get the path of the project (you have a better way to tell me)

Cd to the docker directory of the project, using the

Docker-compose logs-ft

Test interface:

6. common problem

Question 1: automated deployment build reports an error:

As a solution, execute the command:

Sudo chmod 777 / var/run/docker.sock

Problem 2:docker apline temporary error (try again later) error

No dns caused it.

Create a daemon file on the local system to add dns, and then restart docker

Vim / etc/docker/daemon.json {"dns": ["8.8.8.8"]} the above sudo service docker restart is about "how to automatically deploy the SpringBoot project in Gitlab-runner+Docker". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report