Skip to content

cd: 배포 테스트 위치 수정 #1

cd: 배포 테스트 위치 수정

cd: 배포 테스트 위치 수정 #1

Workflow file for this run

name: Docker Image CD
on:
push:
branches: [ 'dev' ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
# 소스 코드 체크아웃
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
# Docker Hub 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# SSH 접속을 위한 SSH 키 설정
- name: Set up SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
# 원격 서버의 SSH 호스트 키를 저장하여 SSH 연결 시 신뢰성을 확인하는 과정
- name: Add known hosts
run: ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts
- name: Check file changes
run: |
CLIENT_FILE_CHANGED=$(git diff --quiet ${{ github.event.before }} HEAD client && echo 'false' || echo 'true')
SERVER_FILE_CHANGED=$(git diff --quiet ${{ github.event.before }} HEAD server && echo 'false' || echo 'true')
if [ "$CLIENT_FILE_CHANGED" == 'false' ] && [ "$SERVER_FILE_CHANGED" == 'false' ]; then
echo "CLIENT_FILE_CHANGED=true" > $GITHUB_ENV
echo "SERVER_FILE_CHANGED=true" >> $GITHUB_ENV
else
echo "CLIENT_FILE_CHANGED=$CLIENT_FILE_CHANGED" > $GITHUB_ENV
echo "SERVER_FILE_CHANGED=$SERVER_FILE_CHANGED" >> $GITHUB_ENV
fi
# 서버 이미지 빌드 및 태그 지정
- name: Build and tag Docker image
run: |
if [ "${{ env.SERVER_FILE_CHANGED }}" == 'true' ]; then
docker build -f server/Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/inear-server:${{ github.sha }} ./
docker tag ${{ secrets.DOCKER_USERNAME }}/inear-server:${{ github.sha }} ${{ secrets.DOCKER_USERNAME }}/inear-server:latest
fi
# 서버 이미지 푸시 (Docker Hub 또는 다른 레지스트리)
- name: Push server image to Docker Hub
run: |
if [ "${{ env.SERVER_FILE_CHANGED }}" == 'true' ]; then
docker push ${{ secrets.DOCKER_USERNAME }}/inear-server:${{ github.sha }}
docker push ${{ secrets.DOCKER_USERNAME }}/inear-server:latest
fi
# 클라이언트 및 nginx 이미지 빌드 및 태그 지정
- name: Build and tag client image
run: |
if [ "${{ env.CLIENT_FILE_CHANGED }}" == 'true' ]; then
yarn --cwd ./client install
echo ${{ secrets.CLIENT_ENV_PRODUCTION }} > ./client/.env.production
echo ${{ secrets.CLIENT_ENV_DEVELOPMENT }} > ./client/.env.development
yarn --cwd ./client build
scp -r ./client/dist/ ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }}:/${{ secrets.SSH_USER }}/web18-inear-test/nginx/html/
fi
# Docker Compose로 전체 애플리케이션 빌드 및 실행 (테스트용)
- name: Run Docker Compose for testing
run: |
docker compose up --build -d
continue-on-error: false
# 서버에서 파일을 최신화 하고, 배포 스크립트를 통해 실제 배포 진행
- name: Deploy with Docker Compose on server
run: |
ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} " \
SERVER_FILE_CHANGED=${{ env.SERVER_FILE_CHANGED }} && \
CLIENT_FILE_CHANGED=${{ env.CLIENT_FILE_CHANGED }} && \
mkdir -p /${{ secrets.SSH_USER }}/web18-inear-test && \
cd /${{ secrets.SSH_USER }}/web18-inear-test && \
docker system prune -af && \
if [ \"\$SERVER_FILE_CHANGED\" == true ]; then
docker pull ${{ secrets.DOCKER_USERNAME }}/inear-server:latest && \
./backend-deploy.sh
fi
if [ \"\$CLIENT_FILE_CHANGED\" == true ]; then
./frontend-deploy.sh
fi
"