Skip to content

Create django-deploy.yml #1

Create django-deploy.yml

Create django-deploy.yml #1

Workflow file for this run

name: Web Application CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
types: [closed]
jobs:
CI:
runs-on: ubuntu-latest
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 "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env
echo "DB_USER=${{ secrets.DB_USER }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .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: |
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 }}:latest
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 /path/to/project
docker-compose down
docker-compose pull
docker-compose up -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 -it linenow-was python manage.py collectstatic --noinput