A simple and effective CI/CD pipeline using GitHub Actions for Java Spring applications built with Maven. This guide will help you set up an automated workflow that builds your project and deploys the JAR to a remote server using SCP.
🌐 Project URL: http://103.7.4.166:8081/
- Initialize your Java Spring Maven project repository on GitHub.
- Select the branch where you want to enable CI/CD.
Go to your GitHub repository:
Repository → Settings → Secrets and variables → Actions
There, click "New repository secret" and add your credentials. For example:
SERVER_PASSWORDSERVER_USERSERVER_HOST
You can then use them in your GitHub Actions workflow like this:
sshpass -p "${{ secrets.SERVER_PASSWORD }}" scp -o StrictHostKeyChecking=no target/*.jar "${{ secrets.SERVER_USER }}"@"${{ secrets.SERVER_HOST }}":/root/OpsCICD/app.jar- Go to the Actions tab in your repository.
- Use the predefined template:
Publish Java Package with Maven, or - Create your own workflow file manually.
📁 Example workflow:
View the GitHub Actions workflow file
- Copy the content from the provided
github-ci-cd.ymlfile into your workflow directory:
ssh-keygen -t rsa -b 4096 -C "shariful.w3@gmail.com" -f ~/.ssh/id_rsa_account1git clone git@github.com-account1:shariful-w3/microservices-config.gitsudo lsof -i :<PORT>sudo kill -9 $(lsof -t -i:8080)The GitHub Actions workflow is defined in this file:
🔗 .github/workflows/maven-publish.yml
on:
push:
branches:
- mainThis triggers the workflow every time code is pushed to the
mainbranch.
jobs:
build:
runs-on: ubuntu-latest- Checks out the code
- Sets up JDK 17
- Builds the project with Maven (
mvn clean package -DskipTests)
deploy:
needs: build
runs-on: ubuntu-latestRuns only after the build job completes successfully.
-
Setup JDK and Build Again
- Useful if artifacts are needed fresh.
-
Install
sshpass- Enables non-interactive SSH login using password.
-
Securely Copy JAR File
sshpass -p "${{ secrets.SERVER_PASSWORD }}" scp ... -
Kill Old Process on Port 8085
lsof -ti:8085 | xargs kill -9
-
Start the New JAR Application
nohup java -jar ... > app.log &
All logs from the deployed application are stored at:
/root/OpsCICD/github/app.log
Continuously stream the log file in real time:
tail -f /root/OpsCICD/github/app.logQuickly check the latest 1000 lines of the log:
tail -n 1000 /root/OpsCICD/github/app.logSearch for specific log entries:
grep 'some search text' /root/OpsCICD/github/app.logMonitor logs in real-time while filtering for multiple keywords (e.g., user-authentication or USER_WEB_DEV):
tail -f /root/OpsCICD/github/app.log | grep --line-buffered -E "user-authentication|USER_WEB_DEV"- Use
Ctrl+Cto stop any live monitoring (tail -f) session. - You can modify the search terms as per your debugging needs.