Skip to content

Commit

Permalink
chore: CD 세팅
Browse files Browse the repository at this point in the history
  • Loading branch information
ymkim97 committed Jan 2, 2025
1 parent 061d31e commit 265c3c8
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 1 deletion.
151 changes: 151 additions & 0 deletions .github/workflows/cd-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: TnT CD for dev with Gradle, Docker, AWS

on:
push:
branches:
- develop

jobs:
move-files:
name: move-files
runs-on: ubuntu-latest
steps:
- name: Checkout with Submodule
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.SUBMODULE_PAT }}
fetch-depth: 0

- name: Public IP
uses: haythem/[email protected]

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

- name: Github Actions IP 보안그룹 추가
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
- name: AWS EC2 Create Directory
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_DEV_HOST }}
username: ${{ secrets.EC2_DEV_USERNAME }}
password: ${{ secrets.EC2_DEV_PASSWORD }}
port: ${{ secrets.EC2_SSH_PORT }}
timeout: 60s
script: |
mkdir -p /home/ubuntu/tnt/
- name: Docker env File
run:
cp src/main/resources/config/dev.env ./infra/.env

- name: AWS EC2 Copy Files
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_DEV_HOST }}
username: ${{ secrets.EC2_DEV_USERNAME }}
password: ${{ secrets.EC2_DEV_PASSWORD }}
port: ${{ secrets.EC2_SSH_PORT }}
timeout: 60s
source: "infra"
target: "/home/ubuntu/tnt"

- name: Github Actions IP 보안그룹 삭제
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout with Submodule
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.SUBMODULE_PAT }}
fetch-depth: 0

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 21
distribution: 'corretto'

- name: Gradle cache
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Gradle 실행 권한 부여
run: chmod +x gradlew

- name: Build without test
run: ./gradlew build -x test

- name: Docker Buildx setting
uses: docker/[email protected]

- name: Dockerhub Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

- name: Dockerhub build and push
uses: docker/[email protected]
with:
context: .
file: ./infra/Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }}:latest
build-args: |
"SPRING_ACTIVE_PROFILE=dev"
platforms: |
linux/amd64
linux/arm64
- name: Public IP
uses: haythem/[email protected]

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

- name: Github Actions IP 보안그룹 추가
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
- name: AWS EC2 Server Deploy
uses: appleboy/ssh-action@master
env:
COMPOSE: "home/ubuntu/infra/docker-compose-dev.yml"
with:
envs: COMPOSE
host: ${{ secrets.EC2_DEV_HOST }}
username: ${{ secrets.EC2_DEV_USERNAME }}
password: ${{ secrets.EC2_DEV_PASSWORD }}
port: ${{ secrets.EC2_SSH_PORT }}
script: |
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_TOKEN }}
docker-compose -f $COMPOSE down --rmi all
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }}
docker-compose -f $COMPOSE up -d
echo "##### EC2 배포 완료 #####"
- name: Github Actions IP 보안그룹 삭제
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
8 changes: 8 additions & 0 deletions infra/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM amazoncorretto:21

ARG SPRING_ACTIVE_PROFILE
ENV SPRING_ACTIVE_PROFILE ${SPRING_ACTIVE_PROFILE}

COPY build/libs/TnT-0.0.1-SNAPSHOT.jar TnT.jar

ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_ACTIVE_PROFILE}", "/TnT.jar"]
38 changes: 38 additions & 0 deletions infra/docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
services:
tnt:
image: ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_REPOSITORY}:latest
container_name: tnt-spring-dev
restart: unless-stopped
expose:
- ${SERVER_PORT}
depends_on:
- mysql
environment:
SPRING_ACTIVE_PROFILE: ${SPRING_ACTIVE_PROFILE}

mysql:
image: mysql:8.0.40
container_name: mysql
platform: linux/arm64/v8
restart: always
ports:
- "3306:3306"
environment:
TZ: Asia/Seoul
MYSQL_DATABASE: ${DEV_MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: ${DEV_MYSQL_PASSWORD}
MYSQL_USERNAME: ${DEV_MYSQL_USERNAME}
MYSQL_PASSWORD: ${DEV_MYSQL_PASSWORD}
command:
- --skip-character-set-client-handshake
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci

redis:
image: redis:alpine
container_name: redis
platform: linux/arm64
restart: always
command: redis-server
ports:
- "6379:6379"
2 changes: 1 addition & 1 deletion src/main/resources/config

0 comments on commit 265c3c8

Please sign in to comment.