diff --git a/.env-example b/.env-example index 2edd7cb..86cae68 100644 --- a/.env-example +++ b/.env-example @@ -5,3 +5,6 @@ DB_USER=postgres DB_PASSWORD=your-password DB_HOST=db # This is the service name defined in docker-compose.yml DB_PORT=5432 +ADMIN_USERNAME=admin +ADMIN_EMAIL=email@domain.com +ADMIN_PASSWORD=admin_password diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..04d1984 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,41 @@ +name: Deploy with Docker on VPS + +on: + push: + branches: [ main ] + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Deploy to VPS + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USER }} + key: ${{ secrets.SSH_KEY }} + port: 22 + script: | + if [ -d "~/PhriniFluentBackend" ]; then + cd ~/PhriniFluentBackend + git pull origin main + else + git clone https://github.com/Flagro/PhriniFluentBackend.git ~/PhriniFluentBackend + cd ~/PhriniFluentBackend + fi + cp .env-example .env + echo "ENVIRONMENT=deployment" >> .env + echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env + echo "DB_USER=${{ secrets.DB_USER }}" >> .env + echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env + echo "ADMIN_USERNAME=${{ secrets.ADMIN_USERNAME }}" >> .env + echo "ADMIN_EMAIL=${{ secrets.ADMIN_EMAIL }}" >> .env + echo "ADMIN_PASSWORD=${{ secrets.ADMIN_PASSWORD }}" >> .env + sudo ../limited-docker-compose.sh -f docker-compose.prod.yml build + sudo ../limited-docker-compose.sh -f docker-compose.prod.yml up -d + sudo ../limited-docker-compose.sh -f docker-compose.prod.yml exec web python manage.py migrate + sudo ../limited-docker-compose.sh -f docker-compose.prod.yml exec web python manage.py collectstatic --noinput + sudo ../limited-docker-compose.sh -f docker-compose.prod.yml exec web python manage.py createadmin diff --git a/core/settings.py b/core/settings.py index a275e9c..a62eb4f 100644 --- a/core/settings.py +++ b/core/settings.py @@ -167,3 +167,8 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' CORS_ALLOW_ALL_ORIGINS = True + +# Admin user +ADMIN_USERNAME = config('ADMIN_USERNAME') +ADMIN_EMAIL = config('ADMIN_EMAIL') +ADMIN_PASSWORD = config('ADMIN_PASSWORD') diff --git a/phrini_fluent_users/management/commands/createadmin.py b/phrini_fluent_users/management/commands/createadmin.py new file mode 100644 index 0000000..96872b6 --- /dev/null +++ b/phrini_fluent_users/management/commands/createadmin.py @@ -0,0 +1,22 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model +from django.conf import settings + +class Command(BaseCommand): + help = 'Creates an admin user if it does not exist' + + def handle(self, *args, **options): + User = get_user_model() + admin_username = settings.ADMIN_USERNAME + admin_email = settings.ADMIN_EMAIL + admin_password = settings.ADMIN_PASSWORD + + if not User.objects.filter(username=admin_username).exists(): + User.objects.create_superuser( + username=admin_username, + email=admin_email, + password=admin_password + ) + self.stdout.write(self.style.SUCCESS('Successfully created new admin user')) + else: + self.stdout.write(self.style.SUCCESS('Admin user already exists'))