Automatically deploy applications by linking Github and Jenkins.

Automatically deploy applications by linking Github and Jenkins.

Introduction

Currently, I am making a system to store data in a DB using Spring Boot in my graduation thesis. I would like to develop the unfinished functions locally and proceed in parallel while operating the system by putting it on the server when the system is completed so that the data input itself can be helped by someone other than myself. So, if you can push to Github when the function is completed and automatically deploy it to the server with it as a trigger, the user (person who helps) the system with the new function (actually it stops for a few seconds) without stopping the operation system ) Can be used. So, from local, when I git push to the private repository of git hub, git hub webhooks sends an http request to jenkins, and with that as a trigger, jenkins git clones and builds and executes the spring application. Note: This is the article on the 15th day of the Spring / Spring Boot Advent Calendar. Lol

Development environment

Java 1.8 Jenkins 2.891 Git 1.8.3.1 Github CentOS 7.2

Setting method

jenkins settings

Create new job

Create a new job Enter the Job name from Create, select Build Freestyle Project, and click OK. スクリーンショット 2017-12-15 10.46.12.png

Receive URL setting of Http request

Then the setting screen will appear, so check Build remotely at the build trigger and enter the authentication token. The authentication token can be anything. For example, if the authentication token is Secret, JENKINS_URL / job / New / build? Token = Secret will be the place where webhooks will send http requests. The JENKINS_URL part is the URL where your jenkins works. For the time being, press the save button once to save.

Github Webhooks settings

Github has a feature called Webhooks that sends an HTTP request to a specified URL when a specific event occurs. I used it this time. Open the Github repository of the system you want to deploy, open Setting, and select Add webhooks from Webhooks. スクリーンショット 2017-12-15 12.03.20.png

Describe the URL that was set in Jenkins earlier and receives the HTTP request in the Payload URL. It is necessary to add login user information when describing in the URL. URL is http://[USER_ID]:[API_TOKEN]@[JENKINS_HOST]/job/[JOB_NAME]/[build|buildWithParameters]?token=[TOKEN_NAME] User_ID and API token can be seen by going to jenkins → jenkins management → user management → click the setting icon → API token display. For Content type, select the default ʻapplication / x-www-form-urlencoded`. Secret is blank. In the column of Which events would you like to trigger this webhook ?, Tory wants to make the push event this time. Check Just the push event Then press Add webhooks and if there is a green check in the Recent Deliveries column, the setting is complete. If you get a 403 error, you can uncheck the jenkins csrf token. Not good for security.

Allow jenkins users to ssh to github.

Create public and private keys for jenkins users

You should have a jenkins user when you installed jenkins. The home directory of the jenkins user is / var / lib / jenkins. Create an .ssh directory here and generate the public key and private key for authentication there.

$ cd /var/lib/Jenkins
$ sudo mkdir .ssh
$ sudo ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /var/lib/jenkins/.ssh/id_rsa
Enter passphrase (empty for no passphrase):(enter)
Enter same passphrase again:(enter)

This will generate the key.

Register the public key of the jenkins user in your Github account

スクリーンショット 2017-12-15 13.01.03.png Add the key information output by `cat .ssh / id_rsa.pub` to the github user's key. To do this, right-click the user icon on the top right of github and click Setting → SSH and GPG keys. Enter the title appropriately with new ssh key, Copy and paste the key information output by `cat .ssh / id_rsa.pub` to the key. Click Add ssh key to complete the registration. Once this is done, let's ssh it to github once. `sudo -u jenkins ssh -T [email protected]` Hi ~~~~~! You've successfully authenticated, but GitHub does not provide shell access. If you see, ssh is successful. If you can't, access it on port 443. `ssh sudo -u jenkins ssh -T -p 443 [email protected]` If ssh succeeds with this, create a config file in `/var/lib/jenkins/.ssh`.

Create /var/lib/jenkins/.ssh/config. (People who couldn't ssh earlier)

Execute the following command.

$ cd /var/lib/jenkins/.ssh  
$ sudo vi config

Contents of config below

.ssh/config


Host github.com
    User git
    Hostname ssh.github.com
    Port 443

Set this

$ sudo -u jenkins ssh -T [email protected]

And earlier Hi ~ ~ ~ ~! You've successfully authenticated, but GitHub does not provide shell access. If you can't, change the permissions of ʻid_rsaandconfig, change the owner to jenkins, create /var/lib/jenkins/.ssh/known_hosts` and change the permissions and owner to jenkins Should be solved.

Settings for jenkins again

Set the Job you created earlier. Click Settings in the upper left. Click Add Build Procedure in the Build column and click Run Shell. Add the following shell there

#Stop the spring application by deleting the previous remaining spring process.
ps aux | grep java | [Name of jar after build].jar | grep -v sudo | awk '{print "kill -9 " $2}' | sudo  sh

#Change to the directory where you want to deploy the spring application. This time/var/webapp/spring/I decided to deploy to (privileges, owners, etc. have been set)
cd /var/webapp/spring/

#Remove previously git cloned application
sudo rm -rf [Repository name]

#git clone.
git clone [Url for repository ssh]

#Move to the root directory of the cloned repository
cd [Repository name]

#Build with Maven. When you build, you will have an executable jar file of spring in the target directory in the root directory of your project.
sudo ./mvnw clean package

#Executing the jar file&Is running in the background.--server.You can run it on any port you like.
sudo java -jar ./target/[Name of jar after build].jar --server.port=8080  &

Save it with this, and the settings are complete. If you run the jar in the background, you will not be able to see the log, so if you write the following description in application.proparties of spring-boot, a log file called /logs/spring.log will be created under the root directory of the project and the log will be spit out there.

/src/main/resource/application.proparties


logging.file=./logs/spring.log 

test

Make some changes to the project and git push to see it. Then the jenkins job will start, and if the Job succeeds, it will be completed. It's OK if it is executed by typing the URL of your application in the browser!

in conclusion

This time, I had a senior in the laboratory help me. Special thanks @cappyzawa, Shotaro! !! !! !!

References

Organizing Jenkins and GitHub webhook integration | Aiming developer blog

Recommended Posts

Automatically deploy applications by linking Github and Jenkins.
Automatically deploy Web applications developed in Java using Jenkins [Tomcat application]
Changed comments automatically generated by Xcode and AppCode
Automatically deploy to WildFly with Jenkins when SVN commits
How to check CircleCI code and automatically deploy to Heroku