Merge remote-tracking branch 'refs/remotes/origin/stage' into dev #10
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: Deploy to Server | |
on: | |
push: | |
branches: | |
- dev | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set deployment parameters | |
id: vars | |
run: | | |
if [ "${{ github.ref_name }}" == "dev" ]; then | |
echo "DEPLOY_DIR=OpenAssistants-dev" >> $GITHUB_ENV | |
echo "ASGI_SERVER=dev_uvicorn" >> $GITHUB_ENV | |
elif [ "${{ github.ref_name }}" == "main" ]; then | |
echo "DEPLOY_DIR=OpenAssistants-main" >> $GITHUB_ENV | |
echo "ASGI_SERVER=main_uvicorn" >> $GITHUB_ENV | |
fi | |
- name: Add SSH Key | |
run: | | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key.pem | |
chmod 600 private_key.pem | |
- name: Deploy to Server | |
env: | |
DEPLOY_DIR: ${{ env.DEPLOY_DIR }} | |
ASGI_SERVER: ${{ env.ASGI_SERVER }} | |
run: | | |
ssh -i private_key.pem -o StrictHostKeyChecking=no [email protected] <<EOF | |
# Navigate to the deployment directory | |
cd /home/ubuntu/$DEPLOY_DIR | |
# Fetch and reset code | |
git fetch origin | |
git reset --hard origin/${{ github.ref_name }} | |
# Activate virtual environment | |
if [ -f "venv/bin/activate" ]; then | |
source venv/bin/activate | |
else | |
python3 -m venv venv | |
source venv/bin/activate | |
fi | |
# Install requirements | |
pip install -r requirements.txt | |
# Django commands | |
python manage.py migrate --noinput || { echo "Migrations failed"; exit 1; } | |
python manage.py collectstatic --noinput || { echo "Collectstatic failed"; exit 1; } | |
# Restart the server | |
sudo supervisorctl restart $ASGI_SERVER | |
EOF |