Skip to content

Create cd.yml

Create cd.yml #1

Workflow file for this run

name: Deploy To EC2
on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]
env:
BASE_RESOURCE_PATH: ./src/main/resources/application-prod.yml
permissions: write-all
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Github Repository 파일 불러오기
uses: actions/checkout@v4
- name: JDK 17버전 설치
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: application-prod.yml 파일 생성
run: |
mkdir -p ./src/main/resources # 디렉토리가 없으면 생성
echo "${{ secrets.APPLICATION_PROD }}" | base64 --decode > ./src/main/resources/application-prod.yml
- name: application-prod.yml 파일 내용 확인
run: cat ./src/main/resources/application-prod.yml
- name: gradlew에 실행 권한 부여하기
run: chmod +x ./gradlew
- name: 테스트 및 빌드하기
run: ./gradlew clean build --no-build-cache -x test
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Debug Secrets
run: |
echo "Key ID: ${{ secrets.AWS_ACCESS_KEY_ID }}"
echo "Secret Key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}"
- name: AWS Resource에 접근할 수 있게 AWS credentials 설정
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ap-northeast-2
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: ECR에 로그인하기
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Docker 이미지 생성
run: docker build --no-cache -t kampus-server .
- name: Docker 이미지에 Tag 붙이기
run: docker tag kampus-server ${{ steps.login-ecr.outputs.registry }}/kampus-dev:latest
- name: ECR에 Docker 이미지 Push하기
run: docker push ${{ steps.login-ecr.outputs.registry }}/kampus-dev:latest
- name: 압축하기
run: tar -czvf $GITHUB_SHA.tar.gz appspec.yml scripts
- name: S3에 프로젝트 폴더 업로드하기
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.tar.gz s3://kampus-deploy/$GITHUB_SHA.tar.gz
- name: Code Deploy를 활용해 EC2에 프로젝트 코드 배포
run: aws deploy create-deployment
--application-name kampus-server
--deployment-config-name CodeDeployDefault.AllAtOnce
--deployment-group-name kampus-server-deploy
--s3-location bucket=kampus-deploy,bundleType=tgz,key=$GITHUB_SHA.tar.gz