Skip to content

chore: 프로덕션 배포 워크플로 작성 #2

chore: 프로덕션 배포 워크플로 작성

chore: 프로덕션 배포 워크플로 작성 #2

name: production Build And Deploy
on:
pull_request:
branches: [ "develop" ]
# push:
# branches: [ "develop" ]
jobs:
build:
runs-on: ubuntu-latest
environment: PROD
strategy:
matrix:
java-version: [ 17 ]
distribution: [ 'temurin' ]
outputs:
# IMAGE_TAG 환경 변수를 다른 Job에서 사용하기 위해 설정
image-tag: ${{ steps.image-tag.outputs.value }}
steps:
# 기본 체크아웃
- name: Checkout
uses: actions/checkout@v3
# JDK를 17 버전으로 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: ${{ matrix.distribution }}
# 이미지 태그 설정
- name: Set up image-tag by GITHUB_SHA
id: image-tag
run: echo "value=$(echo ${GITHUB_SHA::7})" >> $GITHUB_OUTPUT
# test 돌릴때 레디스 필요
- name: Start containers
run: docker-compose -f ./docker-compose-test.yaml up -d
# Gradlew 실행 허용
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew
# Gradle 빌드
- name: Build with Gradle
id: gradle
uses: gradle/gradle-build-action@v2
with:
arguments: |
build
--scan
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }}
# NCP Container Registry 로그인
- name: Login to NCP Container Registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.NCP_CONTAINER_REGISTRY }}
username: ${{ secrets.NCP_ACCESS_KEY }}
password: ${{ secrets.NCP_SECRET_KEY }}
# Docker 이미지 빌드 및 푸시
- name: Docker Build and Push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ secrets.NCP_CONTAINER_REGISTRY }}/server-spring:${{ steps.image-tag.outputs.value }}
# 서버로 docker-compose 파일 전송
- name: Copy docker-compose.yml to NCP Server
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.NCP_HOST }}
username: ${{ secrets.NCP_USERNAME }}
key: ${{ secrets.NCP_PRIVATE_KEY }}
port: ${{ secrets.NCP_PORT }}
source: docker-compose.yaml
target: /home/tenminute/
# 슬랙으로 빌드 스캔 결과 전송
- name: Send to slack
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "Gradle Build Scan Report of ${{ github.workflow }}: ${{ steps.gradle.outputs.build-scan-url }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
deploy:
runs-on: ubuntu-latest
environment: PROD
needs: build
steps:
- name: Deploy to NCP Server
uses: appleboy/ssh-action@master
env:
NCP_CONTAINER_REGISTRY: ${{ secrets.NCP_CONTAINER_REGISTRY }}
NCP_IMAGE_TAG: ${{ needs.build.outputs.image-tag }}
with:
host: ${{ secrets.NCP_HOST }}
username: ${{ secrets.NCP_USERNAME }}
key: ${{ secrets.NCP_PRIVATE_KEY }}
port: ${{ secrets.NCP_PORT }}
envs: NCP_CONTAINER_REGISTRY,NCP_IMAGE_TAG # docker-compose.yml 에서 사용할 환경 변수
script: |
echo "${{ secrets.NCP_SECRET_KEY }}" | docker login -u "${{ secrets.NCP_ACCESS_KEY }}" --password-stdin "${{ secrets.NCP_CONTAINER_REGISTRY }}"
docker pull ${{ secrets.NCP_CONTAINER_REGISTRY }}/server-spring:${{ env.NCP_IMAGE_TAG }}
docker compose -f /home/tenminute/docker-compose.yaml up -d
docker image prune -a -f