Skip to content

Commit

Permalink
Merge pull request #3 from Nexters/feature/2
Browse files Browse the repository at this point in the history
[Feature/2] Github Actions를 통한 CI/CD를 구축한다
  • Loading branch information
miseongk authored Jul 19, 2024
2 parents a95cca5 + c120f0e commit 36b75a3
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .github/ISSUE_TEMPLATE/issue_template.md

This file was deleted.

71 changes: 71 additions & 0 deletions .github/workflows/deploy-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Bottles Deploy Workflow

on:
push:
branches:
- develop

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: '17'

- name: Grant execute permission for gradlew
run: chmod +x gradlew
shell: bash

- name: Build with gradle
run: ./gradlew clean build
shell: bash

- name: Set Date and Time for Tag
run: echo "DATETIME_TAG=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV

- name: Docker build & push
env:
DOCKER_TAG: ${{ env.DATETIME_TAG }}
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build --platform linux/amd64 -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/bottles:$DOCKER_TAG .
docker push ${{ secrets.DOCKER_USERNAME }}/bottles:$DOCKER_TAG
- name: Transfer file using SCP
env:
SERVER_HOST: ${{ secrets.SERVER_HOST }}
SERVER_USER_NAME: ${{ secrets.SERVER_USER_NAME }}
SERVER_PRIVATE_KEY: ${{ secrets.SERVER_PRIVATE_KEY }}
run: |
echo "$SERVER_PRIVATE_KEY" > private_key.pem
chmod 600 private_key.pem
scp -o StrictHostKeyChecking=no -i private_key.pem ./docker-compose.yml $SERVER_USER_NAME@$SERVER_HOST:/home/$SERVER_USER_NAME/docker
scp -o StrictHostKeyChecking=no -i private_key.pem ./deploy.sh $SERVER_USER_NAME@$SERVER_HOST:/home/$SERVER_USER_NAME/deploy
scp -o StrictHostKeyChecking=no -i private_key.pem ./notify_error.sh $SERVER_USER_NAME@$SERVER_HOST:/home/$SERVER_USER_NAME/deploy
rm private_key.pem
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER_NAME }}
key: ${{ secrets.SERVER_PRIVATE_KEY }}
script: |
export SERVER_NGINX_CONF=${{ secrets.SERVER_NGINX_CONF }}
export SERVER_USER_NAME=${{ secrets.SERVER_USER_NAME }}
export DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}
export DOCKER_TAG=${{ env.DATETIME_TAG }}
export DB_DATABASE=${{ secrets.DB_DATABASE }}
export DB_USER_NAME=${{ secrets.DB_USER_NAME }}
export DB_PASSWORD=${{ secrets.DB_PASSWORD }}
export DISCORD_WEBHOOK_URL=${{ secrets.DISCORD_WEBHOOK_URL }}
cd /home/$SERVER_USER_NAME/deploy
sudo chmod +x deploy.sh notify_error.sh
./deploy.sh
52 changes: 52 additions & 0 deletions .github/workflows/pull-request-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Bottles Pull Request Workflow

on:
pull_request:
branches:
- develop
types: [opened, reopened, synchronize]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: '17'

- name: Grant execute permission for gradlew
run: chmod +x gradlew
shell: bash

- name: Build with gradle
run: ./gradlew clean build
shell: bash

- name: Send Discord Notification
if: github.event.action == 'opened' || github.event.action == 'reopened'
env:
DATA: |
{
"embeds": [
{
"author": {
"name": ${{ toJson(github.event.sender.login) }},
"url": "https://github.com/${{ github.event.sender.login }}",
"icon_url": ${{ toJson(github.event.sender.avatar_url) }}
},
"title": ${{ toJson(github.event.pull_request.title) }},
"description": ${{ toJson(github.event.pull_request.body) }},
"url": ${{ toJson(github.event.pull_request.html_url) }},
"color": 65280
}
]
}
run: |
curl -X POST -H 'Content-type: application/json' \
-d "$DATA" \
${{ secrets.DISCORD_WEBHOOK_URL }}
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM amazoncorretto:17-alpine

COPY build/libs/*.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")

runtimeOnly("com.h2database:h2")
runtimeOnly("com.mysql:mysql-connector-j")
runtimeOnly("mysql:mysql-connector-java")


testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
23 changes: 23 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

sudo docker pull ${DOCKER_USERNAME}/bottles:${DOCKER_TAG}
cd ../docker
docker-compose up -d

# 중단된 컨테이너가 존재하는지 확인
EXIT_CONTAINERS=$(docker-compose ps | grep 'Exit 1' | awk '{print $1}')

if [ -n "$EXIT_CONTAINERS" ]; then
for CONTAINER in $EXIT_CONTAINERS; do
../deploy/notify_error.sh "$CONTAINER"
done
fi

LATEST_TAG=${DOCKER_TAG}
RUNNING_TAG=$(docker-compose ps --format "{{.Image}}" | grep "${DOCKER_USERNAME}/bottles" | awk -F: '{print $2}')

if [ "$LATEST_TAG" != "$RUNNING_TAG" ]; then
../deploy/notify_error.sh "bottles:$LATEST_TAG 배포를 실패했습니다.\n현재 bottles:$RUNNING_TAG 가 실행중입니다."
fi

sudo docker image prune -f
38 changes: 38 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3.8'

services:
nginx:
container_name: nginx
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- /etc/nginx/conf.d/${SERVER_NGINX_CONF}:/etc/nginx/conf.d/${SERVER_NGINX_CONF}
- /etc/letsencrypt:/etc/letsencrypt
depends_on:
- springboot

springboot:
container_name: springboot
image: ${DOCKER_USERNAME}/bottles:${DOCKER_TAG}
ports:
- "8080:8080"
depends_on:
- mysql

db:
container_name: mysql
image: mysql
restart: always
ports:
- "3306:3306"
environment:
TZ: Asia/Seoul
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USER_NAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- ./data/mysql/:/var/lib/mysql
- ./db/mysql/config:/etc/mysql/conf.d
20 changes: 20 additions & 0 deletions notify_error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

ERROR_MESSAGE=$1

DATA=$(cat <<EOF
{
"embeds": [
{
"title": "🚨 Deploy Error",
"description": "$ERROR_MESSAGE",
"color": 16711680
}
]
}
EOF
)

curl -X POST -H 'Content-type: application/json' \
-d "$DATA" \
${DISCORD_WEBHOOK_URL}

0 comments on commit 36b75a3

Please sign in to comment.