Skip to content

Commit

Permalink
Merge pull request #141 from eod940/050-FEAT-CD
Browse files Browse the repository at this point in the history
feat: [050-FEAT-CD] CD 기능 구축
  • Loading branch information
eod940 committed Mar 5, 2024
2 parents edd03e5 + 3b9e297 commit 6882b74
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
63 changes: 61 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ on:
pull_request:
branches: [ "master" ]

env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_ID }}/tweaver
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }} # S3 버킷 이름
AWS_CODE_DEPLOY_NAME: valuewith-codedeploy # CodeDeploy 애플리케이션 이름
AWS_CODE_DEPLOY_GROUP: valuewith-codedeploy-group # CodeDeploy 배포 그룹 이름


permissions:
contents: read

Expand Down Expand Up @@ -50,7 +58,7 @@ jobs:
# 5. Docker image 빌드하기
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKERHUB_ID }}/tweaver
run: docker build . --file Dockerfile --tag ${{ env.IMAGE_NAME }}:${{ secrets.MAJOR }}.${{ secrets.MINOR }}

# 6. Docker 로그인 하기
- name: Docker login
Expand All @@ -61,4 +69,55 @@ jobs:

# 7. Docker 푸시하기
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_ID }}/tweaver
run: docker push ${{ env.IMAGE_NAME }}:${{ secrets.MAJOR }}.${{ secrets.MINOR }}

##### 배포

# 8. Docker 이미지 이름을 image.txt 파일에 쓰기
- name: Write Docker image name to txt file
run: echo "${{ env.IMAGE_NAME }}:${{ secrets.MAJOR }}.${{ secrets.MINOR }}" > image.txt

# 9. CodeDeploy 배포를 위한 관련 파일 압축
- name: Create zip file for AWS CodeDeploy
run: mkdir ${{ env.AWS_CODE_DEPLOY_NAME }} && cp -r appspec.yml image.txt scripts ${{ env.AWS_CODE_DEPLOY_NAME }}

# 10. AWS 설정
- name: AWS Configure
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.IAM_ACCESS }}
aws-secret-access-key: ${{ secrets.IAM_SECRET_ACCESS }}
aws-region: ap-northeast-2

# 11. AWS S3로 배포 파일 업로드
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.AWS_CODE_DEPLOY_NAME }} \
--s3-location s3://${{ env.AWS_S3_BUCKET_NAME }}/codedeploy/$GITHUB_SHA.zip \
--ignore-hidden-files \
--source ${{ env.AWS_CODE_DEPLOY_NAME }}
# 12. AWS EC2 CodeDeploy 배포 요청
- name: Delpoy to AWS EC2
run: |
aws deploy create-deployment \
--application-name ${{ env.AWS_CODE_DEPLOY_NAME }} \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} \
--description "Deploy artscope" \
--s3-location bucket=$AWS_S3_BUCKET_NAME,key=codedeploy/$GITHUB_SHA.zip,bundleType=zip
# 13. 자동 버전 관리
- name: Autoincrement a new minor version
run: |
echo "NEW_MINOR_VERSION=$((${{ secrets.MINOR }}+1))" >> $GITHUB_ENV
# 14. 마이너 버전 업데이트
- name: Update Minor version
uses: hmanzur/actions-set-secret@v2.0.0
with:
name: 'MINOR'
value: ${{ env.NEW_MINOR_VERSION }}
repository: ${{ secrets.REPO }}
token: ${{ secrets.REPO_ACCESS_TOKEN }}
24 changes: 24 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/deploy/
overwrite: yes

permissions:
- object: /home/ubuntu/deploy/
pattern: "**"
owner: root
group: root
mode: 777

hooks:
ApplicationStop:
- location: scripts/stop.sh
timeout: 60
runas: root

ApplicationStart:
- location: scripts/start.sh
timeout: 60
runas: root
43 changes: 43 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
IMAGE_FILE_PATH="/home/ubuntu/deploy/image.txt"
IMAGE_NAME=$(cat "$IMAGE_FILE_PATH")
CONTAINER_ENV_PATH="/home/ubuntu/trip.env"
SERVICE_NAME=tweaver

# Docker Compose YAML을 새로운 도커 버전으로 작성해서 저장
echo "version: '3.9'
services:
tweaver:
build:
context: .
dockerfile: Dockerfile
environment:
- TZ=Asia/Seoul
ports:
- 8080:8080
image: ${IMAGE_NAME}
env_file:
- ${CONTAINER_ENV_PATH}
depends_on:
- redis
redis:
image: redis
command: redis-server --port 6379
container_name: tweaver-redis
hostname: tweaver-redis
labels:
- "name=tweaver-redis"
- "mode=standalone"
ports:
- 6379:6379
networks:
default:
external: true
name: tweaver-network" > docker-compose.yml


# 새로운 도커 컨테이너 실행
echo "IMAGE_NAME: $IMAGE_NAME 도커 실행"
docker compose up -d $SERVICE_NAME
11 changes: 11 additions & 0 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CONTAINER_NAME=tweaver-backend

# 도커 컨테이너가 있는지 확인 (-a 옵션으로 정지된 컨테이너도 확인)
RUNNING_CONTAINER_ID=$(docker ps -aq --filter "name=$CONTAINER_NAME")
echo "실행중인 컨테이너 ID: $RUNNING_CONTAINER_ID"

# 이전 도커 컨테이너 종료
if [ -n "$RUNNING_CONTAINER_ID" ]; then
echo "이전 도커 컨테이너 종료 및 삭제합니다"
docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME
fi

0 comments on commit 6882b74

Please sign in to comment.