diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 14d2e3e6..00d4ce73 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -2,12 +2,15 @@ name: 이미지 빌드 & 푸쉬 on: workflow_call: inputs: + image-name: + required: true + type: string image-tag: - required: true - type: string + required: true + type: string spring-profile: - required: true - type: string + required: true + type: string secrets: DOCKER_USERNAME: required: true @@ -17,6 +20,7 @@ on: env: IMAGE_TAG: ${{ inputs.image-tag || 'latest' }} ACTIVE_PROFILE: ${{ inputs.spring-profile || 'local' }} + IMAGE_NAME: ${{ inputs.image-name }} jobs: build: @@ -46,6 +50,6 @@ jobs: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} run: | - echo "IMAGE_TAG=$IMAGE_TAG, ACTIVE_PROFILE=$ACTIVE_PROFILE" && + echo "IMAGE_TAG=$IMAGE_TAG, ACTIVE_PROFILE=$ACTIVE_PROFILE, IMAGE_NAME=$IMAGE_NAME" && chmod +x ./gradlew && ./gradlew jib diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 7d283588..34836446 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,27 +1,43 @@ -name: 이미지 빌드 & 푸쉬 +name: 서비스 배포 on: workflow_call: + secrets: + REMOTE_SSH_HOST: + required: true + REMOTE_SSH_USERNAME: + required: true + REMOTE_SSH_KEY: + required: true + REMOTE_SSH_PASSPHRASE: + required: true + REMOTE_SSH_PORT: + required: true inputs: + environment: + required: true + type: string image-tag: required: true type: string spring-profile: required: true type: string - secrets: - DOCKER_USERNAME: + image-name: required: true - DOCKER_PASSWORD: + type: string + service-name: required: true + type: string env: - IMAGE_TAG: ${{ github.event.inputs.image-tag || 'latest' }} - ACTIVE_PROFILE: ${{ github.event.inputs.spring-profile || 'local' }} + IMAGE_TAG: ${{ inputs.image-tag || 'latest' }} + ACTIVE_PROFILE: ${{ inputs.spring-profile || 'local' }} jobs: - build: + deploy: runs-on: [ ubuntu-latest ] - name: 이미지 빌드하기 + name: 서비스 배포하기 + environment: ${{ inputs.environment }} permissions: id-token: write @@ -31,20 +47,13 @@ jobs: - name: GitHub 에서 레포 받아오기 uses: actions/checkout@v3 - - name: JDK17 준비하기 - uses: actions/setup-java@v3 + - name: 배포 스크립트 실행 + uses: appleboy/ssh-action@master with: - java-version: '17' - distribution: 'temurin' - - - name: Gradle 애드온 준비하기 - uses: gradle/gradle-build-action@v2 - - - name: 이미지 빌드하고 푸쉬하기 - id: build-image - env: - DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} - DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} - run: | - chmod +x ./gradlew && - ./gradlew jib + host: ${{ secrets.REMOTE_SSH_HOST }} + username: ${{ secrets.REMOTE_SSH_USERNAME }} + key: ${{ secrets.REMOTE_SSH_KEY }} + password: ${{ secrets.REMOTE_SSH_PASSPHRASE }} + port: ${{ secrets.REMOTE_SSH_PORT }} + script: | + docker service update --image ${{ inputs.image-name }}:${{ inputs.image-tag }} ${{ inputs.service-name }} diff --git a/.github/workflows/dev.yaml b/.github/workflows/dev.yaml new file mode 100644 index 00000000..9419d69f --- /dev/null +++ b/.github/workflows/dev.yaml @@ -0,0 +1,79 @@ +name: 데브 워크플로우 +on: + push: + branches: [ 'dev' ] + paths: + - 'config/**' # Config 모듈 변경 + - 'member/**' # Member 모듈 변뚶 + - '.github/workflows/**' # 워크플로우와 관련된 파일이 변경된 경우 + - 'build.gradle' # Parent Gradle 모듈 설정이 변경된 경우 + - 'settings.gradle' # Parent Gradle 설정이 변경된 경우 + +env: + SPRING_PROFILE: dev + ENVIRONMENT: development + IMAGE_NAME: no5ing/spring-was-dev + SERVICE_NAME: be-dev-was + +concurrency: + group: development + +jobs: + prepare-variables: + name: 환경변수 준비하기 + runs-on: ubuntu-latest + outputs: + image-tag: ${{ steps.setup-env.outputs.image-tag }} + spring-profile: ${{ steps.setup-env.outputs.spring-profile }} + environment: ${{ steps.setup-env.outputs.environment }} + image-name: ${{ steps.setup-env.outputs.image-name }} + service-name: ${{ steps.setup-env.outputs.service-name }} + steps: + - name: GitHub 에서 레포 받아오기 + uses: actions/checkout@v3 + + - name: 환경변수 출력하기 + id: setup-env + run: | + echo "image-tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + echo "spring-profile=$SPRING_PROFILE" >> $GITHUB_OUTPUT + echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT + echo "image-name=$IMAGE_NAME" >> $GITHUB_OUTPUT + echo "service-name=$SERVICE_NAME" >> $GITHUB_OUTPUT + + call-build-workflow: + if: github.event_name == 'push' + needs: [ prepare-variables ] + name: 이미지 빌드 + uses: ./.github/workflows/build.yaml + permissions: + id-token: write + contents: read + with: + image-tag: ${{ needs.prepare-variables.outputs.image-tag }} + spring-profile: ${{ needs.prepare-variables.outputs.spring-profile }} + image-name: ${{ needs.prepare-variables.outputs.image-name }} + secrets: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + + call-deploy-workflow: + if: github.event_name == 'push' + needs: [ prepare-variables, call-build-workflow ] + name: 서비스 배포 + uses: ./.github/workflows/deploy.yaml + permissions: + id-token: write + contents: read + secrets: + REMOTE_SSH_HOST: ${{ secrets.REMOTE_SSH_HOST }} + REMOTE_SSH_USERNAME: ${{ secrets.REMOTE_SSH_USERNAME }} + REMOTE_SSH_KEY: ${{ secrets.REMOTE_SSH_KEY }} + REMOTE_SSH_PASSPHRASE: ${{ secrets.REMOTE_SSH_PASSPHRASE }} + REMOTE_SSH_PORT: ${{ secrets.REMOTE_SSH_PORT }} + with: + image-tag: ${{ needs.prepare-variables.outputs.image-tag }} + spring-profile: ${{ needs.prepare-variables.outputs.spring-profile }} + environment: ${{ needs.prepare-variables.outputs.environment }} + image-name: ${{ needs.prepare-variables.outputs.image-name }} + service-name: ${{ needs.prepare-variables.outputs.service-name }} diff --git a/.github/workflows/prod.yaml b/.github/workflows/prod.yaml index 24804c7d..046c98e2 100644 --- a/.github/workflows/prod.yaml +++ b/.github/workflows/prod.yaml @@ -1,16 +1,19 @@ name: 프로덕션 워크플로우 on: push: - branches: [ 'main', 'feature/OING-7' ] + branches: [ 'main' ] paths: - 'config/**' # Config 모듈 변경 - - 'member/**' # Member 모듈 변경 + - 'member/**' # Member 모듈 변뚶 - '.github/workflows/**' # 워크플로우와 관련된 파일이 변경된 경우 - 'build.gradle' # Parent Gradle 모듈 설정이 변경된 경우 - 'settings.gradle' # Parent Gradle 설정이 변경된 경우 env: SPRING_PROFILE: prod + ENVIRONMENT: production + IMAGE_NAME: no5ing/spring-was + SERVICE_NAME: be-prod-was concurrency: group: production @@ -22,6 +25,9 @@ jobs: outputs: image-tag: ${{ steps.setup-env.outputs.image-tag }} spring-profile: ${{ steps.setup-env.outputs.spring-profile }} + environment: ${{ steps.setup-env.outputs.environment }} + image-name: ${{ steps.setup-env.outputs.image-name }} + service-name: ${{ steps.setup-env.outputs.service-name }} steps: - name: GitHub 에서 레포 받아오기 uses: actions/checkout@v3 @@ -31,6 +37,9 @@ jobs: run: | echo "image-tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT echo "spring-profile=$SPRING_PROFILE" >> $GITHUB_OUTPUT + echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT + echo "image-name=$IMAGE_NAME" >> $GITHUB_OUTPUT + echo "service-name=$SERVICE_NAME" >> $GITHUB_OUTPUT call-build-workflow: if: github.event_name == 'push' @@ -43,6 +52,28 @@ jobs: with: image-tag: ${{ needs.prepare-variables.outputs.image-tag }} spring-profile: ${{ needs.prepare-variables.outputs.spring-profile }} + image-name: ${{ needs.prepare-variables.outputs.image-name }} secrets: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + + call-deploy-workflow: + if: github.event_name == 'push' + needs: [ prepare-variables, call-build-workflow ] + name: 서비스 배포 + uses: ./.github/workflows/deploy.yaml + permissions: + id-token: write + contents: read + secrets: + REMOTE_SSH_HOST: ${{ secrets.REMOTE_SSH_HOST }} + REMOTE_SSH_USERNAME: ${{ secrets.REMOTE_SSH_USERNAME }} + REMOTE_SSH_KEY: ${{ secrets.REMOTE_SSH_KEY }} + REMOTE_SSH_PASSPHRASE: ${{ secrets.REMOTE_SSH_PASSPHRASE }} + REMOTE_SSH_PORT: ${{ secrets.REMOTE_SSH_PORT }} + with: + image-tag: ${{ needs.prepare-variables.outputs.image-tag }} + spring-profile: ${{ needs.prepare-variables.outputs.spring-profile }} + environment: ${{ needs.prepare-variables.outputs.environment }} + image-name: ${{ needs.prepare-variables.outputs.image-name }} + service-name: ${{ needs.prepare-variables.outputs.service-name }} diff --git a/config/build.gradle b/config/build.gradle index 8a33061d..0fecdbc9 100644 --- a/config/build.gradle +++ b/config/build.gradle @@ -34,11 +34,12 @@ jib { def activeProfile = System.getenv("ACTIVE_PROFILE") def dockerUsername = System.getenv("DOCKER_USERNAME") def dockerPassword = System.getenv("DOCKER_PASSWORD") + def imageName = System.getenv("IMAGE_NAME") from { image = 'amazoncorretto:17-alpine3.17-jdk' } to { - image = 'no5ing/spring-was:latest' + image = imageName + ':latest' tags = ['latest', imageTag] auth { username = dockerUsername ? dockerUsername : ""