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 FullStackApp to AWS | |
on: | |
push: | |
branches: | |
- refactor/pipeline | |
env: | |
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }} | |
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }} | |
AWS_REGION: ap-northeast-2 | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
# 2. JDK 설치 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '21' | |
distribution: 'adopt' | |
# 3. Gradle 빌드 | |
- name: Build with Gradle | |
run: | | |
cd backend # gradlew 파일이 있는 디렉토리로 이동 | |
./gradlew build | |
- name: Login to Docker Hub | |
run: echo $DOCKER_HUB_PASSWORD | docker login -u $DOCKER_HUB_USERNAME --password-stdin | |
# Frontend 빌드 및 S3 배포 | |
- name: Build and deploy Frontend | |
run: | | |
cd frontend | |
npm ci | |
CI=false npm run build | |
aws s3 sync build/ s3://${{ secrets.S3_BUCKET }} --delete | |
aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" | |
# Backend, AI 이미지 빌드 및 푸시 | |
- name: Build and push Docker images | |
run: | | |
docker build -t $DOCKER_HUB_USERNAME/backend:latest ./backend | |
docker build -t $DOCKER_HUB_USERNAME/ai:latest ./ai | |
docker push $DOCKER_HUB_USERNAME/backend:latest | |
docker push $DOCKER_HUB_USERNAME/ai:latest | |
# Backend 서비스 배포 (AWS Systems Manager 사용) | |
- name: Deploy Backend services | |
run: | | |
aws ssm send-command \ | |
--instance-ids i-05f61b1d8708b7fdd \ | |
--document-name "AWS-RunShellScript" \ | |
--parameters '{ | |
"commands": [ | |
"sudo docker pull ${{ env.DOCKER_HUB_USERNAME }}/backend:latest", | |
"sudo docker pull ${{ env.DOCKER_HUB_USERNAME }}/ai:latest", | |
"sudo docker stop backend ai || true", | |
"sudo docker rm backend ai || true", | |
"sudo docker run -d --name backend --network ec2-user_export_network -p 8080:8080 -e SPRING_DATASOURCE_URL=jdbc:mysql://172.18.0.3:3306/moviedatabase?serverTimezone=Asia/Seoul -e SPRING_DATASOURCE_USERNAME=root -e SPRING_DATASOURCE_PASSWORD=qlalfqjsgh486 -e AI_SERVICE_URL=http://172.18.0.5:8000/api/v1 ${{ env.DOCKER_HUB_USERNAME }}/backend:latest" | |
"sudo docker run -d --name ai --network ec2-user_export_network -p 8000:8000 -e PROJECT_NAME=ParseAI -e DATABASE_URL=mysql+aiomysql://root:[email protected]:3306/moviedatabase ${{ env.DOCKER_HUB_USERNAME }}/ai:latest", | |
] | |
}' | |
- name: Cleanup | |
if: always() | |
run: | | |
docker logout | |
rm -f /tmp/ec2_key |