fix: solve eternal ssh session on ci.yml #15
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.12' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Set up Django environment | |
run: | | |
python manage.py migrate | |
python manage.py collectstatic --noinput | |
- name: Run tests | |
run: | | |
python manage.py test | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
if: github.ref == 'refs/heads/main' | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.12' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Add SSH key | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.DEPLOY_SERVER }} >> ~/.ssh/known_hosts | |
- name: Deploy to Server | |
env: | |
DEPLOY_SERVER: ${{ secrets.DEPLOY_SERVER }} | |
DEPLOY_USER: ${{ secrets.DEPLOY_USER }} | |
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} | |
run: | | |
ssh -i ~/.ssh/id_rsa $DEPLOY_USER@$DEPLOY_SERVER << EOF | |
cd $DEPLOY_PATH | |
pwd | |
ls -la | |
git pull | |
source env/bin/activate | |
pip install -r requirements.txt | |
python manage.py migrate | |
python manage.py collectstatic --noinput | |
pkill -f "gunicorn" || true | |
nohup gunicorn -c ./configs/gunicorn.py LeonardoCosta.wsgi &>/dev/null & | |
EOF |