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:
- App repository on BitBucket is called "backend".
- I'll be using dedicated user for deployment on Linux server. It will be called also "backend".
- Linux server has already configured remote access via ssh.
- Linux distro on remote server is using 'systemd' for managing services.
- New service we will create will also be named 'backend' for consistency.
- You have root access on remote server.
- You have basic knowledge of Linux (changing folders, creating files etc.).
- Target jar file of SpringBoot application will be stored in /var/backend/backend.jar
- 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:
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.
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.
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...
Best Casinos to Play Roulette for Real Money | Dr. McD
ReplyDeleteBest 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