-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Nexters/feature/2
[Feature/2] Github Actions를 통한 CI/CD를 구축한다
- Loading branch information
Showing
8 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |