|
| 1 | +name: CI/CD — Backend (Build → Push → Deploy) |
| 2 | + |
| 3 | +on: |
| 4 | + # dev에 머지 전 ci push 대상 브랜치 중첩 적용 (release, dev) |
| 5 | + push: |
| 6 | + branches: [ release, dev ] |
| 7 | + pull_request: |
| 8 | + branches: [ dev ] |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + tag: |
| 12 | + description: "배포할 이미지 태그 (ex. release or <GIT_SHA>)" |
| 13 | + required: false |
| 14 | + default: "release" |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: cicd-${{ github.ref_name }} |
| 18 | + cancel-in-progress: false |
| 19 | + |
| 20 | +jobs: |
| 21 | + build-and-push: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + env: |
| 27 | + IMAGE: ghcr.io/prgrms-web-devcourse-final-project/docsa-backend |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Set up JDK |
| 32 | + uses: actions/setup-java@v4 |
| 33 | + with: |
| 34 | + java-version: '21' |
| 35 | + distribution: 'temurin' |
| 36 | + |
| 37 | + - name: Cache Gradle |
| 38 | + uses: gradle/actions/setup-gradle@v3 |
| 39 | + |
| 40 | + - name: Detect project dir |
| 41 | + id: detect |
| 42 | + shell: bash |
| 43 | + run: | |
| 44 | + if [ -f "./gradlew" ] || [ -f "./build.gradle" ] || [ -f "./build.gradle.kts" ]; then |
| 45 | + echo "dir=." >> $GITHUB_OUTPUT |
| 46 | + elif [ -d "./backend" ]; then |
| 47 | + echo "dir=backend" >> $GITHUB_OUTPUT |
| 48 | + else |
| 49 | + echo "No Gradle project found"; exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Test |
| 53 | + env: |
| 54 | + MAIL_PASSWORD: ${{ secrets.CI_MAIL_PASSWORD }} |
| 55 | + MAIL_USERNAME: ${{ secrets.CI_MAIL_USERNAME }} |
| 56 | + MONGO_URI: ${{ secrets.CI_MONGO_URI }} |
| 57 | + run: | |
| 58 | + cd "${{ steps.detect.outputs.dir }}" |
| 59 | + chmod +x ./gradlew || true |
| 60 | + ./gradlew clean test --no-daemon |
| 61 | +
|
| 62 | + - name: Find Dockerfile |
| 63 | + id: df |
| 64 | + run: | |
| 65 | + if [ -f "infra/backend/Dockerfile" ]; then |
| 66 | + echo "path=infra/backend/Dockerfile" >> $GITHUB_OUTPUT |
| 67 | + echo "ctx=." >> $GITHUB_OUTPUT |
| 68 | + else |
| 69 | + echo "No Dockerfile found"; exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + - name: Build image (PR only) |
| 73 | + if: github.event_name == 'pull_request' |
| 74 | + run: | |
| 75 | + docker build -f "${{ steps.df.outputs.path }}" -t sanity-check:pr "${{ steps.df.outputs.ctx }}" |
| 76 | +
|
| 77 | + - name: Log in to GHCR |
| 78 | + if: github.event_name == 'push' |
| 79 | + uses: docker/login-action@v3 |
| 80 | + with: |
| 81 | + registry: ghcr.io |
| 82 | + username: ${{ github.actor }} |
| 83 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + |
| 85 | + - name: Build & Push image |
| 86 | + if: github.event_name == 'push' |
| 87 | + run: | |
| 88 | + GIT_SHA=${{ github.sha }} |
| 89 | + docker build -f "${{ steps.df.outputs.path }}" -t $IMAGE:release -t $IMAGE:$GIT_SHA "${{ steps.df.outputs.ctx }}" |
| 90 | + docker push $IMAGE:release |
| 91 | + docker push $IMAGE:$GIT_SHA |
| 92 | +
|
| 93 | + deploy: |
| 94 | + needs: build-and-push |
| 95 | + # dev에 머지 전 cd 대상 브랜치 중첩 적용 (release, dev) |
| 96 | + if: github.event_name == 'push' && (github.ref == 'refs/heads/release' || github.ref == 'refs/heads/dev') && |
| 97 | + (needs.build-and-push.result == 'success') |
| 98 | + || (github.event_name == 'workflow_dispatch') |
| 99 | + runs-on: ubuntu-latest |
| 100 | + |
| 101 | + steps: |
| 102 | + - name: Decide tag |
| 103 | + id: tag |
| 104 | + run: | |
| 105 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.tag }}" ]; then |
| 106 | + echo "value=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT |
| 107 | + else |
| 108 | + echo "value=release" >> $GITHUB_OUTPUT |
| 109 | + fi |
| 110 | +
|
| 111 | + - name: Deploy |
| 112 | + uses: appleboy/ssh-action@v1.0.3 |
| 113 | + with: |
| 114 | + host: ${{ secrets.CD_HOST }} |
| 115 | + username: ${{ secrets.CD_USER }} |
| 116 | + key: ${{ secrets.CD_SSH_KEY }} |
| 117 | + port: ${{ secrets.CD_PORT }} |
| 118 | + script: | |
| 119 | + export DEPLOY_TAG='${{ steps.tag.outputs.value }}' |
| 120 | + bash -lc '/srv/docsa/infra/deploy.sh' |
0 commit comments