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
Java 1.8 Jenkins 2.891 Git 1.8.3.1 Github CentOS 7.2
Create a new job Enter the Job name from Create, select Build Freestyle Project, and click OK.
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 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.
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.
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.
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_rsaand
config, change the owner to jenkins, create
/var/lib/jenkins/.ssh/known_hosts` and change the permissions and owner to jenkins Should be solved.
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
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!
This time, I had a senior in the laboratory help me. Special thanks @cappyzawa, Shotaro! !! !! !!
Organizing Jenkins and GitHub webhook integration | Aiming developer blog
Recommended Posts