Use Jenkins for automated builds

The microservice architecture design of a large platform usually generates many projects, so many services and applications need to be deployed and constantly iterated and updated. This is a huge project, so we need to use automation tools to realize the CICD workflow of each microservice project.

CICD is an umbrella term for Continuous Integration and Continuous Deployment. It refers to a fast delivery process that enables software products to be recycled through automated build, test, and Deployment.

Jenkins is a powerful automated build tool based on Java development, and has a very rich repository of plug-ins, which can be well expanded and enriched its own functions. So Jenkins is a great tool for automated builds.

Click the Plugins option on the Jenkins home page to view the descriptions of various plug-ins, as shown in Figure 15-1.

In this chapter, we use Jenkins to build an automation facility for sustainable delivery in conjunction with tools such as Maven, Docker, Selenium, and JMeter.

Continuous delivery workflow

Starting with code submission, establish a continuous delivery workflow that includes automated testing and deployment as shown in Figure 15-2.

The steps of this workflow are as follows:

(1) Developers submit codes to GitLab.

(2)GitLab uses WebHook to notify Jenkins of code updates.

(3)Jenkins pulled the code from the Slave node, packaged it and built the mirror image.

(4) Jenkins runs test cases using images built from nodes.

(5) If the Test passes, the image will be pushed to the mirror warehouse.

(6) Jenkins updated the deployment on the application server.

(7) Jenkins sends build reports to developers via email.

After the developer submits code to the code base, the entire process is automated. If an error occurs in any of the intermediate steps, the execution of the process is terminated and the result is notified to the relevant personnel. The submitted code includes not only the application, but also scripts for building images, scripts for test cases, choreography scripts for deployment, and so on.

Each step can be done using plug-ins or directly from the command line using various tools.

For example, pull project code uses a Git plug-in; Maven is used for packaging projects; Docker or Docker-compose can be used directly from the command line for image building and application deployment. Integration tests execute scripts generated by Selenium, JMeter, and so on from the command line.

Below, we demonstrate and illustrate the use of Jenkins through a simple case.

The installation of Jenkins

The following installation process uses the MacOS as an example.

Since Jenkins requires JVM support, make sure you have JDK 1.8 or above installed on your machine. For the rest of the automated demonstration, make sure you have Maven, Git client, Docker, and so on installed on your machine.

Open Jenkins official website, enter the download page, and select Mac OSX version in LTS stable version on the left to download, as shown in Figure 15-3.

After downloading, click the installation package “Jenkins-2.89.1.pkg” to start the installation.

The installation process is relatively simple. Click Continue and use the recommended plug-in as prompted. Once the installation is complete, open the Jenkins console locally via the following url:

http://localhost:8080
Copy the code

The page shown in Figure 15-4 is displayed after it is opened for the first time.

Open the administrator password file as prompted in Figure 15-4, copy and paste the password into the password input box, and click the Continue button in the lower right corner. If the password is successfully authenticated, the reader is prompted to create an operator user. Once the user is created, you can log into the Jenkins console. Figure 15-5 shows the welcome page for new users.

Basic Jenkins configuration

Since Maven compilation and packaging are required, click System Administration → Global Tool Configuration on the welcome page, as shown in Figure 15-6. The Global Tool Configuration dialog box is displayed.

In the Global Tool Configuration dialog box, click Maven Install, configure a name, and set the Installation path for Maven, as shown in Figure 15-7.

Click Manage Plug-ins as shown in Figure 15-6. In the dialog box that is displayed, click Available Plug-ins to find the Maven Invoker Plugin. Select it and click Install Directly, as shown in Figure 15-8.

Note that when setting the repositys path in the settings.xml configuration, if you are testing locally, it is best to have the same configuration as IDEA so that you do not have to re-download the dependency package when packaging.

In Jenkins command line configuration, in order to use Docker and docker-compose normally, we need to set Jenkins’ system permissions. Since Jenkins uses the default user “Jenkins” to start the service, the permission Settings are for this user.

Follow the steps below to set up a password-free configuration for user “Jenkins” so that the super administrator command “sudo” can be used in Jenkins command line configuration.

On the terminal of MacOS, run the following command to switch to the super administrator root

Enter the password of root:

appledeMacBook-Air:/ apples su
Password:
Copy the code

Edit “Sudoers” and find the following information:

Sh -3.2# vi/etc/sudoers # root and users in group wheel can run anything on any machine as any userroot ALL = (ALL) ALL %admin ALL- (ALL) ALLCopy the code

At the end of the above information, add and save the configuration as follows, referring to root’s permission Settings:

jenkins ALL=(ALL) NOPASSWD: ALL
%admin ALL=(ALL) NOPASSWD: ALL
Copy the code

Add the Jenkins user to the admin user group using the “DSCL” command, which is equivalent to the “usermod” command in Linux:

Sh -3.2# DSCL. -append /Groups/admin GroupMembership JenkinsCopy the code

At this point, Jenkins’ permissions are set.

