Skip to main content

Deploy java maven application from Bitbucket to ssh linux server automaticaly

Currently I'm working on a spring boot project.  The code is hosted on BitBucket GIT repository.
I deploy  target application on Ubuntu Linux VPS server.
At the beginning I was building my result jar manually, copying it manually to server, and restarting a service also manually using ssh shell.
After some time I noticed, that it is quite annoying thing, so I decided to look around what other possibilities I have.

It turned out that BitBucket has a solution ready to use, it just needs a little configuration.
I'll describe full configuration process of Linux server and BitBucket account.
At the end there will be possibility to deploy SpringBoot application from BitBucket to remote ssh server with just one click.

Assumptions:
  1. App repository on BitBucket is called "backend".
  2. I'll be using dedicated user for deployment on Linux server. It will be  called also "backend".
  3. Linux server has already configured remote access via ssh.
  4. Linux distro on remote server is using  'systemd' for managing services.
  5. New service we will create will also be named 'backend' for consistency.
  6. You have root access on remote server.
  7. You have basic knowledge of Linux (changing folders, creating files etc.).
  8. Target jar file of SpringBoot application will be stored in /var/backend/backend.jar
  9. Maven build in SpringBoot project is configured to create executable jar.
Let's begin.

First step is to create new user called "backend" on remote server.

Login to remote server via ssh, and execute command:



enter password when prompted. It will give you root access on the console.
then execute command:

We have just created new user which will be used for running our SpringBoot application.
For security reasons I do not want this user to be able to login with password, so I'm creating it without password set.

Now lets prepare a systemd service to run our application.
Create folder for application and make user "backend" an owner:
Go to folder
/etc/systemd/system
and create file
backend.service
with that content:

then execute:
to reload services configuration. Now service "backend" should be available in system. We have to allow user "backend" to start and stop that service.
Edit file /etc/sudoers and in section " User privilege specification"
just under "root" entry, add lines:

Now user "backend" should be able to stop and start "backend" service without password
Make sure You have "policykit-1" installed.
If not, execute:

Now let's configure server to allow login for user "backend" without password, using ssh private key.
Execute command:

You will be prompted for password, but you can simply press enter twice.
Keys will be generated in /root/.ssh/ folder

id_rsa - is the private key
id_rsa.pub - is the public key

then execute commands:

It creates ssh folder for user "backend", moves generated public key to that folder and changes owner of folder to user and group "backend".
Remember to keep the private key safe.

We have all Linux server configuration done, so now we have to configure BitBucket.
Log into Your BitBucket account and go to the repository with the java application.
Open "settings", and "Enable Pipelines":
Click "SSH keys"  and  "Use my own keys":
Paste the private key and public key in proper fields:
Fill remote server hostname or IP address into "Host Address" and click "Fetch" to get server fingerprint. When fingerprint appears, click "Add Host":

At this moment BitBucket builds should be able to log into remote server using user "backend" and SSH key, without password.

The only thing left to do is to create dedicated pipeline to automaticaly build project and deploy it to server.
Go to "Pipelines" option, and select "Java (Maven)":
You should see editor with pipeline script to be executed at build or deployment:
Paste that script into editor:

replace all occurrences of "your_project_name" with proper project name and "your_remote_host_name" with proper remote host name. Click "Commit file".

Now every commit to the repository will automatically start maven build.
Deployment to server is set to "manual" (trigger: manual) in pipeline file. 

All builds will be visible at "Pipelines" menu:

Clicking on selected build will allow to run deployment process manually.
And that's it...
For now it is possible to deploy application with one click.
Remember that default free BitBucket plan gives 50 minutes of build time  for free every month.
If you have many commits to repo you will run out of free time very fast. Unfortunately first step in pipeline config cannot be manual, so there is no possibility to turn off automatic builds.
However you can "hack" it a little and do not make "mvn clean install" in first step, but for example "ls" command. Automatic build will trigger every commit, but it will be very short. Manual deploy trigger also makes "mvn clean install", so the process will be valid.

I hope it will help you a little, feel free to comment...

Comments

  1. Best Casinos to Play Roulette for Real Money | Dr. McD
    Best Casinos to Play Roulette 성남 출장안마 for 정읍 출장마사지 Real Money 제천 출장안마 · 1. Bovada – Best Casino to Play Roulette · 2. 경기도 출장마사지 InterTops – Best Casino to Play Roulette · 3. Red Dog – Best 강릉 출장마사지 Casino

    ReplyDelete

Post a Comment

Popular posts from this blog

Angular 5, SpringBoot 2, Oauth2 and IMPLICIT flow - silent token refresh

Recently I've been working on simple web application for managing students and users. Application will be published on the internet, so it needs proper access control. I wanted to learn some new stuff, so I decided to use Angular5 with Oauth2 authentication. I didn't want to use any options as "Login with Facebook", or "Login with Google". I wanted my Spring Boot app to work as the authentication server and the resource server. I read a little abouth Oauth2 and different flows possible, and it turns out, that preffered flow to use with web application is IMPLICIT flow. Implicit flow uses only one token. It doesn't have a refresh token, as it could be overtaken by an attacker. Access token has defined validity period. In other flows, where refresh token exists it is used to get another access token when the first one expires. In theory in implicit flow user should just log again, but forcing a user to log in during active session is not an option. ...

Control OpenHab using Telegram Messenger

Recently, I've been trying to discover a way to control my OpenHab instance using Google Assistant. It is rather easy to do this using  IFTTT , but only when You use OpenHab in a cloud. I prefer using private instalation of OpenHab in my LAN, so that was not an option for me. I was searching for another solution, but only app supported by IFTTT which might be usefull form me was Telegram messenger. The only problem is, that OpenHab does not support Telegram controling by default. I google'd a little and it turned out that Telegram has a great public API with good Java support, so I decided to write a simple Spring Boot application as a bridge between Telegram Bot and OpenHab instance. You can find source code of my application on my GitHub account: https://github.com/aogorek/openhabtelegrambot In order to run it there are few steps required: 1. Create Bot in Telegram Messenger First You have to create Your own bot to communicate with. You need to contact @BotFather...

Raspberry PI and additional GPIO pins on several mcp23017 chips and a transistor switch

Some time ago I came to the conclusion that the home automation control system I made before was too complicated. I've been using a Raspberry PI + arduino + ethernet shield + additional connector board.  Arduino together with ethernet shield sometimes causes problems, especially after power outages. It has problems with restarting and requires manual restart. So this was the element I wanted to get rid of. It seems that the raspberry PI itself should definitely be enough. The only limitation is the number of GPIO pins and current efficiency. To avoid that problem I have already started designing the PCB which, when connected to raspberry, will provide us with additional pins and the ability use more power consumng devices. The easiest way to gain additional pins is to use the mcp23017 chip. It uses I2C bus to connect to the raspberry PI. Each chip will provide us with 16 additional pins. The chips can be connected together in chain, so you can have even more pins. I calculated t...