Merge pull request #109 from HaegyeongKim01/week11-haegyeong #18
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
name: Build and Deploy to EC2 | |
# 워크플로우가 언제 실행될 것인지 조건 명시 | |
on: | |
push: # 모든 브랜치 대상 | |
# AWS 관련 값 변수로 설정 | |
env: | |
AWS_REGION: ap-northeast-2 | |
AWS_S3_BUCKET: devcard-deploy-bucket | |
AWS_CODE_DEPLOY_APPLICATION: Devcard-Application-CD | |
AWS_CODE_DEPLOY_GROUP: Devcard-Deployment-Group | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- uses: actions/checkout@v4 | |
# JDK 21 설치 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
# 환경 변수를 사용하여 application-secret.properties 파일 생성 | |
- name: make application-secret.properties | |
run: | | |
mkdir -p ./src/main/resources | |
cd ./src/main/resources | |
touch ./application-secret.properties | |
echo "kakao.javascript.key=${{ secrets.KAKAO_JAVASCRIPT_KEY }}" >> ./application-secret.properties | |
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> ./application-secret.properties | |
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ./application-secret.properties | |
echo "GH_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}" >> ./application-secret.properties | |
echo "GH_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}" >> ./application-secret.properties | |
echo "GH_REDIRECT_URI=${{ secrets.GH_REDIRECT_URI }}" >> ./application-secret.properties | |
# 권한 부여 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
shell: bash | |
- name: Build and Test | |
env: | |
DB_USERNAME: ${{ secrets.DB_USERNAME }} | |
DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
run: ./gradlew build test --info | |
# 빌드 파일을 zip 형식으로 압축 | |
- name: Make zip file | |
run: zip -r ./$GITHUB_SHA.zip . | |
shell: bash | |
# AWS 권한 | |
- name: AWS credential 설정 | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-region: ${{ env.AWS_REGION }} | |
aws-access-key-id: ${{ secrets.CICD_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.CICD_SECRET_KEY }} | |
# S3 버킷에 빌드파일(zip 파일)을 업로드 | |
- name: Upload to S3 | |
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$AWS_S3_BUCKET/$GITHUB_SHA.zip | |
# EC2 인스턴스에 S3에 저장되어 있던 zip 파일을 받아와 배포 시작 | |
- name: EC2에 배포 | |
run: aws deploy create-deployment --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} --s3-location bucket=$AWS_S3_BUCKET,key=$GITHUB_SHA.zip,bundleType=zip |