Skip to content

Commit

Permalink
Added CI-CD workflow to lint, test & automatically deploy on remote s…
Browse files Browse the repository at this point in the history
…erver
  • Loading branch information
n8creator committed Jan 10, 2024
1 parent 059752c commit d2250ed
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 12 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Useful links
# - https://docs.github.com/en/actions/learn-github-actions/variables
# - https://github.com/dokku/github-action/issues/8
#

name: CI/CD Pipeline

on:
push:
branches:
- main

jobs:
test:
name: Run linting and tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install dependencies
run: |
pip install poetry
make install
- name: Run linter
run: make lint

- name: Run tests
run: make test

- name: Code Climate Coverage Action
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: make coverage_xml

deploy:
name: Deploy app on Dokku Server
runs-on: ubuntu-latest
needs: test
if: ${{ needs.test.result == 'success' }}
steps:
- name: Clone repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Push to Dokku
uses: dokku/github-action@master
with:
git_push_flags: '--force'
branch: main
git_remote_url: ssh://${{ secrets.DOKKU_USERNAME }}@${{ secrets.DOKKU_SERVER_IP }}:22/~/${{ secrets.DOKKU_APP_NAME }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}

notify:
name: Send Slack Notification
runs-on: ubuntu-latest
needs: [test, deploy]
if: always()
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Slack Notification on Success
if: ${{ needs.test.result == 'success' && needs.deploy.result == 'success' }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_ICON: https://www.google.com/s2/favicons?domain=github.com&sz=128
SLACK_COLOR: "good"
SLACK_USERNAME: "GitHub Actions"
SLACK_TITLE: >
🟢 CI/CD Pipeline Succeeded for "${{ github.repository }}" project
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

- name: Slack Notification on Failure
if: ${{ needs.test.result == 'failure' || needs.deploy.result == 'failure' }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_ICON: https://www.google.com/s2/favicons?domain=github.com&sz=128
SLACK_COLOR: "danger"
SLACK_USERNAME: "GitHub Actions"
SLACK_TITLE: >
❌ CI/CD Workflow Failed for "${{ github.repository }}" project
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
27 changes: 16 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# Install project
DIRS = task_manager/

# Setup
install:
@poetry install

# Lint project
# Lint
isort:
poetry run isort task_manager/
poetry run isort $(DIRS)

black: isort
poetry run black task_manager/
@poetry run black $(DIRS)

format: isort black

lint: isort black
poetry run flake8 --config ./.flake8 task_manager/
lint:
@poetry run flake8 --config ./.flake8 $(DIRS)

# Run tests
test:
poetry run pytest -vv --cov ./task_manager/ --cov-report term-missing:skip-covered
# poetry run pytest -vv $(DIRS) --cov --cov-report term-missing:skip-covered
poetry run pytest -vv $(DIRS) --cov

coverage_xml:
poetry run coverage xml

# Start & deploy project
start:
Expand All @@ -23,10 +31,7 @@ start:
migrate:
poetry run ./manage.py migrate

deploy:
git push dokku main

# System commands for Makefile
MAKEFLAGS += --no-print-directory

.PHONY: requirements install isort black lint test start migrate deploy
.PHONY: install isort black format lint test coverage_xml start migrate
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
### Hexlet tests and linter status:
[![Actions Status](https://github.com/n8creator/python-project-lvl4/workflows/hexlet-check/badge.svg)](https://github.com/n8creator/python-project-lvl4/actions)
[![Actions Status](https://github.com/n8creator/python-project-lvl4/workflows/hexlet-check/badge.svg)](https://github.com/n8creator/python-project-lvl4/actions)
[![Github Actions Status](https://github.com/n8creator/python-project-lvl4/actions/workflows/ci-cd.yml/badge.svg)](https://github.com/n8creator/python-project-lvl4/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/07700d46e12b1094c627/maintainability)](https://codeclimate.com/github/n8creator/python-project-lvl4/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/07700d46e12b1094c627/test_coverage)](https://codeclimate.com/github/n8creator/python-project-lvl4/test_coverage)

0 comments on commit d2250ed

Please sign in to comment.