Skip to content

Merge pull request #197 from wl2258/dev #52

Merge pull request #197 from wl2258/dev

Merge pull request #197 from wl2258/dev #52

Workflow file for this run

name: Deploy to Amazon EC2
on: # Workflow를 자동으로 트리거 하기 위한 이벤트
release:
types: [push] # push를 하면 자동으로 실행
push:
branches: [dev, main] # develop, master 브랜치에서 동작
# 리전, 버킷 이름, CodeDeploy 앱 이름, CodeDeploy 배포 그룹 이름
env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: seoul-codedeploy-molly-web
DEV_S3_BUCKET_DIR: dev
DEV_CODE_DEPLOY_APPLICATION_NAME: molly-dev
DEV_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: molly-dev-group
PROD_S3_BUCKET_DIR: prod
PROD_CODE_DEPLOY_APPLICATION_NAME: molly-prod
PROD_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: molly-prod-group
permissions:
contents: read
jobs: # 병렬로 실행되는 작업 단위, 여러 step으로 이루어짐
# dev
dev-deploy:
if: contains(github.ref, 'dev')
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
# (1) 기본 체크아웃
- name: Checkout
uses: actions/checkout@v3
# (2) JDK 11 세팅
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
# (3) 환경별 yml 파일 생성 - dev
- name: make env yml
run: |
cd ./molly-spring/src/main/resources
touch ./application-BUNDLE-KEY.yml
echo "${{ secrets.YML_BUNDLE_KEY_DEV }}" > ./application-BUNDLE-KEY.yml
touch ./application-database.yml
echo "${{ secrets.YML_DATABASE }}" > ./application-database.yml
touch ./application-social-login.yml
echo "${{ secrets.YML_SOCIAL_LOGIN }}" > ./application-social-login.yml
shell: bash
# (4) ./gradlw 실행 권한 부여
- name: Grant execute permission for gradlew
run: cd molly-spring && ./gradlew clean build -x test
shell: bash
# (5) Gradle build (Test 제외)
- name: Build with Gradle
run: cd molly-spring && ./gradlew clean build -x test
shell: bash
# (6) Cd Project Root DIR
- name: Cd Project Root DIR
run: cd ../../../
shell: bash
# (7) Make Deploy DIR
- name: Make Deploy DIR
run: |
mkdir ./dev-molly-deploy
chmod -R 777 ./dev-molly-deploy
shell: bash
# (8) Copy Jar To Delpoy DIR
- name: Copy jar and yml To Delpoy DIR
run: |
cp ./molly-spring/build/libs/molly-spring-0.0.1-SNAPSHOT.jar ./dev-molly-deploy
cp ./.github/scripts/dev/devappsepc.yml ./dev-molly-deploy/appspec.yml
cp ./.github/scripts/dev/devdeploymolly.sh ./dev-molly-deploy/deploymolly.sh
cp ./.github/scripts/dev/devhealth.sh ./dev-molly-deploy/health.sh
cp ./.github/scripts/dev/devprofile.sh ./dev-molly-deploy/profile.sh
cp ./.github/scripts/dev/devstart.sh ./dev-molly-deploy/start.sh
cp ./.github/scripts/dev/devstop.sh ./dev-molly-deploy/stop.sh
cp ./.github/scripts/dev/devswitch.sh ./dev-molly-deploy/switch.sh
chmod +x ./dev-molly-deploy
shell: bash
# (9) Make zip file
- name: Make zip file
run: |
cd ./dev-molly-deploy
ZIP_FILENAME="$(date '+%Y%m%d')_$GITHUB_SHA"
zip -qq -r "$ZIP_FILENAME".zip .
shell: bash
# (10) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용)
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
# (11) 빌드 결과물을 S3 버킷에 업로드
# $GITHUB_SHA: Github 자체에서 커밋마다 생성하는 랜덤한 변수값 (파일 업로드시 이름 중복 X)
- name: Upload to AWS S3
run: |
cd ./dev-molly-deploy
ZIP_FILENAME="$(date '+%Y%m%d')_$GITHUB_SHA"
aws s3 cp --region ${{ env.AWS_REGION }} ./"$ZIP_FILENAME".zip s3://${{ env.S3_BUCKET_NAME }}/${{ env.DEV_S3_BUCKET_DIR }}/"$ZIP_FILENAME".zip
# (12) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행
# --application-name: CodeDeploy 애플리케이션 이름
# --deployment-config-name: 배포 방식 (기본값 사용)
# --deployment-group-name: CodeDeploy 배포 그룹 이름
# --s3-location: 버킷 이름, 키 값, 번들타입
- name: Deploy to AWS EC2 from S3
run: |
ZIP_FILENAME="$(date '+%Y%m%d')_$GITHUB_SHA"
aws deploy create-deployment --application-name $DEV_CODE_DEPLOY_APPLICATION_NAME --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name $DEV_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=dev/"$ZIP_FILENAME".zip