Automatically deploy a Web application developed in Java using Jenkins [Preparation]

In order to practice CI / CD, I set up Jenkins to generate Artifacts and deploy it to the application server.

In this article, as * preparation *, we will manage the web application project written in Java on Github and set it up to build in cooperation with Jenkins.

Artifacts

I think there are generally two ways to deploy, "deploy with war" and "deploy with jar", so I decided to try either pattern.

That is, ** 1. Output the war file and deploy it to the application server. ** ** ** 2. Output the jar file and execute it with the java command. ** ** This time, the former uses Tomcat for the application server, and the latter uses the built-in Tomcat in Spring Boot to execute the jar.

environment

Creating a Java project

In order to prepare two types of Artifacts, a war file and a jar file, we have prepared two Java web application projects that use the Spring Framework.

Click here for Github Repository (https://github.com/kazokmr/JavaWebAppDeploySample)

Pattern 1: Project to output war file

Create a web application using Spring MVC

The base is based on Serving Web Content with Spring MVC on Spring's HP, I tried to prepare a Maven project, but Spring Boot I used it and the packaging was also jar, so I bought it before Spring thorough introduction and Mr. Tada did it at JJUG CCC. I also referred to the hands-on material Spring 5 & Spring Boot 2 hands-on preparation procedure.

Check that the war file is registered in [File> Project Structure> Artifacts] of IntelliJ IDEA, and add it if it does not exist.

Creating Maven Wrapper

I prepared Maven Wrapper so that I can execute mvn command without installing Maven on Jenkins server. mvn -N io.takari:maven:wrapper -Dmaven=3.6.0 For how to make Maven Wrapper (mvnw), refer to here.

Start Tomcat from IntelliJ IDEA on the development machine

Note that it was difficult to start Tomcat in the development environment and check it with a browser

  1. First, I didn't have Tomcat installed on my development PC, so I installed it with homebrew. brew install tomcat
  2. Specify [Tomcat Server> local] from [Run> Edit Configurations> Add New Configuration] of IntelliJ IDEA to display the setting screen.
  3. Specify /usr/local/Cellar/tomcat/9.0.14/libexec for Path of [Application Server].
  4. Enter http: // localhost: 8080 / web-spring-mvc / greeting for [URL].
  5. Add war expanded from the war that can be specified as Artifacts in [Deploy at the server startup] on the Deployment tab.
  6. In the Deployment tab, under Application Context, specify / web-spring-mvc. (Match with ContextPath of URL 4)

When I run Tomcat from IntelliJ IDEA, I go to http: // localhost: 8080 / web-sprig-mvc / greeting and the screen is displayed.

Pattern 2: Project to output jar file

Execute Artifacts developed by Spring Boot application and output by jar with java command. The sample project creates a Maven project based on Building an Application with Spring Boot. Since the output of this sample was Rest API, I edited it to output html (Thymeleaf).

First, create a project using Spring Initializer.

Maven Dependency settings

Below, about the difficult points

--If you do not add spring-boot-starter-thymeleaf, the screen will not be output because Thymeleaf cannot be used. --When using JUnit5, remove junit from the dependency of spring-boot-starter-test (I get an error when trying to specify a version that does not exist from the dependent junit.) --The output jar file starts the service on the JVM server, but [Executable jar](https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html# Set the executable option of spring-boot-maven-plugin to true in order to make it deployment-script-customization-when-it-runs).

Creating a server

The following three virtual environments have been built on the local machine.

  1. Jenkins server
  2. Application server (Tomcat)
  3. Application server (Java only)

To build, start multiple environments at the same time with Vagrant and [ansible-playbook](https://github.com/kazokmr/ansible-java- I'm using deploy-env) to install the necessary software.

Build a Jenkins server

Main installation software

JRE 8 is used as an execution environment for Jenkins operation, and OpenJDK 11 is used for building applications.

To install Jenkins, refer to Jenkins Wiki.

Build tomcat server

Main installation software

For how to write Ansible-playbook, I referred to Sample of ansible head family.

I stumbled on the firewall settings, but I was able to connect by "changing the service start of Tomcat from started to restarted" and "starting firewalld before specifying the port of firewalld".

/roles/tomcat/tasks/main.yml(Excerpt)


- name: Start Tomcat
  service: name=tomcat state=restarted enabled=yes

- name: running firewall daemon
  service: name=firewalld state=restarted

When accessing the tomcat manager screen (/ manager / html) from a local PC, "403 access denied" is displayed, so an error screen or here When I looked at 30/000000), it was said that the initial value was set so that access to the manager screen could only be done from the machine on which Tomcat was installed, so /webapps/manager/META-INF/context. I edited the xml and changed it so that it can be accessed from within the virtual environment network (192.168.33.0/24).

Also, in order to be able to execute the tomcat manager from the command, assign the manager-sciprt role to the execution user in tomcat-users.xml. Reference

Build a JVM server

Make settings to run the Spring Boot application.

  1. Install OpenJDK 11 to be able to execute java commands
  2. Create a jvm user to run the application.
  3. Create a service that runs the Spring Boot application.

At the time of creation, the jar file does not exist yet, so even if you start the service, the status will be Fail.

Start Vagrant

When you run vagrant up, the three environments will start up in sequence, and Ansible will run to install the necessary software and settings. After the installation is complete, you can access the virtual environment with vagrant ssh [hostname].

Jenkins

You can log in to Jenkins by visiting http://192.168.33.10:8080/. image.png

After that, perform the initial setup from the screen and set the path of the installed JDK and Git. image.png

Tomcat

You can access the Tomcat console by visiting http://192.168.33.30:8080/. image.png

If you use the users of roles / tomcat / vars / main.yml included in the sample, you can use the management screen and Script.

JVM

Make sure you see ** openjdk version ** when you access it with vagrant ssh jvm and run java -version. image.png

Also, if you execute # systemctl status app, you can check the execution status of the Spring Boot application service, but the first time you will get an error because there is no jar file. (You can see it by looking at / var / log / message.)

Settings for SSH access to the JVM server

It uses an SSH connection to transfer the jar file from the Jenkins server to the JVM server and run the services of the application. In this sample, I did not create a public key in advance because I started multiple environments with vagrant at the same time, so I passed the key with the command as follows. (Actually, it is better to prepare the public key in advance and register it with the authorized_key module of ansible.)

** Temporarily enable access with password authentication on the JVM server **

  1. Open # vi / etc / ssh / sshd_config and switch" PasswordAuthentication "to yes
  2. Restart ssh with # systemctl restart sshd.
  3. Register the jvm user password with # passwd jvm

** Create an SSH key as a Jenkins user on the Jenkins server and send the public key to the JVM server ** If you execute $ sudo su -s / bin / bash jenkins, you can execute the command as a Jenkins user, so execute the command to create an SSH key.

1.jenkins user's home directory[/var/lib/jenkins]Move to
$ cd ~

2.Make an ssh key
$ ssh-keygen -b 2048 -t rsa -f /var/lib/jenkins/.ssh/id_rsa -N ""

3.Send the public key to the JVM server(You will be asked for the jvm user password when sending)
$ ssh-copy-id -i .ssh/id_rsa.pub [email protected]

4.Connection test
$ ssh -i .ssh/id_rsa [email protected]

5.End
$ exit

** Don't forget to disable password authentication on the JVM server once you have ssh access ** By the way, the home directory of the jvm user created on the JVM server is / var / jvm, so the SSH connection information is stored in /var/jvm/.ssh.

You're ready to build and deploy in Jenkins.

** Maven project (Java + Spring) ** and ** Jenkins server ** and ** deployment environment ** managed by Github are ready! The setting method to execute the build and deploy after this is Tomcat application and SpringBoot application ), So please refer to here.

Recommended Posts

Automatically deploy a Web application developed in Java using Jenkins [Preparation]
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
Automatically deploy Web applications developed in Java using Jenkins [Tomcat application]
[AWS] How to automatically deploy a Web application created with Rails 6 to ECR / ECS using CircleCI ① Preparation [Container deployment]
Try using Java framework Nablarch [Web application]
Deploy a Java web app on Heroku
Role of JSP in Web application [Java]
Java beginner tried to make a simple web application using Spring Boot
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
I tried to make a talk application in Java using AI "A3RT"
How to display a web page in Java
Let's create a super-simple web framework in Java
Deploy Java application developed in IntelliJ IDEA environment to Alibaba Cloud ECS instance
[For beginners] Until building a Web application development environment using Java on Mac OS
How to convert A to a and a to A using AND and OR in Java
Create a JAVA WEB application and try OMC APM
Deploy a Java application developed locally with the Cloud Toolkit to an Alibaba Cloud ECS instance
Let's make a calculator application in Java ~ Display the application window
Web application development environment construction in Java (for inexperienced people)
Try developing a containerized Java web application with Eclipse + Codewind
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
Socket communication with a web browser using Java and JavaScript ②
I tried to implement a buggy web application in Kotlin
Socket communication with a web browser using Java and JavaScript ①
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
Try using RocksDB in Java
Find a subset in Java
Make a rhombus using Java
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-
Let's create a TODO application in Java 4 Implementation of posting function
Collecting client information when an error occurs in a web application
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
How to automatically operate a screen created in Java on Windows
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
3 Implement a simple interpreter in Java
I created a PDF in Java.
Run Java application in Azure Batch
Encrypt using RSA cryptography in Java
Upload a file using Java HttpURLConnection
Access Teradata from a Java application
Create a Java project using Eclipse
A simple sample callback in Java
Using Java with AWS Lambda-Eclipse Preparation
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Get stuck in a Java primer
Web application structure by Java and processing flow in the presentation layer
Deploy an image consisting of Tomcat, Java and MySQL using Ansible and Jenkins
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Creating a java web application development environment with docker for mac part1
Volume of trying to create a Java Web application on Windows Server 2016
How to migrate a web application created in a local docker environment to AWS
The story that the Servlet could not be loaded in the Java Web application
Deploy a Node.js application to an ECS instance using the Cloud Toolkit
Create a java web application development environment with docker for mac part2