diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..d929ef98 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,94 @@ +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/github-tag-action@v6.2 + 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=${{ toLower(env.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 ] + steps: + - 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: | + docker pull ghcr.io/${{ env.OWNER_LC }}/catfe-backend:latest + docker stop catfe-backend 2>/dev/null + docker rm catfe-backend 2>/dev/null + docker run -d --name catfe-backend -p 8080:8080 ghcr.io/${{ env.OWNER_LC }}/catfe-backend:latest \ No newline at end of file