chore : CD에 env설정 #25
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: CD - Deploy to Production Server | |
on: | |
push: | |
branches: [ "dev" ] # main 브랜치 푸시 시 실행 | |
# flow에서 사용할 변수 | |
env: | |
AWS_REGION: ap-northeast-2 | |
S3_BUCKET_NAME: meerket-bucket | |
CODE_DEPLOY_APPLICATION_NAME: meerket-dev | |
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: meerket-deployment-group | |
permissions: write-all | |
#여기서부터 build job | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
# 1) 기본 체크아웃 | |
- name: Checkout | |
uses: actions/checkout@v4 | |
# 2) JDK 17 셋팅 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# 2.5) secret설정한 env 등록 | |
- name: Create .env file from Secret | |
run: | | |
echo "${{ secrets.ENV_PROD}}" | base64 --decode > .env | |
# # 4) gradle 테스트 빌드 | |
# - name: Build Test with Gradle | |
# run: ./gradlew test -i | |
# # run: ./gradlew clean build -i | |
# # working-directory: ${{ env.working-directory }} | |
# 3) gradlew 권한 설정 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./meerket/gradlew # gradlew에 실행 권한 부여 | |
- name: Build with Gradle | |
working-directory: ./meerket | |
run: | | |
echo "Setting execute permission for gradlew" | |
chmod +x ./gradlew | |
echo "Cleaning and building the project" | |
./gradlew clean build -x test --no-daemon --info | |
echo "Verifying JAR file generation..." | |
if [ -f ./meerket-application/build/libs/meerket-application-0.0.1.jar ]; then | |
echo "JAR file generated successfully!" | |
else | |
echo "JAR file not found! Please check the build configuration." | |
exit 1 | |
fi | |
# 여기서부터 배포 job | |
deploy: | |
name: deploy | |
runs-on: ubuntu-latest | |
environment: production | |
needs: [ build ] # test와 build 작업이 성공적으로 완료된 경우에만 실행 | |
steps: | |
# 1) 기본 체크아웃 | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# 2) JDK 17 셋팅 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# 3) .env 파일 다운로드 및 복사 | |
- name: Download .env from S3 | |
run: | | |
aws s3 cp s3://${{ env.S3_BUCKET_NAME }}/.env /home/ubuntu/project/.env | |
echo ".env downloaded to /home/ubuntu/project/" | |
# 2.5) secret 설정한 firesbase json key 등록 | |
- name: create-json | |
uses: jsdaniell/[email protected] | |
with: | |
name: "meerket-83e38-firebase-adminsdk-gyt9i-d2df62fdf4.json" | |
json: ${{secrets.FIREBASE_SERVICE_KEY }} | |
dir: './meerket/meerket-application/src/main/resources' | |
# 3) gradlew 권한 설정 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./meerket/gradlew # gradlew에 실행 권한 부여 | |
# 4) gradle 테스트 빌드 | |
- name: Build with Gradle | |
working-directory: ./meerket | |
run: ./gradlew clean build -x test | |
# run: ./gradlew clean build -i | |
# working-directory: ${{ env.working-directory }} | |
# AWS 인증 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_DEV }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }} | |
aws-region: ${{ env.AWS_REGION }} | |
# AWS S3에 업로드 | |
- name: Upload to AWS S3 | |
run: | | |
cd meerket | |
aws deploy push \ | |
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ | |
--ignore-hidden-files \ | |
--s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ | |
--source . | |
echo "S3 Upload completed. Verifying contents in S3..." | |
aws s3 ls s3://meerket-bucket/ | |
# AWS EC2에 Deploy | |
- 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=$GITHUB_SHA.zip,bundleType=zip |