An example of Jenkins’ automated deployment

To demonstrate Jenkins in action, let’s create an automatic deployment example.

In this example, you use a very simple project with only one main program, the code shown below

@SpringBootApplication@RestController public class DemoApplication { public static void main (String[] args){ SpringApplication.run(DemoApplication.class,args) ; } @RequestMapping (value = "/")public String index(){ return "Hello world! "; }}Copy the code

After the application starts, the home page displays “Hello World! .

The implementation of this automatic deployment project is described below.

Create a task

Click “New” on the Jenkins home page to open the Create task page, as shown in Figure 15-9.

Enter the task name demo, select Build a Free-style software project, and click OK to create an empty task, as shown in Figure 15-10.

Configuration tasks

In Figure 15-10, click the Source Management option. The dialog box shown in Figure 15-11 is displayed. In Figure 15-11, select Git option and enter the location of the Demo project in the address box of the code base.

Because this is a public project, you do not set permissions to access the project. If it is a private project, you must configure the user name and password for accessing the project in The Credentials shown in Figure 15-11.

Click Build Triggers in Figure 15-11, select Poll SCM in the Build Triggers dialog box, and configure a schedule for a scheduled task, as shown in Figure 15-12.

Schedule 00 20*** in Figure 15-12 indicates that the task is built at 20:00 every day. Scheduled tasks are not used in this example.

Next, use Maven to configure the packaging of the project. Click Build. In the Add Build Step drop-down list, select Invoke Top-level Maven Targets, as shown in Figure 15-13.

In the preceding command output, select Maven from Maven Version and enter the following commands in Goals:

clean package
Copy the code

Configure the operation commands for image creation and deployment, using Dockerfile and docker-comemage. yml, which are included in the docker directory of the project.

