Jenkins Automated deployment

Sat jan 02, 2021 15:11:03

Formally learn automated deployment

The first exploration, the first implementation of Java’s Springboot project automatic deployment

Introduction: THE system I use is deepinv20 system

1. Since this is a Java project, install the Java environment first

Download jdK-8U271-linux-x64.tar. gz from the official website to the local directory

Unpack the

tar -zxvf jdk-8u271-linux-x64.tar.gz

Move the unzipped folder to the current user’s file directory

The mv jdk1.8.0 _271 / home/shizc /

Configure Java environment variables

sudo vi /etc/bash.bashrc

After opening the configuration file, enter the following information at the end of the file

JAVA_HOME = / home/shizc/jdk1.8.0 _271

CLASSPATH=.:$JAVA_HOME/bin.tools.jar

PATH=
J A V A H O M E / b i n : JAVA_HOME/bin:
PATH

export JAVA_HOME CLASSPATH PATH

After saving the configuration and exiting, run the following command to make the configuration take effect

source /etc/bash.bashrc

2. Set environment variables for Maven, the Java project packaging tool

You can go to the Maven website to download the latest maven package to the local, and then unzip to use, I used the IDEA plugin with Maven3

sudo vi /etc/bash.bashrc

Add content:

The export PATH = “/ home/shizc/idea – IU – 183.6156.11 / plugins/maven/lib/maven3 / bin: $PATH.” “

Save and exit

source /etc/bash.bashrc

Yes Modification takes effect

The input

mvn –help

Verify that the configuration is successful

3. Initialize a test project using Spring Initializr

Ali Cloud’s mirror image can be used in China to speed up construction

Address: start.aliyun.com/

Just select Spring Web so we can write interfaces to test, and that’s it

Next, we write an interface and access it

Access after startup

4. Install the Git environment

This example uses the open source Gitee code cloud as the code repository, so you need to use a Git environment for code upload and download

sudo apt-get install git

The installation is complete using the command

git –help

To verify

Then we upload the project to the Gitee repository

The Gitee plug-in for IDEA needs to be installed first

Restart the idea

Configuration code cloud account

Upload code

After the message is displayed, go to Gitee to view it

5. Install Docker and use container deployment for the SpringBoot project

If you don’t know About Docker, Google it

Docker ali cloud binary package download address: mirrors.aliyun.com/docker-ce/l…

Download your favorite version, such as the latest version 19.03.12

Download it and unzip it

The tar XZVF docker 19.03.12 – ce. TGZ

Copy the decompressed program files to the /usr/bin directory

sudo cp docker/* /usr/bin/

When the copy is complete, Docker is already installed

Enter the command

docker version

The relevant information will appear

Then we configure Docker to start automatically

vi /usr/lib/systemd/system/docker.service

The input

[Unit] Description=Docker Application Container Engine Documentation=docs.docker.com After=network-online.target firewalld.service Wants=network-online.target

[Service] Type=notify ExecStart=/usr/bin/dockerd ExecReload=/bin/kill -s HUP $MAINPID LimitNOFILE=infinity LimitNPROC=infinity TimeoutStartSec=0 Delegate=yes KillMode=process Restart=on-failure StartLimitBurst=3 StartLimitInterval=60s

[Install] WantedBy=multi-user.target

After writing the configuration file, start the service

Systemctl start docker service systemctl enable docker service

6. Install Jenkins

Jenkins is an open source continuous integration (CI) tool that provides a user-friendly interface. Originated in Hudson (Hudson is commercial), Jenkins is mainly used for continuous, automated building/testing of software projects and monitoring the execution of external tasks. Jenkins is written in the Java language and can run in popular servlet containers such as Tomcat or on its own. It is often used in combination with version management tools (SCM) and build tools. Common version control tools include SVN and GIT, and build tools include Maven, Ant, and Gradle.

Pull the DOCker image of the JDK locally

sudo docker pull openjdk:8-alpine

This mirror image is only 105M, which is pretty small

Theoretically, as long as the function meets the requirements, the mirror should be as small as possible, and this is the smallest I’ve ever seen

Download Jenkins

Website address: www.jenkins.io/zh/download…

Download the WAR package, which is more convenient

Start command:

nohup java -jar /home/shizc/Documents/jenkins.war>/home/shizc/Documents/jenkins.out&

The default port number for this background startup mode is 8080

Enter: IP :8080 in the address bar

Follow the instructions on the page to find the password and enter the command:

cat /home/shizc/.jenkins/secrets/initialAdminPassword

Enter your password, click Continue, and wait

After the above page, don’t rush to the next step, the default mirror source is foreign, will be very slow, into the domestic mirror source, the installation is much faster.

I need to close the Jenkins program that I just started,

Use the JPS command to view the Java program currently running. JPS comes with the JDK

Using the command

kill -9 8719

Close Jenkins program

Use the JPS command again to verify that it has been shut down

The Jenkins directory is: /home/shizc/.jenkins/

We go to the Updates directory and make file changes

cd /home/shizc/.jenkins/updates

Sed -i ‘s # updates. Jenkins. IO/download# ht… ‘ default.json

sed -i ‘s#www.google.com#www.baidu.com#g’ default.json

Then use the command:

nohup java -jar /home/shizc/Documents/jenkins.war>/home/shizc/Documents/jenkins.out&

Start it again, Jenkins.

The next step is to set up your own user and password

Next, set up the global tool configuration, JDK, Git, and Maven

Click on the Settings in the image

Click save

Click OK to create a task that jumps to the next page

Enter a description

Enter the address of the code repository that needs to be deployed automatically. It must be accessible from the Internet. (Simple Intranet environment, this article is not applicable oh)

Then comes the core

Post the content of the build script you wrote. Here is the content of the script:

#! MVN clean package echo 'package ok! ' echo 'build start! 'CD./ service_name="demo1" service_port=10086 Delete the IID = $(echo "asd |" sudo docker images | grep "$service_name" | awk '} {print $3) echo "IID $IID" if [-n] IID "$" Then echo "exist" $service_name "image, IID = $IID" # remove mirror echo "asd |" sudo docker rmi - f $service_name echo "delete $service_name # building "echo" asd "| sudo docker build - t $service_name. Echo" build $service_name "else echo," not to exist "$service_name" image, build docker "# building echo" asd "| sudo docker build - t $service_name. Echo" build $service_name "fi # to check the container id CID = $(echo "asd |" sudo docker ps | grep "$service_name" | awk '} {print $1) echo "CID $CID" if [-n] "$CID" Then the echo "exist" $service_name "container, CID = $CID" # stop echo "asd" | sudo docker stop $service_name echo "asd" | # delete container Sudo docker rm $service_name else echo "not exist" $service_name "container" fi # start echo "asd |" sudo docker run - d --name $service_name --net=host -p $service_port:$service_port $service_nameCopy the code

The main process of the script is: package -> build docker image -> start docker container

Of course, to save disk space, add judgment to the script to remove old images and containers. Of course, for version control, you can modify the script.

Versioning the project with the mirrored version. For example, you can save five images of demo:v1 to Demo: V5. If the images exceed the versions, delete them and start the container using the images of different versions.

Then click the Save button. Go to the next page.

Click the Build Now button to begin the automatic deployment process

Let’s modify the code, submit it to the repository, and click Build to see if we can do one-click deployment

Submit the code and click Build.

Then the task information will appear below, click that time, you can enter the details page.

Go to the details page and click console Output to see the entire deployment process and, if the build fails, the reason for the failure. Modify the configuration or script based on the reason, then click Build again.

When we see a success sign, we go to the browser, type in the address, and see if what comes back is what we expect.

Hey, hey, it worked.

This way, we can create a task for each of our microservices and, after the code is updated, manually click build for more flexibility.

In addition, since we are talking about automatic deployment, of course, we can achieve code submission without intervention, the program automatically good = build deployment.

This is where the trigger comes in. You can set up a training rotation, constantly check the code is not updated. But I don’t think it’s common to use fully automated deployment. It’s a waste of resources. The deployment mode can be set to periodic deployment. Automatically release the latest code at a certain point in time.

This is my first time using Jenkins automatic deployment, so make a note of it and check it out if you forget.

More standardized and advanced automated deployment applications need further study and exploration. If you touch the automation deployment, assembly lines and so on of big factories, you will find that this is similar to what we are talking about, and even adapted from this.