Skip to content

Commit

Permalink
Merge pull request #12 from cloud-computing-511/chore/#11/cd-setting
Browse files Browse the repository at this point in the history
[CHORE] CD 파이프라인 구축
  • Loading branch information
junseokkim authored Jun 9, 2024
2 parents aec418b + a9434e6 commit 6e14f27
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .github/workflows/ohhanahana-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Ohhanahana CD Pipeline

on:
push:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
# 소스 코드를 체크아웃합니다.
- name: Checkout code
uses: actions/checkout@v4

# Docker Buildx를 설정합니다.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

# AWS ECR에 로그인합니다.
- name: Login to AWS ECR
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}
# 서브모듈을 포함하여 소스 코드를 다시 체크아웃합니다.
- name: Checkout submodule
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.ACTIONS_TOKEN }}

# 서브모듈을 업데이트합니다.
- name: Update submodule
run: |
git submodule update --remote --recursive
# 스크립트 파일에 실행 권한을 부여합니다.
- name: Make deploy script executable
run: chmod +x scripts/deploy.sh

# 기존 이미지를 삭제합니다. 이미지가 없는 경우 무시하고 넘어갑니다.
- name: Delete existing Docker images in ECR
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
IMAGE_TAG=latest
REPO_NAME=${{ secrets.ECR_REPOSITORY }}
REGISTRY_ID=$(aws sts get-caller-identity --query "Account" --output text)
# 기존 이미지가 있는지 확인
IMAGE_IDS=$(aws ecr list-images --repository-name $REPO_NAME --query "imageIds[?imageTag=='$IMAGE_TAG']" --output json)
if [ "$IMAGE_IDS" != "[]" ]; then
aws ecr batch-delete-image --repository-name $REPO_NAME --image-ids imageTag=$IMAGE_TAG
fi
# Docker 이미지를 빌드하고 ECR에 푸시합니다.
- name: Build and push Docker image
env:
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
IMAGE_TAG: latest # 동적으로 태그 설정 가능
run: |
# Active profile을 main으로 지정해서 build
docker build --build-arg SPRING_PROFILES_ACTIVE=main -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
# AWS CodeDeploy를 사용하여 EC2 인스턴스에 배포합니다.
- name: Deploy to EC2 using CodeDeploy
uses: aws-actions/[email protected]
with:
application-name: ohhanahana-app
deployment-group: ohhanahana-bg
deployment-config-name: CodeDeployDefault.OneAtATime
description: "Deploying the latest Docker image"
github-token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:17

ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java", "-jar", "/app.jar"]
10 changes: 10 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/deployment
hooks:
AfterInstall:
- location: scripts/deploy.sh
timeout: 300
runas: ubuntu
14 changes: 14 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# 디렉토리로 이동
cd /home/ubuntu/deployment

# 기존 컨테이너 중지 및 삭제
docker stop $(docker ps -a -q --filter "name=ohhanahana-app") || true
docker rm $(docker ps -a -q --filter "name=ohhanahana-app") || true

# 최신 이미지 가져오기de
docker pull $ECR_REGISTRY/$ECR_REPOSITORY:latest

# 새 컨테이너 실행
docker run -d --name ohhanahana-app -p 8080:8080 $ECR_REGISTRY/$ECR_REPOSITORY:latest

0 comments on commit 6e14f27

Please sign in to comment.