项目作者: edonosotti

项目描述 :
A sample Python app that implements a REST API, with database migrations and CI/CD support.
高级语言: Python
项目地址: git://github.com/edonosotti/ci-cd-tutorial-sample-app.git
创建时间: 2018-11-08T21:16:30Z
项目社区:https://github.com/edonosotti/ci-cd-tutorial-sample-app

开源协议:

下载


Build Status
codebeat badge
Maintainability

CD/CI Tutorial Sample Application

Description

This sample Python REST API application was written for a tutorial on implementing Continuous Integration and Delivery pipelines.

It demonstrates how to:

  • Write a basic REST API using the Flask microframework
  • Basic database operations and migrations using the Flask wrappers around Alembic and SQLAlchemy
  • Write automated unit tests with unittest

Also:

Related article: https://medium.com/rockedscience/docker-ci-cd-pipeline-with-github-actions-6d4cd1731030

Requirements

  • Python 3.8
  • Pip
  • virtualenv, or conda, or miniconda

The psycopg2 package does require libpq-dev and gcc.
To install them (with apt), run:

  1. $ sudo apt-get install libpq-dev gcc

Installation

With virtualenv:

  1. $ python -m venv venv
  2. $ source venv/bin/activate
  3. $ pip install -r requirements.txt

With conda or miniconda:

  1. $ conda env create -n ci-cd-tutorial-sample-app python=3.8
  2. $ source activate ci-cd-tutorial-sample-app
  3. $ pip install -r requirements.txt

Optional: set the DATABASE_URL environment variable to a valid SQLAlchemy connection string. Otherwise, a local SQLite database will be created.

Initalize and seed the database:

  1. $ flask db upgrade
  2. $ python seed.py

Running tests

Run:

  1. $ python -m unittest discover

Running the application

Running locally

Run the application using the built-in Flask server:

  1. $ flask run

Running on a production server

Run the application using gunicorn:

  1. $ pip install -r requirements-server.txt
  2. $ gunicorn app:app

To set the listening address and port, run:

  1. $ gunicorn app:app -b 0.0.0.0:8000

Running on Docker

Run:

  1. $ docker build -t ci-cd-tutorial-sample-app:latest .
  2. $ docker run -d -p 8000:8000 ci-cd-tutorial-sample-app:latest

Deploying to Heroku

Run:

  1. $ heroku create
  2. $ git push heroku master
  3. $ heroku run flask db upgrade
  4. $ heroku run python seed.py
  5. $ heroku open

or use the automated deploy feature:

Deploy

For more information about using Python on Heroku, see these Dev Center articles: