Objective: To improve production efficiency through automation.

The first way: use the Idea Deployment tool for semi-automation.

Idea new A SpringBoot project, check spring Web dependencies.

(1) : Write a Controller, remember to start the function under the same subpackage, otherwise the packet scanning can not be found, need to manually add the packet scanning address.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloCtl {

    @GetMapping("/hello")
    private String hello(a){
        return "Greetings from IDEA Deployment"; }}Copy the code

(2) : Configure Devployment for ADDRESS and file relationship mapping.

Use Maven to wrap it up. After the connection is successful, configure the file mapping relationship.

I don’t know why IDEA can’t detect the jar update and automatically upload the new JAR package, but the code update can be automatically uploaded, anyone know how to solve this problem?

java -jar ***.jar
Copy the code

Access the host by yourself :8080/hello. How to automatically start a new JAR package can be solved with a shell or Python script.

  • The while loop determines whether the file is updated.
  • Update and kill the current Springboot process and restart the JAR package.
  • It doesn’t seem to be particularly normative, so update.

Second: via Docker

(1) : Also create a New Springboot, configure docker, configure dockerFile.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloCtl {
    
    @GetMapping
    public String hello(a){
        return "Greetings from docker."; }}Copy the code

Configure the IDEA docker

docker pull openjdk:8
Copy the code

Add the Dockerfile file to the target directory.

FROM openjdk:8
COPYDemo - 0.0.1 - the SNAPSHOT. Jar/TMP/app. The jarWORKDIR /tmp
ENTRYPOINT ["java"."-jar"."app.jar"]
Copy the code

Remember that the demo-0.0**.jar package is your own jar package.

(2) : Add a docker running context.

(3) : Push the constructed Image to docker Hub or Docker private server and deploy it on the server side

Docker pull your image Docker run -p 8080:8080 your imageCopy the code

The third way (recommended) : CI (continuous integration) Jenkins+Gitlab+Docker

In simple terms, in the traditional deployment process, Git Push new code -> pull new code ->Maven build to generate a new Image-> pull new Image-> delete the old Image, delete the old container -> run the new Image-> check whether the project is normal through HTTP or browser. In CI, developers only need to care about git Push new code and check whether the project is running normally. The tedious and repetitive labor is completed by automatic construction tools Jenkins+Gitlab+Docker. Developers only need to realize highly customized automatic deployment through simple configuration.

(1) (Optional, you can choose not to build it, and directly use the existing Github or other Git platform) : build gitLab private server, I wanted to build github, but failed to find a solution for a long time.

Docker pull gitlab/gitlab ‐ ce: the latestCopy the code

Gitlab is a bit big, so you need to be patient. Docker Acceleration (optional)

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["Your docker accelerate address, like https://skdgjskd.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Copy the code

Speed up the address for https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors.

Create mount folders and start containers

Create a mount folder
mkdir -p /opt/gitlab/etc
mkdir -p /opt/gitlab/log
mkdir -p /opt/gitla/data
#gitlab
docker run \
    --detach \
    --publish 8443:443 \
    --publish 8090:80 \
    --name gitlab \
    --restart always \
    -v /opt/gitlab/etc:/etc/gitlab \
    -v /opt/gitlab/log:/var/log/ gitlab \ - v/opt/gitlab/data: / var/opt/gitlab \ gitlab/gitlab ‐ ce: the latest# change configuration
 vi /opt/gitlab/etc/gitlab.rb
 Change external_url 'http://{to your host's IP address}'
vi /opt/gitlab/data/gitlab-rails/etc/gitlab.yml
# Open and find
# gitlab:
# ## Web server settings (note: host is the FQDN, do not include http://)
# host: {enter your host address here}
# port: {8090}
# https: false

If the host IP address is 8090, you can verify whether the setup is successful.
Set the root password and log in as user root
Copy the code

(2) Jenkins must be built.

docker run -p 8080:8080 -p 50000:50000 -v jenkins_data:/var/jenkins_home -v jenkinsci/blueocean 
# Jenkin containers take time to start
Copy the code

# It says that you need to enter the Jenkin password, we need to enter the Jenkin container to check.
docker exec- it 7 f485bd95c3b/bin/bash into cat/var/Jenkins container jenkins_home/secrets/initialAdminPasswordCopy the code

After entering the password, enter Jenkins, select recommended installation, you can choose to directly log in with the admin account, and the password is obtained by cat above.

(3) Build docker private server (optional, public server can be used)

Docker run ‐name Docker ‐registry ‐d ‐p 55:5000 Registry vi /etc/docker/daemon.json# Add the following
#{"insecure‐registries":[" host IP address :5000"]}
Systemctl restart docker and start the container.
Copy the code

(4) Configure Jenkins JDK, Maven, Docker, Git and plug-in environment.

# Enter Jenkins container
ssh-keygen -t rsa
cat /var/jenkins_home/.ssh/id_rsa.pub 
Copy the code

Configure jenkin JDK, Maven, Docker, Gitlab plugin, Maven plugin, SSH plugin, and save the Settings

(5) Configure GitLab Webhook and Jenkin anonymous login.

Create a project on Gitlab to configure the remote repository address, along with the user name and password, in the myTest configuration item.


Configure your Webhook on Gitlab. Remove restrictions on local urls (since my Jenkins and GitLab are built on the same machine)

(6) Configure the Jenkins build script.

The first script does these things

  • SSH login docker host
  • Stop and delete the old Springboot container
  • Delete the old Springboot Image

First, configure Jenkins’ SSH credentials

#! /bin/bash
result=$(docker ps | grep "{specify your old Springboot container name, or id}") 
if [[ "$result"! ="" ]] 
then 
echo "stop old container"Docker stop {specify your old container}fiResult1 = $(docker ps ‐ a | grep"{specify your old Springboot container name, or id}") 
if [[ "$result1"! ="" ]] 
then 
echo "rm old container"Docker rm {container name}fi
result2=$(docker images | grep {old Image name})
if [[ "$result2"! ="" ]] 
then 
echo "rm old image"Docker rmi {old Image name}fi
Copy the code

There are two more scripts, but we’ll talk about that later. Jenkins is a visual scheduling tool that helps us manage the build process. Ok next we create a SpringBoot project in idea and add the code.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloCtl {

    @GetMapping("/hello")
    private String hello(a){
        return "Greetings from Jenkins"; }}Copy the code

Configure POM, add docker package plug-in (Google open source Java container management tool Jib, more detailed configuration refer to github official website github.com/GoogleConta…

<properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.7.0</version>
                <configuration>
                    <! -- Configure basic mirroring -->
                    <from>
                        <image>openjdk:8-jdk-alpine</image>
                    </from>
                    <! -- Configure the final push address, warehouse name, mirror name -->
                    <to>
                        <! How to configure public server address please refer to my "how to use Docker package Springboot four ways" article -->
                        <image>192.168.208.102:5000 / test1</image>
                        <tags>
                            <tag>idea</tag>
                        </tags>
                    </to>
                    <! Because the private server is HTTP and not HTTPS, the JIB does not push you to non-HTTPS private server by default.
                    <allowInsecureRegistries>true</allowInsecureRegistries>
                </configuration>
            </plugin>
        </plugins>
    </build>
Copy the code

Push Maven project to Docker host and try to build in Docker host. In the Pom directory, enter the build command

mvn compile jib:build
Copy the code

As you can see, the Image was successfully built and pushed to private server.

  • Pull new Image from private server
  • Run the new Image
#idt specifies the pull addressDocker run --name} -p 8081:8081 -idt 192.168.101.64:5000/{fill in your own Imaga:Tag} dockerlog -f test1
Copy the code

After the configuration, we saved it. Here, we pushed the code we had written before on the private server, and immediately saw that Jenkins automatically executed three scripts for us, realizing the full automatic deployment.

The final summary

I won’t say SSH is too simple. Idea visualization construction Docker, is also relatively simple, only need you to configure, and are visual. Docker + Jenkins + GitLab, Jenkins is actually a visual build script management tool, you just need to follow the steps I give to configure it. Any questions, just leave a message. If this article helps you, how about a little like?