Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[๐Ÿš€ Deploy] ver1 ๋ฐฐํฌ ํ™˜๊ฒฝ ๊ตฌ์„ฑ #20

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 53 additions & 79 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,29 @@
name: Backend Deploy

## Controls when the workflow will run
#on:
# # Triggers the workflow on push or pull request events but only for the "develop" branch
# push:
# branches: [ "main" ]
on:
# Triggers the workflow on push or pull request events but only for the "develop" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
#
# # Allows you to run this workflow manually from the Actions tab
# workflow_dispatch:

# ๋ณธ์ธ์ด ์„ค์ •ํ•œ ๊ฐ’์„ ์—ฌ๊ธฐ์„œ ์ฑ„์›Œ๋„ฃ์Šต๋‹ˆ๋‹ค.
# ๋ฆฌ์ „, ๋ฒ„ํ‚ท ์ด๋ฆ„, CodeDeploy ์•ฑ ์ด๋ฆ„, CodeDeploy ๋ฐฐํฌ ๊ทธ๋ฃน ์ด๋ฆ„
env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: 1team-s3
S3_BUCKET_BACKEND: 1team-s3/build/backend
CODE_DEPLOY_APPLICATION_NAME: lookforbutler
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: lookforbutler-backend

permissions:
contents: read

jobs:
deploy:
build:
name: Deploy Backend
runs-on: ubuntu-latest
environment: production

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# (1) ๊ธฐ๋ณธ ์ฒดํฌ์•„์›ƒ
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: backend

# (2) JDK 17 ์„ธํŒ…
- name: Set up JDK 17
Expand All @@ -44,78 +34,62 @@ jobs:
distribution: "zulu"
java-version: "17"

# (3) S3 application-prod.yml ์„ธํŒ…
- name: Download application-prod.yml
uses: keithweaver/aws-s3-github-action@v1.0.0 # Verifies the recursive flag
with:
command: cp
source: ${{ secrets.S3_PROD_YML }}
destination: ./backend/api/src/main/resources
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_KEY }}
aws_region: ${{ secrets.AWS_REGION }}
- name: Set secret file (prod)
run: |
echo "${{ secrets.APPLICATION_PROD }}" | base64 --decode > ./api/src/main/resources/application-prod.yml
chmod 600 ./api/src/main/resources/application-prod.yml

# (4) S3 application-test.yml ์„ธํŒ…
# - name: Download application-test.yml
# uses: keithweaver/aws-s3-github-action@v1.0.0 # Verifies the recursive flag
# with:
# command: cp
# source: ${{ secrets.S3_TEST_YML }}
# destination: ./backend/api/src/main/resources
# aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY }}
# aws_secret_access_key: ${{ secrets.AWS_SECRET_KEY }}
# aws_region: ${{ secrets.AWS_REGION }}
- name: Set secret file (test)
run: |
echo "${{ secrets.APPLICATION_TEST }}" base64 --decode > ./api/src/main/resources/application-test.yml
chmod 600 ./api/src/main/resources/application-test.yml

# (5) ํ˜„์žฌ ์œ„์น˜ ํŒŒ์•…ํ•˜๋Š” cmd
- name: cmd
run: |
pwd
ls -la
# cd ./backend/api/src/main/resources
echo "ls ./backend/api/src/main/resources"
ls -la ./backend/api/src/main/resources
sudo chmod +x ./backend/api/src/main/resources/application-prod.yml
ls -la ./backend/api/src/main/resources

# (6) Gradle ์„ธํŒ…
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: build -x test
build-root-directory: backend
echo "ls ./api/src/main/resources"
ls -la ./api/src/main/resources
sudo chmod +x ./api/src/main/resources/application-prod.yml
ls -la ./api/src/main/resources

# # (6) Grant execute permission to gradlew
# - name: Grant execute permission to gradlew
# run: chmod +x ./backend/gradlew
# Grant execute permission to gradlew
- name: Grant execute permission to gradlew
run: chmod +x gradlew

# # (7) Gradle ๋นŒ๋“œ
# - name: Execute Gradle build
# run: |
# cd backend
# ./gradlew test -i
# Spring Boot Build
- name: Spring Boot Build
run: |
./gradlew clean build --exclude-task test -x asciidoctor

