Update deploy.yml #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: Deploy Backend to EC2 | |
on: | |
push: | |
branches: | |
- develop | |
- main | |
workflow_dispatch: # 수동 트리거 추가 | |
jobs: | |
deploy-backend: | |
runs-on: ubuntu-latest | |
# 환경변수 설정 | |
env: | |
AWS_SG_ID: ${{ secrets.AWS_SG_ID }} | |
AWS_SG_RDS_ID: ${{ secrets.AWS_SG_RDS_ID }} | |
AWS_DEFAULT_REGION: ap-northeast-2 | |
AWS_EC2_SSH_KEY: ${{ secrets.AWS_EC2_SSH_KEY }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
APPLICATION_YML: ${{ secrets.APPLICATION_YML }} | |
steps: | |
# Java 21 설치 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '21' | |
# Github action IP 가져오기 | |
- name: Get Github action IP | |
id: ip | |
uses: haythem/public-ip@v1.2 | |
# 배포 브랜치 지정 (with: ref: 옵션이 없을 경우 트리거가 되는 브랜치) | |
- name: Checkout branch | |
uses: actions/checkout@v2 | |
with: | |
ref: develop | |
# secrets 내용을 읽어 yml 파일을 특정 위치에 생성 | |
- name: Create application.yml | |
run: | | |
echo -e "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml | |
# SSH Agent 설정 및 GitHub Secrets에 저장된 SSH 키 로드 | |
- name: Set up SSH agent | |
uses: webfactory/ssh-agent@v0.5.3 | |
with: | |
ssh-private-key: ${{ secrets.AWS_EC2_SSH_KEY }} | |
# Github action IP 보안그룹에 추가 | |
- name: Add Github Actions IP to Security group | |
run: | | |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_RDS_ID }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
# EC2 호스트 키 등록 | |
- name: Add EC2 to known hosts | |
run: ssh-keyscan -H ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com >> ~/.ssh/known_hosts | |
# Docker Compose로 필요한 서비스 실행 | |
- name: Run Docker Compose | |
run: | | |
docker-compose up -d | |
# init-index.sh 스크립트 실행 | |
- name: Run init-index.sh script | |
run: | | |
chmod +x ./init-index.sh | |
./init-index.sh | |
# Build Application | |
- name: Build the application | |
run: ./gradlew build | |
# EC2에 빌드된 소스 배포 | |
- name: Deploy source to EC2 | |
run: | | |
scp init-index.sh ubuntu@ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com:/home/ubuntu/catchweak | |
scp docker-compose.yml ubuntu@ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com:/home/ubuntu/catchweak | |
scp -r build/libs ubuntu@ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com:/home/ubuntu/catchweak | |
# 환경 세팅 docker-compose.yml 및 init-index.sh 실행 | |
- name: Run Envirenments scripts | |
run: | | |
ssh ubuntu@ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com << EOF | |
cd /home/ubuntu/catchweak | |
chmod +x init-index.sh | |
./init-index.sh # 인덱스 초기화 스크립트 실행 | |
docker-compose up -d | |
EOF | |
# 서버 실행 | |
- name: Run Backend server | |
run: | | |
ssh ubuntu@ec2-13-209-47-84.ap-northeast-2.compute.amazonaws.com << EOF | |
cd /home/ubuntu/catchweak | |
./gradlew build | |
nohup java -jar build/libs/catchweak.jar & | |
EOF | |
# Github action IP 보안그룹에서 제거 (배포 후) | |
- name: Remove Github Actions IP from Security group | |
run: | | |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_RDS_ID }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |