Skip to content

feat: Goal 및 관련 엔티티 #4

feat: Goal 및 관련 엔티티

feat: Goal 및 관련 엔티티 #4

Workflow file for this run

name: CD
on:
workflow_dispatch:
push:
branches: [ "main" ]
permissions:
contents: read
# 배포 환경에 따라 변경
env:
EC2_USERNAME: ubuntu
DOCKER_REPOSITORY: goalmate
ENV_FILE_PATH: ./src/main/resources
ENV_FILE_NAME: env.yaml
jobs:
deploy-with-docker:
runs-on: ubuntu-latest
steps:
- name: Set Dynamic Environment Variables
run: echo "CURRENT_TIME=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# Gradle caching
- name: Gradle Caching
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# Copy environment file
- name: Copy environment file
run: |
mkdir -p ${{ env.ENV_FILE_PATH}}
echo "${{ secrets.ENV_FILE }}" > ${{ env.ENV_FILE_PATH}}/${{ env.ENV_FILE_NAME }}
# Grant execute permission for gradlew
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Build with Gradle
- name: Build with Gradle
run: ./gradlew build -x test
# Docker Hub 로그인
- name: Docker Hub 로그인
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_ID }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Docker build & push
- name: Docker build & Push
run: |
# 이미지 빌드
docker build -f Dockerfile -t ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:${CURRENT_TIME} .
# latest 태그도 동시에 푸시
docker tag ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:${CURRENT_TIME} ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:latest
# Docker Hub에 푸시
docker push ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:${CURRENT_TIME}
docker push ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:latest
# 태그 출력 (디버깅용)
echo "Docker image pushed with tag: ${TAG}"
# Send deploy related files
- name: Send deploy scripts
uses: appleboy/scp-action@master
with:
username: ${{ env.EC2_USERNAME }}
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
port: 22
source: "./deploy/*"
target: "~"
# Deploy to Dev
- name: Deploy to Dev
uses: appleboy/ssh-action@master
with:
username: ${{ env.EC2_USERNAME }}
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
docker pull ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:${CURRENT_TIME}
docker service update --image ${{ secrets.DOCKER_ID }}/${{ env.DOCKER_REPOSITORY }}:${TAG} goalmate-stack_goalmate
cd deploy
chmod 777 ./deploy.sh
# Swarm 초기화 (이미 초기화된 경우 에러 무시)
docker swarm init || true
# 스택 배포
docker stack deploy -c docker-compose.yaml goalmate-stack
# 필요 없는 이미지 삭제
docker image prune -af