# (7) AWS ์ธ์ฆ (IAM ์‚ฌ์šฉ์ž Access Key, Secret Key ํ™œ์šฉ)
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
# ๋„์ปค ํ—ˆ๋ธŒ ๋กœ๊ทธ์ธ
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ env.AWS_REGION }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# (8) ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฌผ์„ S3 ๋ฒ„ํ‚ท์— ์—…๋กœ๋“œ
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--ignore-hidden-files \
--s3-location s3://$S3_BUCKET_NAME/build/backend/app.zip \
--source ./backend
# Build Docker image
- name: Build Docker image
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} ./

# (9) S3 ๋ฒ„ํ‚ท์— ์žˆ๋Š” ํŒŒ์ผ์„ ๋Œ€์ƒ์œผ๋กœ CodeDeploy ์‹คํ–‰
- name: Deploy to AWS EC2 from S3
run: |
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=build/backend/app.zip,bundleType=zip
# Docker Hub์— private ์ด๋ฏธ์ง€๋กœ ํ‘ธ์‹œ
- name: Push Docker image to Docker Hub
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}

# Deploy to AWS EC2
- name: Deploy to AWS EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_HOST }} # EC2 ์ธ์Šคํ„ด์Šค ํผ๋ธ”๋ฆญ DNS
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.AWS_PRIVACY_KEY }} # pem ํ‚ค
port: ${{ secrets.REMOTE_SSH_PORT }} # ์ ‘์†ํฌํŠธ
script: |
sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_PASSWORD }}"
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
sudo docker stop ${{ secrets.DOCKERHUB_REPOSITORY }} || true
sudo docker rm ${{ secrets.DOCKERHUB_REPOSITORY }} || true
sudo docker image prune -a || y
sudo docker run -d -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod --name ${{ secrets.DOCKERHUB_REPOSITORY }} ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:latest

# APP
FROM openjdk:17.0-slim
ARG JAR_FILE=/api/build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
47 changes: 46 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id 'org.springframework.boot' version '3.1.2'
id 'io.spring.dependency-management' version '1.1.2'
id 'org.hibernate.orm' version '6.2.6.Final'
id "org.asciidoctor.jvm.convert" version "3.3.2"
id 'jacoco'
}

group = 'mutsa'
Expand All @@ -19,6 +21,7 @@ configurations {
compileOnly {
extendsFrom annotationProcessor
}
asciidoctorExt
}

repositories {
Expand Down Expand Up @@ -81,6 +84,10 @@ dependencies {
//test-embedded-redis
testImplementation 'it.ozimov:embedded-redis:0.7.3'

//restdocs
asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

//socket
implementation 'org.springframework.boot:spring-boot-starter-websocket'
// gson
Expand All @@ -104,4 +111,42 @@ hibernate {
enhancement {
enableAssociationManagement = true
}
}
}

ext {
snippetsDir = file('build/generated-snippets')
}

asciidoctor { // asciidoctor ์ž‘์—… ๊ตฌ์„ฑ
inputs.dir snippetsDir
configurations 'asciidoctorExt'
dependsOn test
}

// static/docs ํด๋” ๋น„์šฐ๊ธฐ
asciidoctor.doFirst {
delete file('src/main/resources/static/docs')
}

// asccidoctor ์ž‘์—… ์ดํ›„ ์ƒ์„ฑ๋œ HTML ํŒŒ์ผ์„ static/docs ๋กœ copy
task copyDocument(type: Copy) {
dependsOn asciidoctor
from file("build/docs/asciidoc")
into file("src/main/resources/static/docs")
}

// build ์˜ ์˜์กด์ž‘์—… ๋ช…์‹œ
build {
dependsOn copyDocument
}


test {
outputs.dir snippetsDir
finalizedBy jacocoTestReport // ํ…Œ์ŠคํŠธ ์ข…๋ฃŒํ›„ ํ•ญ์ƒ ๋ฆฌํฌํŠธ ์ƒ์„ฑ
}

jacocoTestReport {
dependsOn test // ๋ฆฌํฌํŠธ ์ƒ์„ฑ์„ ์œ„ํ•ด์„œ๋Š” test๊ฐ€ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•จ
}

This file was deleted.

Loading