Skip to content

Commit

Permalink
Merge branch 'staging', remote-tracking branch 'origin' into main-test
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Aug 8, 2024
2 parents 8be5d19 + 66f53c3 commit 5d6ce4f
Show file tree
Hide file tree
Showing 438 changed files with 16,354 additions and 4,944 deletions.
2,145 changes: 0 additions & 2,145 deletions .$genti_architecture.drawio.bkp

This file was deleted.

156 changes: 98 additions & 58 deletions .github/workflows/cicd-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
name: CI/CD using github actions & docker

# event trigger
# main이나 develop 브랜치에 push가 되었을 때 실행
# main이나 staging 브랜치에 push가 되었을 때 실행
on:
push:
branches: [ "main", "dev" ]
branches: [ "main", "staging", "main-test" ]

env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: genti-deploy
CODE_DEPLOY_APPLICATION_NAME: genti
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: genti-deploy-group
permissions:
contents: read

Expand All @@ -33,9 +38,9 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-
# 공통 yml 파일 생성 - secret, oauth
# 공통 yml 파일 생성 - secret
- name: make application-secret.yml
if: contains(github.ref, 'dev') || contains(github.ref, 'main')
if: contains(github.ref, 'staging') || contains(github.ref, 'main')
run: |
cd ./genti-api/src/main/resources
touch ./application-secret.yml
Expand All @@ -44,13 +49,37 @@ jobs:

# 환경별 yml 파일 생성(3) - deploy
- name: make application-deploy.yml
if: contains(github.ref, 'dev') || contains(github.ref, 'main')
if: contains(github.ref, 'main')
run: |
cd ./genti-api/src/main/resources
touch ./application-deploy.yml
echo "${{ secrets.APPLICATION_DEPLOY }}" > ./application-deploy.yml
shell: bash

# 환경별 yml 파일 생성(3) - staging
- name: make application-staging.yml
if: contains(github.ref, 'staging')
run: |
cd ./genti-api/src/main/resources
touch ./application-staging.yml
echo "${{ secrets.APPLICATION_STAGING }}" > ./application-staging.yml
shell: bash

- name: make staging docker-compose file
if: contains(github.ref, 'staging')
run: |
mkdir -p ./docker/staging && cd ./docker/staging
touch ./docker-compose.yml
echo "${{ secrets.DOCKER_COMPOSE_STAGING }}" > ./docker-compose.yml
shell: bash

- name: make deploy docker-compose file
if: contains(github.ref, 'main')
run: |
mkdir -p ./docker/deploy && cd ./docker/deploy
touch ./docker-compose.yml
echo "${{ secrets.DOCKER_COMPOSE_DEPLOY }}" > ./docker-compose.yml
shell: bash

# gradle build
- name: Setup Gradle Wrapper
Expand All @@ -59,7 +88,7 @@ jobs:
- name: Build with Gradle Wrapper
run: |
chmod +x ./gradlew
./gradlew clean build
./gradlew clean build -x test
- name: Get Github action IP
Expand All @@ -69,7 +98,6 @@ jobs:
- name: Setting environment variables
run: |
echo "AWS_DEFAULT_REGION=ap-northeast-2" >> $GITHUB_ENV
echo "AWS_SG_NAME=github-actions" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
Expand All @@ -79,91 +107,103 @@ jobs:
aws-region: ap-northeast-2

- name: Add Github Actions IP to Security group
if: contains(github.ref, 'staging')
run: |
aws ec2 authorize-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ap-northeast-2

- name: Upload docker compose file to deploy server

