Skip to content

klimuntowskirafal/ci-cd-tutorial-sample-app-public

Repository files navigation

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:

$ sudo apt-get install libpq-dev gcc

Installation

With virtualenv:

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

With conda or miniconda:

$ conda env create -n ci-cd-tutorial-sample-app python=3.8
$ source activate ci-cd-tutorial-sample-app
$ 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:

$ flask db upgrade
$ python seed.py

Running tests

Run:

$ python -m unittest discover

Running the application

Running locally

Run the application using the built-in Flask server:

$ flask run

Running on a production server

Run the application using gunicorn:

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

To set the listening address and port, run:

$ gunicorn app:app -b 0.0.0.0:8000

Running on Docker

Run:

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

Deploying to Heroku

Run:

$ heroku create
$ git push heroku master
$ heroku run flask db upgrade
$ heroku run python seed.py
$ heroku open

or use the automated deploy feature:

Deploy

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

Info:

From Jenkins running on ec2, this repo provides pipeline to manage dev, staging, production environments

Release notes:

  • new feature in new branch -> staging -> main (production)

Run app localy

docker compose up -d
  1. This will start flask and jenkins ccontainers.
  2. From jenkins-cicd container try to run docker ps. If permission denied chown to 1000:1000 to the file /var/run/docker.sock on the container executing as a root user.
  3. Go to localhost:8000 and see if you receive {"status":"ok"}
  4. Go '''localhost:8080''' and set up jenkins with multibranch pipeline - https://www.youtube.com/watch?v=aDmeeVDrp0o

Preparation for deployment

Set up Jenkins on EC2

Add github webhook

https://hevodata.com/learn/jenkins-github-webhook/#steps

Authenticate to AWS in Jenkins

use of CloudBees AWS Credentials Plugin - https://www.youtube.com/watch?v=iiF2iQV-3eM

Pepare AWS ECR

  1. login to ECR aws ecr get-login-password --region REGIONHERE!!!! | docker login --username AWS --password-stdin AWS_ACCOUNTID_HERE!!!!.dkr.ecr.REGIONHERE!!!.amazonaws.com
  2. create registries for storing previously created images and push images to aws ecr

Execute Jenkins pipeline manualy or by pushing branch to repo and create or update ecs stack through cloudformation

see Jenkinsfile for relevant code

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages