Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M3-127 백엔드 도커 CI/CD 구축 #2

Merged
merged 10 commits into from
Jun 22, 2024
52 changes: 52 additions & 0 deletions .github/workflows/cd_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Backend Dev Server CD

on:
push:
branches: [ "develop" ]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: JDK 17 설치
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'

- name: env 설정
run: |
echo "DB_URI=${{ secrets.DB_URI }}" >> .env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "SPRING_PROFILES_ACTIVE=dev" >> .env

- name: gradlew에 실행 권한 부여
run: chmod +x ./gradlew

- name: 프로젝트 빌드
run: ./gradlew clean bootjar

- name: 압축
run: zip -r ./ground_flip.zip .

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION}}

- name: S3에 업로드
run: aws s3 cp ground_flip.zip s3://ground-flip-dev/deploy/ground_flip.zip --region ap-northeast-2

- name: Code Deploy 로 배포
run: >
aws deploy create-deployment --application-name ground-flip-dev
--deployment-config-name CodeDeployDefault.AllAtOnce
--deployment-group-name ground_flip_dev
--s3-location bucket=ground-flip-dev,bundleType=zip,key=deploy/ground_flip.zip
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: BackEnd CI

on:
pull_request:
branches:
- develop

jobs:
build_and_test:
runs-on: ubuntu-20.04

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: JDK 17 설치
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5

- name: Grant execute permission to gradlew
run: chmod +x ./gradlew

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

- name: Test with Gradle
run: ./gradlew test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/

*.env
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:17-jdk
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
18 changes: 18 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 0.0
os: linux

files:
- source: /
destination: /home/ubuntu/ground_flip
overwrite: yes
permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu

hooks:
AfterInstall:
- location: deploy.sh
timeout: 500
runas : root
26 changes: 26 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

APP_NAME="ground_flip"
REPOSITORY=/home/ubuntu/ground_flip

echo "> Check the currently running container"
CONTAINER_ID=$(docker ps -aqf "name=$APP_NAME")
pwd
ls
if [ -z "$CONTAINER_ID" ];
then
echo "> No such container is running."
else
echo "> Stop and remove container: $CONTAINER_ID"
docker stop "$CONTAINER_ID"
docker rm "$CONTAINER_ID"
fi

echo "> Remove previous Docker image"
docker rmi "$APP_NAME"

echo "> Build Docker image"
docker build -t "$APP_NAME" "$REPOSITORY"

echo "> Run the Docker container"
docker run -d -p 8080:8080 --env-file /home/ubuntu/ground_flip/.env --name "$APP_NAME" "$APP_NAME"
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class GroundFlipApplicationTests {

@Test
Expand Down
Loading