Skip to content

test ci 30 проверка деплоя #42

test ci 30 проверка деплоя

test ci 30 проверка деплоя #42

Workflow file for this run

#name: Django CI
#
#on: [push, pull_request]
#
#jobs:
# lint:
# 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.10'
#
# - name: Install flake8
# run: |
# python -m pip install --upgrade pip
# pip install flake8
#
# - name: Run Flake8
# run: flake8 .
#
# test:
# runs-on: ubuntu-latest
# needs: lint
#
# services:
# postgres:
# image: postgres:16
# env:
# POSTGRES_DB: testdb
# POSTGRES_USER: postgres
# POSTGRES_PASSWORD: postgres
# options: >-
# --health-cmd pg_isready
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5
# ports:
# - 5432:5432
#
# steps:
# - name: Check out code
# uses: actions/checkout@v3
#
# - name: Set up Python
# uses: actions/setup-python@v4
# with:
# python-version: '3.10'
#
# - name: Cache pip
# uses: actions/cache@v3
# with:
# path: ~/.cache/pip
# key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
# restore-keys: ${{ runner.os }}-pip-
#
# - name: Install dependencies
# run: |
# python -m pip install --upgrade pip
# pip install -r requirements.txt
#
# - name: Run migrations
# env:
# DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
# run: python manage.py migrate
#
# - name: Run tests
# env:
# DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
# run: python manage.py test
#
# build:
# runs-on: ubuntu-latest
# needs: test
#
# steps:
# - name: Check out code
# uses: actions/checkout@v3
#
# - name: Log in to Docker Hub
# run: echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
#
# - name: Build Docker image
# run: docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }} .
#
# - name: Push Docker image to Docker Hub
# run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }}
#
# deploy:
# runs-on: ubuntu-latest
# needs: build
#
# steps:
# - name: Set up SSH
# uses: webfactory/ssh-agent@v0.8.0
# with:
# ssh-private-key: ${{ secrets.SSH_KEY_SERVER }}
#
# - name: Deploy to server
# run: |
# ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
# docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }}
# docker stop library || true
# docker rm library || true
# docker run -d --name library -p 80:8000 ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }}
# EOF
name: Django CI/CD Pipeline
on: [push, pull_request]
jobs:
lint:
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.10'
- name: Install flake8
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Run Flake8
run: flake8 .
test:
runs-on: ubuntu-latest
needs: lint
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run migrations
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
run: python manage.py migrate
- name: Run tests
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb
run: python manage.py test
deploy:
runs-on: ubuntu-latest
needs: test
steps:
- name: Set up SSH
uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SSH_KEY_SERVER }}
- name: Copy project files to server
run: |
rsync -avz --exclude '__pycache__' . ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }}:${{ secrets.DEPLOY_DIR }}
- name: Install dependencies on server
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
cd ${{ secrets.DEPLOY_DIR }}
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
EOF
- name: Apply migrations on server
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
cd ${{ secrets.DEPLOY_DIR }}
source venv/bin/activate
python manage.py migrate
EOF
- name: Collect static files on server
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
cd ${{ secrets.DEPLOY_DIR }}
source venv/bin/activate
python manage.py collectstatic --noinput
EOF
- name: Restart application
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
sudo systemctl restart myapp.service
EOF