Skip to content

Commit 6e14f27

Browse files
authored
Merge pull request #12 from cloud-computing-511/chore/#11/cd-setting
[CHORE] CD 파이프라인 구축
2 parents aec418b + a9434e6 commit 6e14f27

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

.github/workflows/ohhanahana-cd.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
6+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
7+
8+
name: Ohhanahana CD Pipeline
9+
10+
on:
11+
push:
12+
branches: [ "main" ]
13+
14+
jobs:
15+
build:
16+
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
# 소스 코드를 체크아웃합니다.
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
# Docker Buildx를 설정합니다.
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v1
27+
28+
# AWS ECR에 로그인합니다.
29+
- name: Login to AWS ECR
30+
env:
31+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
run: |
34+
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}
35+
36+
# 서브모듈을 포함하여 소스 코드를 다시 체크아웃합니다.
37+
- name: Checkout submodule
38+
uses: actions/checkout@v4
39+
with:
40+
submodules: true
41+
token: ${{ secrets.ACTIONS_TOKEN }}
42+
43+
# 서브모듈을 업데이트합니다.
44+
- name: Update submodule
45+
run: |
46+
git submodule update --remote --recursive
47+
48+
# 스크립트 파일에 실행 권한을 부여합니다.
49+
- name: Make deploy script executable
50+
run: chmod +x scripts/deploy.sh
51+
52+
# 기존 이미지를 삭제합니다. 이미지가 없는 경우 무시하고 넘어갑니다.
53+
- name: Delete existing Docker images in ECR
54+
env:
55+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
56+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
57+
run: |
58+
IMAGE_TAG=latest
59+
REPO_NAME=${{ secrets.ECR_REPOSITORY }}
60+
REGISTRY_ID=$(aws sts get-caller-identity --query "Account" --output text)
61+
# 기존 이미지가 있는지 확인
62+
IMAGE_IDS=$(aws ecr list-images --repository-name $REPO_NAME --query "imageIds[?imageTag=='$IMAGE_TAG']" --output json)
63+
if [ "$IMAGE_IDS" != "[]" ]; then
64+
aws ecr batch-delete-image --repository-name $REPO_NAME --image-ids imageTag=$IMAGE_TAG
65+
fi
66+
67+
# Docker 이미지를 빌드하고 ECR에 푸시합니다.
68+
- name: Build and push Docker image
69+
env:
70+
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
71+
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
72+
IMAGE_TAG: latest # 동적으로 태그 설정 가능
73+
run: |
74+
# Active profile을 main으로 지정해서 build
75+
docker build --build-arg SPRING_PROFILES_ACTIVE=main -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
76+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
77+
78+
# AWS CodeDeploy를 사용하여 EC2 인스턴스에 배포합니다.
79+
- name: Deploy to EC2 using CodeDeploy
80+
uses: aws-actions/[email protected]
81+
with:
82+
application-name: ohhanahana-app
83+
deployment-group: ohhanahana-bg
84+
deployment-config-name: CodeDeployDefault.OneAtATime
85+
description: "Deploying the latest Docker image"
86+
github-token: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM openjdk:17
2+
3+
ARG JAR_FILE=build/libs/*.jar
4+
COPY ${JAR_FILE} app.jar
5+
6+
ENTRYPOINT ["java", "-jar", "/app.jar"]

appspec.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 0.0
2+
os: linux
3+
files:
4+
- source: /
5+
destination: /home/ubuntu/deployment
6+
hooks:
7+
AfterInstall:
8+
- location: scripts/deploy.sh
9+
timeout: 300
10+
runas: ubuntu

scripts/deploy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# 디렉토리로 이동
4+
cd /home/ubuntu/deployment
5+
6+
# 기존 컨테이너 중지 및 삭제
7+
docker stop $(docker ps -a -q --filter "name=ohhanahana-app") || true
8+
docker rm $(docker ps -a -q --filter "name=ohhanahana-app") || true
9+
10+
# 최신 이미지 가져오기de
11+
docker pull $ECR_REGISTRY/$ECR_REPOSITORY:latest
12+
13+
# 새 컨테이너 실행
14+
docker run -d --name ohhanahana-app -p 8080:8080 $ECR_REGISTRY/$ECR_REPOSITORY:latest

0 commit comments

Comments
 (0)