diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 35d641e..87e903a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,13 +1,11 @@ name: CI-pipeline on: workflow_dispatch: - push: - branches: - - dev - pull_request: branches: - main + types: + - opened jobs: linters: @@ -57,9 +55,6 @@ jobs: Repository: ${{ github.repository }} See changes: https://github.com/${{ github.repository }}/commit/${{github.sha}} - - name: Check fail - run: echo "Unreachable" - - name: Notify in telegram about success uses: appleboy/telegram-action@master with: diff --git a/.github/workflows/jenkins-deploy.yml b/.github/workflows/jenkins-deploy.yml new file mode 100644 index 0000000..e2eb589 --- /dev/null +++ b/.github/workflows/jenkins-deploy.yml @@ -0,0 +1,16 @@ +name: Deploy on server +on: + workflow_dispatch: + +jobs: + Deploy: + name: Deploy + runs-on: ubuntu-latest + steps: + - name: Trigger Jenkins jobs + uses: appleboy/jenkins-action@master + with: + url: "http://ci.saccada.xyz" + user: ${{ env.JENKINS_USERNAME }} + token: ${{ secrets.JENKINS_TOKEN }} + job: "Deploy Django application" diff --git a/Dockerfile b/Dockerfile index 8a6e1cf..52cafbc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,11 +3,8 @@ WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -COPY ./mysite ./mysite -COPY ./polls ./polls COPY ./manage.py . -RUN ./manage.py makemigrations # RUN ./manage.py migrate COPY docker-entrypoint.sh . diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..4891947 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,36 @@ +pipeline { + agent any + + + stages { + stage('Hello') { + steps { + echo 'Hello from Jenkins' + } + } + stage('Connect to server') { + steps { + withCredentials([ + sshUserPrivateKey( + credentialsId: '73d234d2-3f9d-44e9-97fc-b6317070b462', + keyFileVariable: 'SSH_PRIVATE_KEY' + ) + ]) { + sh '''#!/bin/bash + ssh -i "$SSH_PRIVATE_KEY" -p 2222 saccada@saccada.xyz << EOF + cd ~/ws/django-example-app + git pull --rebase + docker compose pull + docker compose down + docker compose up -d + ''' + } + } + } + } + post { + always { + sleep 5 + } + } +} diff --git a/compose.yaml b/compose.yaml index c6e96c4..ddb62be 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,10 +1,14 @@ services: - app: - container_name: wtukatyr/django-example-app:latest + django-app: + image: wtukatyr/django-example-app:latest + container_name: django-app ports: - "8989:8000" depends_on: - db + volumes: + - ./mysite:/app/mysite + - ./polls:/app/polls environment: DATABASE_NAME: ${DATABASE_NAME} DATABASE_USERNAME: ${DATABASE_USERNAME} @@ -14,7 +18,9 @@ services: env_file: - .env networks: - - proxy-common + - django_app_network + - proxy_common + db: image: postgres:14 restart: always @@ -30,13 +36,21 @@ services: volumes: - pg-data:/var/lib/postgresql/data environment: - POSTGRES_USER: sa - POSTGRES_PASSWORD: example - POSTGRES_PORT: 5432 + POSTGRES_DB: ${DATABASE_NAME} + POSTGRES_USER: ${DATABASE_USERNAME} + POSTGRES_PASSWORD: ${DATABASE_PASSWORD} + POSTGRES_PORT: ${DATABASE_PORT} + env_file: + - .env + networks: + - django_app_network + - proxy_common volumes: pg-data: networks: + django_app_network: + name: django_app_network proxy_common: external: true diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index b942dc5..48fa0a7 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -4,7 +4,10 @@ echo "Collect static files" python manage.py collectstatic --noinput + # Apply database migrations +echo "Make migrations" +RUN ./manage.py makemigrations echo "Apply database migrations" python manage.py migrate diff --git a/mysite/settings.py b/mysite/settings.py index 0a5c1ea..bc8a238 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -24,9 +24,10 @@ SECRET_KEY = os.getenv("DJANGO_SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False -ALLOWED_HOSTS = ["localhost"] +CSRF_TRUSTED_ORIGINS = ["https://app.saccada.xyz"] +ALLOWED_HOSTS = ["localhost", "app.saccada.xyz"] # Application definition