Skip to content

Commit

Permalink
cd: 배포 이전을 위한 cd 파일 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimday0326 committed May 20, 2024
1 parent d4b5a61 commit fe969b3
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 96 deletions.
192 changes: 96 additions & 96 deletions .github/workflows/cd-prod.yml
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
name: Java CD with Gradle in Tiki-Taza

on:
workflow_dispatch:
push:
branches: [ "main" ]

env:
AWS_REGION: ap-northeast-2
ECR_URI: 975050255579.dkr.ecr.ap-northeast-2.amazonaws.com/tikitaza
ECS_CLUSTER: tikitaza-cluster
ECS_SERVICE: tikitaza-service-prod
ECS_TASK_DEFINITION: .aws/task-definition-prod.json
CONTAINER_NAME: tikitaza-container-prod

permissions:
contents: read

jobs:
deploy:
name: prod-deploy
runs-on: ubuntu-latest
environment: v1.0.0
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

## create application.yml
- name: make application-secret.yml
run: |
mkdir -p ./api/src/main/resources
touch ./api/src/main/resources/application-secret.yml
shell: bash
- name: deliver application-secret.yml
run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
shell: bash

## gradlew 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew

## gradle build
- name: Build with Gradle
run: ./gradlew clean build

## 현재 시간 가져오기
- name: Get current time
uses: josStorer/get-current-time@v2
id: current-time
with:
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"

- name: Show Current Time
run: echo "CurrentTime=${{ steps.current-time.outputs.formattedTime }}"

## AWS에 로그인
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

## ECR에 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push image to Amazon ECR
id: build-image
run: |
docker build -t tikitaza:${{ steps.current-time.outputs.formattedTime }} .
docker tag tikitaza:${{ steps.current-time.outputs.formattedTime }} ${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod
docker push ${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod
echo "image=${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
#name: Java CD with Gradle in Tiki-Taza
#
#on:
# workflow_dispatch:
# push:
# branches: [ "main" ]
#
#env:
# AWS_REGION: ap-northeast-2
# ECR_URI: 975050255579.dkr.ecr.ap-northeast-2.amazonaws.com/tikitaza
# ECS_CLUSTER: tikitaza-cluster
# ECS_SERVICE: tikitaza-service-prod
# ECS_TASK_DEFINITION: .aws/task-definition-prod.json
# CONTAINER_NAME: tikitaza-container-prod
#
#permissions:
# contents: read
#
#jobs:
# deploy:
# name: prod-deploy
# runs-on: ubuntu-latest
# environment: v1.0.0
# steps:
# - uses: actions/checkout@v4
# - name: Set up JDK 17
# uses: actions/setup-java@v4
# with:
# java-version: '17'
# distribution: 'temurin'
#
# ## create application.yml
# - name: make application-secret.yml
# run: |
# mkdir -p ./api/src/main/resources
# touch ./api/src/main/resources/application-secret.yml
# shell: bash
# - name: deliver application-secret.yml
# run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
# shell: bash
#
# ## gradlew 권한 부여
# - name: Grant execute permission for gradlew
# run: chmod +x gradlew
#
# ## gradle build
# - name: Build with Gradle
# run: ./gradlew clean build
#
# ## 현재 시간 가져오기
# - name: Get current time
# uses: josStorer/get-current-time@v2
# id: current-time
# with:
# format: YYYY-MM-DDTHH-mm-ss
# utcOffset: "+09:00"
#
# - name: Show Current Time
# run: echo "CurrentTime=${{ steps.current-time.outputs.formattedTime }}"
#
# ## AWS에 로그인
# - name: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: ${{ env.AWS_REGION }}
#
# ## ECR에 로그인
# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v2
#
# - name: Build, tag, and push image to Amazon ECR
# id: build-image
# run: |
# docker build -t tikitaza:${{ steps.current-time.outputs.formattedTime }} .
# docker tag tikitaza:${{ steps.current-time.outputs.formattedTime }} ${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod
# docker push ${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod
# echo "image=${{ env.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}-prod" >> $GITHUB_OUTPUT
#
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: task-def
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.build-image.outputs.image }}
#
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# with:
# task-definition: ${{ steps.task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: true
112 changes: 112 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Java CD with Gradle in Tiki-Taza

on:
workflow_dispatch:
push:
branches: [ "main" ]

permissions:
contents: read

jobs:
build-and-push-docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## gradle 캐싱
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
## create application.yml
- name: make application-secret.yml
run: |
mkdir -p ./api/src/main/resources
touch ./api/src/main/resources/application-secret.yml
shell: bash
- name: deliver application-secret.yml
run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
shell: bash

# 빌드 및 테스트 단계.
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# gradle build
- name: Build with Gradle
run: ./gradlew build -x test

# push 하기 위해 로그인
- name: Docker Hub 로그인
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_ID }}
password: ${{ secrets.DOCKER_PASSWORD }}

#도커 빌드 & 이미지 push
- name: Docker build & Push
run: |
docker build -f Dockerfile -t ${{ secrets.DOCKER_ID }}/tiki-taza .
docker push ${{ secrets.DOCKER_ID }}/tiki-taza
# Docker 파일을 EC2 서버에 배포
- name: Deploy to Prod
uses: appleboy/ssh-action@master
id: deploy-prod
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_PEM_KEY }}
port: 22
script: |
if [ ! -z "$(docker ps -q)" ]; then
docker stop $(docker ps -q)
fi
if [ ! -z "$(docker ps -aq)" ]; then
docker rm $(docker ps -aq)
fi
docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_PASSWORD }}
docker pull ${{ secrets.DOCKER_ID }}/tiki-taza
docker pull wurstmeister/zookeeper
docker network create tikitaza
docker run -d \
--name tiki-taza \
--network=tikitaza \
-p 80:8080 \
-e TZ=Asia/Seoul \
${{ secrets.DOCKER_ID }}/tiki-taza
docker run -d \
--name zookeeper \
--network=tikitaza \
-p 2181:2181 \
wurstmeister/zookeeper
# Kafka 컨테이너 실행
docker run -d \
--name kafka \
--network=tikitaza \
-p 9092:9092 \
-e KAFKA_ADVERTISED_HOST_NAME=${{ secrets.EC2_HOST }}\
-e KAFKA_ADVERTISED_PORT=9092 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
wurstmeister/kafka
docker system prune -f

0 comments on commit fe969b3

Please sign in to comment.