diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..7975adabf --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,87 @@ +name: Django-app workflow + +on: [push] + +jobs: + tests: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.7 + + - name: Install dependencies + run: | + # обновление pip + python -m pip install --upgrade pip + # установка flake8 и его плагинов + pip install flake8 pep8-naming flake8-broken-line flake8-return flake8-isort + # установка зависимостей + pip install -r requirements.txt + + - name: Test with flake8 and django tests + run: | + # запуск проверки проекта по flake8 + python -m flake8 + # перейти в папку, содержащую manage.py — + #<корневая_папка_infra_actions>/<папка_проекта>/manage.py + cd infra_project/ + # запустить написанные разработчиком тесты + python manage.py test + + + build_and_push_to_docker_hub: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + needs: tests + steps: + - name: Check out the repo + # Проверка доступности репозитория Docker Hub для workflow + uses: actions/checkout@v2 + - name: Set up Docker Buildx + # Вызов сборщика контейнеров docker + uses: docker/setup-buildx-action@v1 + - name: Login to Docker + # Запуск скрипта авторизации на Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Push to Docker Hub + # Пуш образа в Docker Hub + uses: docker/build-push-action@v2 + with: + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/infra-actions:latest + + deploy: + runs-on: ubuntu-latest + needs: build_and_push_to_docker_hub + steps: + - name: executing remote ssh commands to deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USER }} + key: ${{ secrets.SSH_KEY }} + passphrase: ${{ secrets.PASSPHRASE }} # Если ваш ssh-ключ защищён фразой-паролем + script: | + # Выполняет pull образа с DockerHub + sudo docker pull aisolomatin/infra-actions:latest + #остановка всех контейнеров + sudo docker stop $(sudo docker ps -a -q) + sudo docker run --rm -d -p 5000:5000 aisolomatin/infra-actions:latest + + send_message: + runs-on: ubuntu-latest + needs: deploy + steps: + - name: send message + uses: appleboy/telegram-action@master + with: + to: ${{ secrets.TELEGRAM_TO }} + token: ${{ secrets.TELEGRAM_TOKEN }} + message: ${{ github.workflow }} успешно выполнен! diff --git a/infra_project/infra_app/tests.py b/infra_project/infra_app/tests.py index 77c89862c..6e4c52c76 100644 --- a/infra_project/infra_app/tests.py +++ b/infra_project/infra_app/tests.py @@ -1,4 +1,5 @@ from http import HTTPStatus + from django.test import Client, TestCase @@ -11,7 +12,7 @@ def test_about_url_exists_at_desired_location(self): response = self.guest_client.get('/') self.assertEqual(response.status_code, HTTPStatus.OK) - response = self.guest_client.get('/second_page/') + response = self.guest_client.get('/second/') self.assertEqual(response.status_code, HTTPStatus.OK) def test_page_shows_correct_content(self): @@ -19,5 +20,5 @@ def test_page_shows_correct_content(self): response = self.guest_client.get('/') self.assertContains(response, 'У меня получилось!') - response = self.guest_client.get('/second_page/') - self.assertContains(response, 'А это вторая страница!') + response = self.guest_client.get('/second/') + self.assertContains(response, 'А это вторая страница') diff --git a/infra_project/infra_project/apps.py b/infra_project/infra_project/apps.py new file mode 100644 index 000000000..4f5b31776 --- /dev/null +++ b/infra_project/infra_project/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class InfraProjectConfig(AppConfig): + name = 'infra_project' diff --git a/infra_project/infra_project/settings.py b/infra_project/infra_project/settings.py index 012b314ae..09a32ae72 100644 --- a/infra_project/infra_project/settings.py +++ b/infra_project/infra_project/settings.py @@ -25,13 +25,19 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + 'localhost', + '127.0.0.1', + '[::1]', + 'testserver', +] # Application definition INSTALLED_APPS = [ 'infra_app.apps.InfraAppConfig', + 'infra_project.apps.InfraProjectConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/infra_project/infra_project/urls.py b/infra_project/infra_project/urls.py index 10c002bae..c6a29369b 100644 --- a/infra_project/infra_project/urls.py +++ b/infra_project/infra_project/urls.py @@ -14,7 +14,7 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path, include +from django.urls import include, path urlpatterns = [ path('', include('infra_app.urls', namespace='infra_app')), diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 000000000..7d7e1979a --- /dev/null +++ b/setup.cfg @@ -0,0 +1,12 @@ +[flake8] +ignore = + W503, + F811 +exclude = + tests/, + */migrations/, + venv/, + env/ +per-file-ignores = + */settings.py:E501 +max-complexity = 10