Merge pull request #71 from JongbeomLee623/feature/ljb-deploy #106
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Web Application CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
pull_request_target: | |
types: [closed] | |
jobs: | |
CI: | |
runs-on: ubuntu-latest | |
services: | |
db: | |
image: mysql:8.0 | |
env: | |
MYSQL_ROOT_PASSWORD: ${{ secrets.DATABASE_USER_PASSWORD }} | |
MYSQL_DATABASE: ${{ secrets.DATABASE_NAME }} | |
MYSQL_USER: ${{ secrets.DATABASE_USER }} | |
MYSQL_PASSWORD: ${{ secrets.DATABASE_USER_PASSWORD }} | |
ports: | |
- 3306:3306 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Create .env file | |
run: | | |
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env | |
echo "DEBUG=${{ secrets.DEBUG }}" >> .env | |
echo "DJANGO_DEPLOY=${{ secrets.DJANGO_DEPLOY }}" >> .env | |
echo "DATABASE_ENGINE=${{ secrets.DATABASE_ENGINE }}" >> .env | |
echo "DATABASE_NAME=${{ secrets.DATABASE_NAME }}" >> .env | |
echo "DATABASE_USER=${{ secrets.DATABASE_USER }}" >> .env | |
echo "DATABASE_USER_PASSWORD=${{ secrets.DATABASE_USER_PASSWORD }}" >> .env | |
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env | |
echo "DATABASE_PORT=${{ secrets.DATABASE_PORT }}" >> .env | |
echo "CELERY_BROKER_URL=${{ secrets.CELERY_BROKER_URL }}" >> .env | |
echo "CELERY_RESULT_BACKEND=${{ secrets.CELERY_RESULT_BACKEND }}" >> .env | |
echo "KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }}" >> .env | |
echo "KAKAO_CALLBACK_URI=${{ secrets.KAKAO_CALLBACK_URI }}" >> .env | |
echo "BACK_BASE_URL=${{ secrets.BACK_BASE_URL }}" >> .env | |
echo "KAKAO_SECRET=${{ secrets.KAKAO_SECRET }}" >> .env | |
cat .env | |
- name: Set up Python 3.12 | |
uses: actions/setup-python@v3 | |
with: | |
python-version: '3.12' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Run migrations and tests | |
run: | | |
cat .env | |
python manage.py makemigrations | |
python manage.py migrate | |
python manage.py test | |
build_and_push: | |
needs: CI | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPONAME }}:${{ GITHUB.SHA }} | |
deploy: | |
needs: build_and_push | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true | |
steps: | |
- name: SSH into server and pull Docker image | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
cd /root/linenow-dev | |
git pull origin main | |
docker-compose down | |
docker-compose pull | |
docker-compose up --build -d | |
- name: Collect static files | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
docker exec linenow-was python manage.py collectstatic --noinput | |
- name: Cleanup Docker resources on server # Docker 자원 정리 작업 추가 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
docker system prune -f |