In the last article, I talked about using Maven’s aggregation and inheritance to build the microservice module, and wrote a HelloWorld demo to run locally, and submitted the code to Github.

Of course, this is not enough. It would be tedious to have to checkout the modified code every time it is submitted to a repository like github. Therefore, Jenkins’ CI automatic deployment function is needed to solve this problem. After development, the programmer delivers code that can be compiled into a JAR package, but the JAR package needs to run under the JVM, so it is better to deliver it as a Docker package. So the evolution of the developed deliverables is: code => Jar package => Docker image.

Now LET me show you how Jenkins automates code compilation and packaging into applications, and packaging the applications into Docker images.

Jenkins CI Automation

Jenkins is actually an automation tool, through its own many plug-ins, can help you automate the execution of shell commands. So our goal is that when we submit code to Github, log on to our Jenkins platform, click Deploy Pipeline, and automate our tasks as follows:

  • Pull the source code of our project on Github (Git plug-in)
  • Build packages using Maven build packages to generate JAR packages (Maven plug-ins)
  • Send jar packages to remote machines with Docker environments (SCP)
  • Log in to the remote machine and use Dockerfile to package the JAR package to generate a Docker image (SSH plug-in). Docker Run executes the image
  • Send messages to nails successfully deployed (nails plugin)

Process and Procedure

The following steps are briefly described. I use a CentOS 7 VM with docker environment installed on it, and Jenkins uses docker image Jenkins/Jenkins. During the operation, the Jenkins container pulled github code and compiled jar packages. Since there was no Docker environment in the Jenkins container, jar packages could not be packaged into Docker images, so SCP command was used to send jar packages in the Jenkins container to CentOS virtual machine. In Jenkins, use SSH plug-in to log in to CentOS VM, execute Dockerfile to package jar package into Docker image and run the image.

Problems encountered

Jenkins plugin download error?

  • Because of the wall, many Jenkins plugins cannot be downloaded, so you can change the source in plugin management.

Using Jenkins containers?

  • Because I am too lazy to configure Jenkins’ environment, besides Jenkins, I also need to install Git and Maven. Of course, Jenkins can also help you install it automatically. Using the Docker image, git installation is omitted, but there is no Maven in the Docker image. Jenkins is a Web application, download its WAR package and log in using a browser.

Do not know the password when installing docker container as user root?

  • Docker run this command to log in as root, docker exec it –user root /bin/bash

For convenience, I put the two microservices in the same Docker image, but the CMD command of Docker can only execute one statement

  • Java -jar demo1 is a web package, and java-jar demo1 is a web package, and java-jar demo1 is a web package, and java-jar demo1 is a web package. Being in service forever does not return success, whereas & executes two commands simultaneously

successful

Since then, after the code is written and submitted to Github, log in to the Jenkins platform built locally, click the button of deployment pipeline construction, and automatically pull the code, package the JAR, package it into a Docker image, and run the Docker image. One-click update deployed code, very convenient.