Update main.yml #2
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: | |
branches: [ "weekly/11"] | |
pull_request: | |
# branches: [ "Master" ] | |
types: [closed] | |
# 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 | |
if: github.repository == 'kakao-tech-campus-2nd-step3/Team28_BE' && github.event.pull_request.merged == true | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- uses: actions/checkout@v4 | |
# JDK 17 설치 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
# 공개되면 안되는 정보를 담은 .properties 파일을 생성 | |
- name: make application-secret.properties | |
run: | | |
mkdir -p ./src/main/resources | |
cd ./src/main/resources | |
touch ./application-secret.properties | |
echo "${{ secrets.APPLICATION_SECRET }}" > ./application-secret.properties | |
- name: Check environment variables | |
run: | | |
echo "AWS_REGION: ${{ env.AWS_REGION }}" | |
echo "AWS_S3_BUCKET: ${{ env.AWS_S3_BUCKET }}" | |
echo "AWS_CODE_DEPLOY_APPLICATION: ${{ env.AWS_CODE_DEPLOY_APPLICATION }}" | |
echo "AWS_CODE_DEPLOY_GROUP: ${{ env.AWS_CODE_DEPLOY_GROUP }}" | |
echo "DATABASE_URL: ${{ secrets.DATABASE_URL }}" | |
echo "APPLICATION_SECRET is set" $([ -n "${{ secrets.APPLICATION_SECRET }}" ] && echo "Yes" || echo "No") | |
# 권한 부여 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
shell: bash | |
- name: Build and Test | |
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 |