The contents of the Dockerfile look like this: FROM Java :8 VOLUME/ TMP adddemo-0.0.1 -snapshot.jar app.jar RUN bash c'touch /app.jar'EXPOSE8080 ENTRYPOINT, "/app.jar"] ["java", "-Djava.security.egd=file:/dev/./urandom", "-jarCopy the code

The deployment script in docker-comemage.yml looks like this:

demo:
build:ports:
"8888:8080"
Copy the code

Click Build, select Execute Shell from the Add Build Steps drop-down list box, and enter the following Command in the Command box:

cd /Users/Shared/Jenkins/Home/workspace/demo /docker cp -f .. Jar sudo /usr/local/bin/docker-compose down --rmi all sudo /usr/local/bin/docker-compose up  -dCopy the code

These commands are the same as the commands we use to deploy applications directly on the host using tools like Docker, that is, stop the running container, delete the container and image, and finally deploy again, as shown in Figure 15-14.

Perform a task

To manually execute a task, click the task name to return to the task home page. On the task home page, click Build Now in the left menu, as shown in Figure 15-15.

During task execution, information is output in the console. The output log of a complete execution process is shown as follows

Started by user mr.csj Building in workspace /Users/Shared/Jenkins/Home/workspace/demo>git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository >git config remote.origin.url https://gitee.com/chenshaojian/demo.git #timeout=10 Fetching upstream changes from https://gitee.com/chenshaojian/demo.git>git --version # timeout=10 >git fetch --tags --progress https://gitee.com/chenshaojian/demo.git+refs/heads/* :refs/remotes/origin/* >git rev-parse refs/remotes/origin/master^ {  commit] # timeout=10 >git rev-parse refs/remotes/origin/origin/master"{ commit}# timeout=10Checking out Revision 1b0348a999cee3a1920b1b20576b54e58a50ab2 (refs/remotes/origin/master) >git config core.sparsecheckout # timeout=10 >git checkout-f 1b0348a999cee3a1920b1b2c576b54e58a50ab2Commit message: "add docker-compose" >git rev-list 8791f0a371ab67a83d1005197744475de5f177df # Timeout =10[demo]$/Users/apple/apache-maven-3.5.0/bin/ MVN Clean package [INFO]Scanning for projects.. [INFO] [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Building demo 0.0.1 - the SNAPSHOT [INFO] [INFO] [INFO] maven - clean - plugin: 2.6.1: the clean (default-clean)& demo ---[INFO] Deleting /Users/Shared/Jenkins/Home/workspace/demo/target [INFO] [INFO] -- Maven-resources-plugin :2.6:resources (default-resources)& demo -[INFO] Using 'UTF-8' encoding to copy filtered Resources. [INFO]Copying 1 resource[INEO]Copying 0 resource[INFO] [INFO] -- Maven-Compiler-plugin :3.1:compile (default-compile) demo ---[ INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/Shared/Jenkins/Home/workspace/demo/target/classes[INEO] [INFO] --- Maven-resources-plugin :2.6:testResources (default-testresources) edemo -- [INFO] Using 'UTF-8'encoding to copy filtered  resources.[INFO] skip non existing resourceDirectory /Users/Shared/Jenkins/Home/workspace/demo/src/test/resources[INFO] [INFO] -- Maven-compiler-plugin :3.1:testCompile (default-testcompile)Cdemo [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/Shared/Jenkins/Home/workspace/demo/target/test-classes[INEO] [ INFO] -- Test (default-test) C demo --[INFO] Tests are skipped. [INFO] [INFO] -- Maven-jar-plugin :2.6:jar (default-jar) demo --[INFO] Building jar: / Users/Shared/Jenkins/Home/workspace/demo/target/demo - 0.0.1 - the SNAPSHOT. Jar [INFO] - [INFO] Spring - the boot - maven - plugin: 1.5.8 RELEA. SE: repackage (default) demo [INFO] [INEO] BUILD SUCCESS [INFO] [INFO] Total time: 5.095s [INFO] Finished at: 2017-10-30T16:18:18+08:00[INFO] Final Memory:29M/182M [INFO] [demo]$ /bin/sh -xe/Users/Shared/Jenkins/tmp/jenkins4696633078670494346.sh +cd /Users/Shared/ Jenkins/Home/workspace/demo/docker+ cp -f . /target/demo-0.0.1 -snapshot.jar. + sudo /usr/local/bin/docker-compose down --rmi allRemoving image docker_demo Failed to remove image for service demo:404 Client Error: Not Found ("No suchimage: docker_demo: latest") + sudo /usr/local/bin/docker-compose up -dBuilding demo Step 1/6 : FROM java: 8 --->d23bdf 5b1b1b Step 2/6:VOLUME /tmp---> Using cache --->64c36a425bbf Step 3/6: ADD demo-0.0.1 -snapshot. jar app.jar-- >1788813d23d2 step 4/6:RUN bash-c 'touch /app.jar'-- > Running in e4cfd4447b78 --->2c44a754963b Removing intermediate container e4cfd4447b78Step 5/6 :EXPOSE 8080 ---> Running in 95b96954618e---> 8bc53f642637 Removing intermediate container 95b96954618e Step 6/6:ENTRYPOINT Java-djava.security.egd =file:/dev/./urandom-jar/app.:一 >Running in al92a418f4F1 -->3a27629ceba9 Removing Intermediate container a192a4184f1Successfully built 3a27629ceba9 Successfully tagged docker demo: latest Image for service demo was built because it did not already exist. To rebuithis image you must use `docker-compose build' or 'docker-compose up --buildCreating docker demo_1.. Aac docker_DEMO_1 -[1A-[2K Creating Docker Demo 1... a [32mdone-[Om--[1BFinished:sUCCESSCopy the code

You can see from the console output log that the build completed successfully. In this case, we can open the home page of the application through the following url:

http://localhost:8888
Copy the code

This shows what we expect, which is the output “Hello World!” , as shown in Figure 15-16.

In the output log of this section, there is an error message like the following:

+ sudo /usr/local /bin/docker-compose down --rmi allRemoving image docker demo
Failed to remove image for service demo:404 Client Error:Not Found ("No suchimage: docker demo: latest")
Copy the code

The reason for this error is that there is no image that can be removed during the first build, but this does not affect the entire build process.

Now verify the automated deployment of project updates. First, the output result of the project’s main program “Hello World! “Hello Jerkins!” , and then commit the code. After completion, click the “Build Now” option in Jenkins. After completion, refresh the browser accessing the application, and you can see the effect as shown in Figure 15-17.

Looking at the console’s output log again, the command to remove the image now no longer displays an error, but instead prints something like the following, indicating that the running container has been stopped and the original container and image removed:

+ sudo /usr/local/bin/docker-compose down--rmi all Stopping docker demo 1 ... -[1A-[2K Stopping Docker Demo 1... -[32mdone-[Om-[1BRemoving Docker Demo 1... -[1A[2K Removing Docker Demo 1 ... -[32mdone-[Om一[1BRemoving image docker demoCopy the code

This is just a simple demonstration of automatic deployment, which can be implemented in practice through scheduled tasks or in combination with WebHook code submission notifications. In addition, you can also use Selenium, JMeter and other tools to generate test scripts and add the function of automatic testing.

summary

This chapter describes how to design a continuous delivery workflow using Jenkins, the automated build tool, and demonstrates the implementation process of automated deployment with a simple example. In this example, we use Git for code pulling, Maven for program packaging, Docker for image creation and application update and deployment. This example shows Jenkins’ strong scalability.

By learning this chapter, you will be able to build a comprehensive automation infrastructure for an automated build process that integrates testing and continuous deployment in microservice publishing.

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

  1. Like, forward, have your “like and comment”, is the motivation of my creation.

  2. Follow the public account “Java rotten pigskin” and share original knowledge from time to time.

  3. Also look forward to the follow-up article ing🚀

  4. [666] Scan the code to obtain the learning materials package

Article is not original, and all of us feel while reading the article good remember thumb up forward + attention oh ~ this article source: www.toutiao.com/i6908692344…