Update ci.yml #9
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: Java CI with Gradle & Deploy to EC2 | |
# event trigger | |
# master 브랜치에 push. pr 되었을 때 실행 | |
on: | |
push: | |
branches: [ "master" ] | |
pull_request: | |
types : | |
- closed | |
branches: [ "master" ] | |
env: | |
AWS_REGION: ap-northeast-2 | |
S3_BUCKET_NAME: catchyou-cicd-bucket | |
CODE_DEPLOY_APP_NAME: catchyou-deploy | |
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: catchyou-deploy-group | |
permissions: | |
contents: read | |
jobs: | |
deploy: | |
name : Deploy | |
runs-on: ubuntu-latest | |
environment : production | |
steps: | |
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | |
# 1) 기본 체크아웃 | |
- name: Checkout | |
- uses: actions/checkout@v1 | |
# JDK setting - github actions에서 사용할 JDK 설정 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v1 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# gradle caching - 빌드 시간 향상 | |
- name: Gradle Caching | |
uses: actions/cache@v1 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
# 환경별 properties 파일 생성(1) - application.properties | |
- run: mkdir -p ./Api/src/main/resources | |
- run: touch ./Api/src/main/resources/application.properties | |
- run: echo "${{ secrets.APPLICATION }}" > ./Api/src/main/resources/application.properties | |
- run: cat ./Api/src/main/resources/application.properties | |
# 환경별 properties 파일 생성(2) - application-core.properties | |
- run: mkdir -p ./Core/src/main/resources | |
- run: touch ./Core/src/main/resources/application-core.properties | |
- run: echo "${{ secrets.APPLICATION_CORE }}" > ./Core/src/main/resources/application-core.properties | |
- run: cat ./Core/src/main/resources/application-core.properties | |
# 환경별 properties 파일 생성(3) - application-domain.properties | |
- run: mkdir -p ./Domain/src/main/resources | |
- run: touch ./Domain/src/main/resources/application-domain.properties | |
- run: echo "${{ secrets.APPLICATION_DOMAIN }}" > ./Domain/src/main/resources/application-domain.properties | |
- run: cat ./Domain/src/main/resources/application-domain.properties | |
# 환경별 properties 파일 생성(2) - application-infrastructure.properties | |
- run: mkdir -p ./Infrastructure/src/main/resources | |
- run: touch ./Infrastructure/src/main/resources/application-infrastructure.properties | |
- run: echo "${{ secrets.APPLICATION_INFRASTRUCTURE }}" > ./Infrastructure/src/main/resources/application-infrastructure.properties | |
- run: cat ./Infrastructure/src/main/resources/application-infrastructure.properties | |
# 3) gradlew 권한 설정 | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
working-directory: ${{ env.working-directory }} | |
# 4) gradlew 빌드 | |
- name: Build with Gradle | |
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee | |
with: | |
arguments: clean build -x test | |
# AWS 사용자 정보 입력 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.ACCESS_KEY_SECRET }} | |
aws-region: ap-northeast-2 | |
# AWS S3에 업로드 | |
- name: Upload to AWS S3 | |
run: | | |
aws deploy push \ | |
--application-name ${{ env.CODE_DEPLOY_APP_NAME }} \ | |
--ignore-hidden-files \ | |
--s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ | |
--source . | |
# CodeDeploy에 배포 요청 | |
- name: Code Deploy | |
run: aws deploy create-deployment --application-name ${{ secrets.CODE_DEPLOY_APP_NAME }} | |
--deployment-config-name CodeDeployDefault.AllAtOnce \ | |
--deployment-group-name ${{ secrets.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} | |
--s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=deploy/$GITHUB_SHA.zip |