- name: Login to aws ECR
if: contains(github.ref, 'main')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEPLOY }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
port: 22
source: "./docker/deploy/*"
target: "/home/ubuntu/workspace/"
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Upload docker compose file to dev server
if: contains(github.ref, 'dev')
- name: Build, tag, and push image to aws ECR
if: contains(github.ref, 'main')
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: genti-deploy
IMAGE_TAG: latest
run: |
docker build -f ./Dockerfile_deploy -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Upload docker-compose, appspec, afterInstall file to S3
if: contains(github.ref, 'main')
run: |
# Create a temporary directory for the zip contents
mkdir -p temp_dir/scripts
cp -r ./scripts/* temp_dir/scripts/
cp appspec.yml temp_dir/
cp ./docker/deploy/docker-compose.yml temp_dir/docker-compose.yml
# Navigate to the temporary directory and create the zip file
cd temp_dir
zip -r ../$GITHUB_SHA.zip ./*

# Move back to the initial directory and upload the zip file to S3
cd ..
aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME

# Clean up the temporary directory
rm -rf temp_dir

- name: Upload docker compose file to staging server
if: contains(github.ref, 'staging')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEV }}
host: ${{ secrets.HOST_STAGING }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
port: 22
source: "./docker/dev/*"
source: "./docker/staging/*"
target: "/home/ubuntu/workspace/"

# docker build & push to deploy server
- name: Docker build & push to deploy
- name: Deploy to EC2 with CodeDeploy
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile_deploy -t ${{ secrets.DOCKER_USERNAME }}/genti-deploy .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-deploy
# docker build & push to develop
- name: Docker build & push to dev
if: contains(github.ref, 'dev')
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip
# docker build & push to staging
- name: Docker build & push to staging
if: contains(github.ref, 'staging')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile_dev -t ${{ secrets.DOCKER_USERNAME }}/genti-dev .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-dev
## deploy to deploy server
- name: Deploy to deploy server
uses: appleboy/ssh-action@master
id: deploy-deploy
if: contains(github.ref, 'main')
with:
host: ${{ secrets.HOST_DEPLOY }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.EC2_KEY }}
envs: GITHUB_SHA
script: |
sudo docker ps
cd /home/ubuntu/workspace/docker/deploy
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-deploy
sudo docker compose up -d
sudo docker image prune -f
docker build -f Dockerfile_staging -t ${{ secrets.DOCKER_USERNAME }}/genti-staging .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-staging
## deploy to dev server
- name: Deploy to dev server
## deploy to staging server
- name: Deploy to staging server
uses: appleboy/ssh-action@master
id: deploy-dev
if: contains(github.ref, 'dev')
id: deploy-staging
if: contains(github.ref, 'staging')
with:
host: ${{ secrets.HOST_DEV }} # EC2 퍼블릭 IPv4 DNS
host: ${{ secrets.HOST_STAGING }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
password: ${{ secrets.PASSWORD }}
port: 22
key: ${{ secrets.EC2_KEY }}
script: |
sudo docker ps
cd /home/ubuntu/workspace/docker/dev
cd /home/ubuntu/workspace/docker/staging
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-dev
sudo docker compose up -d
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-staging
sudo docker-compose up -d
sudo docker image prune -f
- name: delete github actions ip from aws security group
if: contains(github.ref, 'staging')
run: |
aws ec2 revoke-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand Down
74 changes: 0 additions & 74 deletions .github/workflows/gradle.yml

This file was deleted.

19 changes: 16 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,22 @@ application-deploy.yaml
application-deploy.yml
application-secret.yaml
application-secret.yml
application-oauth.yaml
application-oauth.yml
application-staging.yml
application-staging.yaml

docker/db/

other/genti-sbl-iam_accessKeys.csv

genti-keypair.pem

README_PRIVATE.md
README_PRIVATE.md

docker-compose.yml
.env

logs/

firebase-genti.json

*.pem
11 changes: 9 additions & 2 deletions Dockerfile_deploy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM openjdk:21-jdk as builder

WORKDIR application
ARG JAR_FILE=build/libs/*.jar
ARG JAR_FILE=genti-api/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

Expand All @@ -11,9 +11,16 @@ WORKDIR application
ENV port 8080
EXPOSE 8080
ENV spring.profiles.active deploy

# build stage에 있는 파일을 복사
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./

ENTRYPOINT ["java", "-Dspring.profiles.active=deploy", "-Duser.timezone=Asia/Seoul", "org.springframework.boot.loader.launch.JarLauncher"]
COPY genti-api/src/main/resources/logback-spring.xml /application/
COPY genti-api/src/main/resources/console-appender.xml /application/
COPY genti-api/src/main/resources/discord-appender.xml /application/
COPY genti-api/src/main/resources/file-appender.xml /application/

ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
19 changes: 0 additions & 19 deletions Dockerfile_dev

This file was deleted.

Loading

0 comments on commit 5d6ce4f

Please sign in to comment.