Skip to content

Commit

Permalink
ci: Dockerfile, .github/workflows/ci-cd.yml 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamji34 committed Sep 5, 2024
1 parent 42c1dde commit 176fce3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI/CD Pipeline

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'

- name: Grant execute permission for Gradle
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build -x test

- name: Build Docker Image
run: |
docker build -t ${{ secrets.IMAGE_NAME }} .
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Push Docker Image to Docker Hub
run: |
docker push ${{ secrets.IMAGE_NAME }}
- name: Deploy to Backend EC2 through Bastion Host
env:
BASTION_HOST: ${{ secrets.BASTION_HOST }}
TARGET_HOST: ${{ secrets.EC2_HOST }}
USER: ${{ secrets.EC2_USER }}
SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
IMAGE_NAME: ${{ secrets.IMAGE_NAME }}
run: |
echo "$SSH_KEY" | tr -d '\r' > hackathon.pem
chmod 600 hackathon.pem
ssh -o StrictHostKeyChecking=no -i hackathon.pem $USER@$BASTION_HOST << 'EOF'
ssh -o StrictHostKeyChecking=no -i hackathon.pem $USER@${{ secrets.EC2_HOST }} << 'INNER_EOF'
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
docker pull ${{ secrets.IMAGE_NAME }}
if [ $(docker ps -a -q -f name= gratitude-container) ]; then
docker stop gratitude-container || true
docker rm gratitude-container || true
fi
docker run -d --name gratitude-container -p 8080:8080 ${{ secrets.IMAGE_NAME }}
INNER_EOF
EOF
rm hackathon.pem
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Gradle 빌드를 위한 베이스 이미지
FROM gradle:7.5-jdk17 AS builder

WORKDIR /app
COPY . .

RUN gradle build -x test

# 실행을 위한 새로운 베이스 이미지
FROM openjdk:17-jdk-slim

WORKDIR /app
COPY --from=builder /app/build/libs/gratitude-server-0.0.1.jar app.jar
COPY --from=builder /app/src/main/resources/application.yml /app/resources/

EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

0 comments on commit 176fce3

Please sign in to comment.