diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..ab64325 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,52 @@ +name: Deploy Spring Boot to EC2 + +on: + push: + branches: + - main + - develop + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up JDK 21 + uses: actions/setup-java@v1 + with: + java-version: '21' + + - name: Build with Gradle + run: ./gradlew build + + - name: List build/libs contents + run: ls -la build/libs + + - name: Log in to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + + - name: Build Docker image + run: docker build -t ${{ secrets.DOCKER_USERNAME }}/the_monitor:latest . + + - name: Push Docker image + run: docker push ${{ secrets.DOCKER_USERNAME }}/the_monitor:latest + + deploy: + runs-on: ubuntu-latest + needs: build + + steps: + - name: Connect to EC2 and deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ec2-user + key: ${{ secrets.EC2_PRIVATE_KEY }} + script: | + docker pull ${{ secrets.DOCKER_USERNAME }}/the_monitor:latest + docker stop the_monitor-container || true + docker rm the_monitor-container || true + docker run -d -p 8080:8080 --name the_monitor-container ${{ secrets.DOCKER_USERNAME }}/the_monitor:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..054e893 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:21-jdk-slim + +WORKDIR /app + +COPY build/libs/the_monitor-0.0.1-SNAPSHOT.jar /app/the_monitor.jar + +EXPOSE 8080 + +ENTRYPOINT ["java", "-jar", "/app/the_monitor.jar"] \ No newline at end of file