-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploy to EC2 instance using GitHub Action and Dockerfile Related: #9
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |