Skip to content

Commit

Permalink
Merge pull request #4 from CodeForAfrica/ch-setup-ci-cd
Browse files Browse the repository at this point in the history
Chore setup continuous integration/continuous delivery
  • Loading branch information
esirK committed Oct 22, 2021
2 parents ed047f1 + dd4b798 commit e4c327d
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Required
TWOOPSTRACKER_DATABASE_URL=
TWOOPSTRACKER_SECRET_KEY=

# End of Required

# Optional
TWOOPSTRACKER_ALLOWED_HOSTS=
TWOOPSTRACKER_DEBUG=False
35 changes: 35 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Bug report
about: Create a report to help us improve

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Feature request
about: Suggest an idea for this project

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
25 changes: 25 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Chore (non-breaking change which does not add visible functionality but improves code quality)
- [ ] This change requires a documentation update

## Screenshots


## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
59 changes: 59 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Dokku CD
on:
push:
branches: [main]
env:
DOKKU_REMOTE_BRANCH: "master"
DOKKU_REMOTE_URL: "ssh://dokku@dokku-1.dev.codeforafrica.org/twoopstracker"
GIT_PUSH_FLAGS: "--force"
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: codeforafrica/twoopstracker:latest
target: python-app-ci
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new

- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Push to Dokku
uses: dokku/github-action@v1.0.2
with:
branch: ${{ env.DOKKU_REMOTE_BRANCH }}
git_push_flags: ${{ env.GIT_PUSH_FLAGS }}
git_remote_url: ${{ env.DOKKU_REMOTE_URL }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
64 changes: 64 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CI

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
docker:
name: Build, Lint & Test
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build
uses: docker/build-push-action@v2
with:
context: .
push: false
tags: codeforafrica/twoopstracker:latest
target: python-app-ci
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Run app
run: |
echo "TWOOPSTRACKER_SECRET_KEY=${{ secrets.TWOOPSTRACKER_SECRET_KEY }}" >> .env
echo "TWOOPSTRACKER_SENTRY_DSN=${{ secrets.TWOOPSTRACKER_SENTRY_DSN }}" >> .env
make runci
- name: Run Lint
run: make lint

- name: Test app
run: make test

- name: Stop app
run: make stop
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
COMPOSE = docker-compose
COMPOSE_BUILD_ENV = COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1
COMPOSE_BUILD_FLAGS = --progress=plain

build:
$(COMPOSE_BUILD_ENV) $(COMPOSE) build $(COMPOSE_BUILD_FLAGS)

run:
$(COMPOSE_BUILD_ENV) $(COMPOSE) up -d

runci:
$(COMPOSE_BUILD_ENV) $(COMPOSE) up -d app

enter:
$(COMPOSE) exec app bash

createsuperuser:
$(COMPOSE) exec app python manage.py createsuperuser

lint:
$(COMPOSE) exec -T app pre-commit run --all-files

test:
$(COMPOSE) exec -T app python manage.py test

stop:
$(COMPOSE) down
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
timeout: 5s
retries: 5
app:
image: codeforafrica/twoops_tracker:0.0.1
image: codeforafrica/twoopstracker:latest
restart: unless-stopped
build:
context: .
Expand All @@ -30,6 +30,11 @@ services:
env_file:
- .env
environment:
- TWOOPSTRACKER_ALLOWED_HOSTS=${TWOOPSTRACKER_ALLOWED_HOSTS:-*}
- TWOOPSTRACKER_DATABASE_URL=${TWOOPSTRACKER_DATABASE_URL:-postgresql://twoops_tracker:twoops_tracker@db:5432/twoops_tracker}
- TWOOPSTRACKER_DEBUG=${TWOOPSTRACKER_DEBUG:-True}
# psql connection parameters (used in wait-for-postgrsql.sh)
# see: https://www.postgresql.org/docs/9.6/libpq-envars.html
- PGHOST=db
- PGUSER=twoops_tracker
- PGPASSWORD=twoops_tracker
Expand Down
1 change: 1 addition & 0 deletions requirements-all.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Django==3.2.8
environs[django]==9.3.4
greenlet==1.1.2
gunicorn[gevent, setproctitle]==20.1.0
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ black==21.9b0
flake8==4.0.1
isort==5.9.3
mypy==0.910
pre-commit==2.15.0
psycopg2-binary==2.9.1
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-r requirements-all.txt
psycopg2==2.9.1
21 changes: 10 additions & 11 deletions twoopstracker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"""

from pathlib import Path
from typing import List

from environs import Env

env = Env()
env.read_env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -20,14 +26,12 @@
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-=9wtf8zn6a7y+%305l899xvu^q7vcinp)z%a#msn)6d$zik!ch"
SECRET_KEY = env.str("TWOOPSTRACKER_SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = env.bool("TWOOPSTRACKER_DEBUG", False)

ALLOWED_HOSTS = [
"*",
]
ALLOWED_HOSTS: List[str] = env.str("TWOOPSTRACKER_ALLOWED_HOSTS", "").split(",")


# Application definition
Expand Down Expand Up @@ -78,12 +82,7 @@
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
DATABASES = {"default": env.dj_db_url("TWOOPSTRACKER_DATABASE_URL")}

AUTH_USER_MODEL = "authentication.User"

Expand Down

0 comments on commit e4c327d

Please sign in to comment.