Update icurri_deploy.yml #6
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: DOTFLOW_SERVER_DEPLOY | |
on: | |
push: | |
branches: [ "release" ] | |
jobs: | |
ci: | |
name: CI | |
runs-on: ubuntu-latest | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ap-northeast-2 | |
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }} | |
ECR_URI: ${{ secrets.ECR_URI }} | |
IMAGE_TAG: ${{ github.sha }} | |
steps: | |
#체크아웃 | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
# JDK 설치 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
#서브 모듈 접근 | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.SUBMODULE_ACTION_TOKEN }} | |
submodules: true | |
# 서브 모듈 변경 점 있으면 update | |
- name: Git Submodule Update | |
run: | | |
git submodule update --remote --recursive | |
# gradlew 권한 변경 | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
# 빌드(test는 제외) | |
- name: Build with Gradle | |
uses: gradle/gradle-build-action@v2 | |
with: | |
arguments: clean build -x test | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
## Release Branch Build | |
- name: Build, Tag (Release Branch) | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
if: github.ref == 'refs/heads/release' | |
run: | | |
docker build --build-arg SPRING_PROFILE=dev -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
## Main Branch Build | |
- name: Build, Tag (Main Branch) | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
if: github.ref == 'refs/heads/main' | |
run: | | |
docker build --build-arg SPRING_PROFILE=dev -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
- name: AWS ECR Push | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
run: | | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | |
cd: | |
needs: ci | |
name: CD | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
contents: read | |
env: | |
AWS_REGION: ap-northeast-2 | |
steps: | |
# AWS 자격 증명 설정 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v3 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
# SSH로 EC2에 배포 | |
- name: Deploy to EC2 (Release Branch) | |
if: github.ref == 'refs/heads/release' | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.RELEASE_EC2_HOST }} | |
username: ubuntu | |
key: ${{ secrets.RELEASE_SSH_KEY }} | |
script: | | |
# 컨테이너 중지 및 제거 | |
sudo docker ps -q | xargs -r sudo docker stop || true | |
sudo docker ps -aq | xargs -r sudo docker rm || true | |
# 사용하지 않는 이미지 및 시스템 정리 | |
sudo docker images -q | xargs -r sudo docker rmi || true | |
sudo docker system prune -af || true | |
# ECR 로그인 | |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | sudo docker login --username AWS --password-stdin ${{ secrets.ECR_URI }} | |
# Docker 이미지 풀 | |
sudo docker pull ${{ secrets.ECR_URI }}:${{ github.sha }} | |
# 새 컨테이너 실행 | |
sudo docker run --name dotflow-dev-repo -d \ | |
-e SPRING_PROFILE=dev \ | |
-e TZ=Asia/Seoul \ | |
-e SERVER=true \ | |
-p 8080:8080 \ | |
${{ secrets.ECR_URI }}:${{ github.sha }} | |