-
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.
ci: Dockerfile, .github/workflows/ci-cd.yml 추가
- Loading branch information
Showing
2 changed files
with
84 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,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 |
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,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"] |