From ecf6b11066342fa47ed7ba08f003991fb0dbb8db Mon Sep 17 00:00:00 2001 From: LEEYUJOON <123285545+L-U-Ready@users.noreply.github.com> Date: Sun, 13 Oct 2024 13:11:10 +0900 Subject: [PATCH] :rocket: deploy files setting (#10) Deploy to EC2 instance using GitHub Action and Dockerfile Related: #9 --- .github/workflows/deploy.yml | 52 ++++++++++++++++++++++++++++++++++++ Dockerfile | 9 +++++++ 2 files changed, 61 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile 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