This approach will deploy a spring boot MVC application to Tomcat in EC2 location. We will use two built-in mechanisms of Github
and
<aside> đĄ Disclaimer: This approach is not an ideal approach for the production environment as CI and production server are in the same location here. This is why the tomcat server is stopped before the maven builds start, to avoid low configuration EC2 freeze conditions. Use it only for personal projects.
</aside>
Sample maven.yml
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: <https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven>
name: Java CI with Maven
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: stop tomcat
run: sudo systemctl stop tomcat & rm -rf _work/prod-mnhmilu-server.com/prod-mnhmilu-server.com/demo/target/myblog.war
- name: Build with Maven
run: mvn -B package --file demo/pom.xml
- name: Remove old myblog
run: sudo rm -r /opt/tomcat/webapps/myblog
- name: Copy to tomcat folder and start tomcat
run: sudo cp -rf /home/ubuntu/actions-runner/_work/prod-mnhmilu-server.com/prod-mnhmilu-server.com/demo/target/myblog.war /opt/tomcat/webapps & sudo systemctl start tomcat