Infra: 배포 워크플로우 수정 #22
This file contains hidden or 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 | |
| on: | |
| push: | |
| paths: | |
| - '.github/workflows/**' | |
| - 'src/**' | |
| - 'build.gradle.kts' | |
| - 'settings.gradle.kts' | |
| - 'Dockerfile' | |
| branches: | |
| - main # main 브랜치로 머지되면 실행 | |
| jobs: | |
| makeTagAndRelease: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| tag_name: ${{ steps.create_tag.outputs.new_tag }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Create Tag | |
| id: create_tag | |
| uses: mathieudutour/[email protected] | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create Release | |
| id: create_release | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: ${{ steps.create_tag.outputs.new_tag }} | |
| release_name: Release ${{ steps.create_tag.outputs.new_tag }} | |
| body: ${{ steps.create_tag.outputs.changelog }} | |
| draft: false | |
| prerelease: false | |
| buildImageAndPush: | |
| name: 도커 이미지 빌드와 푸시 | |
| needs: makeTagAndRelease | |
| runs-on: ubuntu-latest | |
| env: | |
| DOCKER_IMAGE_NAME: catfe-backend | |
| DOT_ENV: ${{ secrets.DOT_ENV }} | |
| OWNER: ${{ github.repository_owner }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: .env 생성 | |
| run: echo "$DOT_ENV" > .env | |
| - name: Docker Buildx 설치 | |
| uses: docker/setup-buildx-action@v2 | |
| - name: 레지스트리 로그인 | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: set lower case owner name | |
| run: | | |
| echo "OWNER_LC=${OWNER,,}" >> ${GITHUB_ENV} | |
| - name: 빌드 앤 푸시 | |
| uses: docker/build-push-action@v3 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| ghcr.io/${{ env.OWNER_LC }}/${{ env.DOCKER_IMAGE_NAME }}:${{ needs.makeTagAndRelease.outputs.tag_name }}, | |
| ghcr.io/${{ env.OWNER_LC }}/${{ env.DOCKER_IMAGE_NAME }}:latest | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: [ buildImageAndPush ] | |
| env: | |
| OWNER: ${{ github.repository_owner }} | |
| steps: | |
| - name: set lower case owner name | |
| run: | | |
| echo "OWNER_LC=${OWNER,,}" >> ${GITHUB_ENV} | |
| - name: AWS SSM Send-Command | |
| uses: peterkimzz/aws-ssm-send-command@master | |
| id: ssm | |
| with: | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| instance-ids: "i-00e384163ab61f6cc" | |
| working-directory: / | |
| comment: Deploy | |
| command: | | |
| # 0. env 변수 확인 | |
| echo "OWNER_LC = ${{ env.OWNER_LC }}" | |
| # 1. 최신 이미지 pull | |
| docker pull ghcr.io/${{ env.OWNER_LC }}/catfe-backend:latest | |
| # 2. 기존 컨테이너 종료 및 제거 | |
| docker stop catfe-backend 2>/dev/null | |
| docker rm catfe-backend 2>/dev/null | |
| # 3. 새로운 컨테이너 실행 | |
| docker run -d --name catfe-backend -p 8080:8080 ghcr.io/${{ env.OWNER_LC }}/catfe-backend:latest | |
| # 4. dangling 이미지 삭제 | |
| docker rmi $(docker images -f "dangling=true" -q) |