[Building a CI environment in 2 hours] Procedure for building a Python Web server with CircleCI and passing an HTTP communication test

The feature of CircleCI is that you can use the CI environment for free if you have one environment. When the environment construction is completed, the CI container will be launched for each commit on GitHub and the defined automated test will be executed on the container. In this post, I'm testing a web server built with Python 3.5 and Flask running on CircleCI, using requests to access the web server on the container and returning HTTP Status code 200.

Operation flow of CI environment built in 2 hours

Test a web server built with Python3.5 + Flask

スクリーンショット 2016-02-26 21.06.22.png

This article is a simple test to start a web server using Flask, a Python web framework, on CircleCI, access an HTTP endpoint, and check the HTTP Status.

CircleCI's way of thinking about jenkins craftsmanship

The jenkins craftsmanship problem is an uncommon coined word that is said within my narrow observation range. The problem is that if you put jenkins as a CI environment in PJ, jenkins craftsmen will generally occur and magic modification will proceed and it will be convenient, but when the jenkins environment is damaged even though you can only deploy with jenkins, the craftsmen I can only fix it. Is often the problem. I think that tragedy often occurs at the stage when jenkins becomes an indispensable infrastructure and it is required not to fall to the same level as the production environment. A respected senior said he didn't want to get involved in the jenkins cluster if possible.

CircleCI does not cause this problem because it ** aggregates all information about the CI environment in circle.yml **. Also, the problem of over-packing the SDK in one Jenkins does not occur because of the mechanism to generate a separate container for each test.

CircleCI construction procedure manual

1. Have a GitHub account
2. Register as a user on the CircleCI Sign Up page (https://circleci.com/signup/)
3. Select and register a GitHub repository
4. Hello World on CircleCI

Create circle.yml in the top directory of the repository, write the following and commit. The moment you commit, it will be hooked and the test will start automatically on CircleCI.

circle.yml


test:
  override:
    - pwd ~
    - ifconfig
    - echo HelloWorld
    - exit 0

■ Execution result スクリーンショット 2016-02-26 19.23.07.png

5. Build a Python 3.5 environment on CircleCI

See the official documentation here (https://circleci.com/docs/environment#python) for the versions of Python that CircleCI supports. If you commit, the test will run automatically.

circle.yml


machine:
  python:
    version: 3.5.0

test:
  override:
    - pwd ~
    - ifconfig
    - echo HelloWorld
    - exit 0
6. Build an environment with pip

pip is a Gem, package management system in Ruby. The standard method of CicleCI is to list the package name and version in requirements.txt in the top directory of the repository. For more information, see the official documentation Python section (https://circleci.com/docs/language-python).

circle.yml


machine:
  python:
    version: 3.5.0

dependencies:
  pre:
    - pip install -r ./requirements.txt

test:
  override:
    - pwd ~
    - ifconfig
    - echo HelloWorld
    - exit 0

requirements.txt


Flask==0.10.1
Flask-Script==2.0.5
pytz==2015.7
redis==2.10.5
requests==2.9.1
six==1.10.0
SQLAlchemy==1.0.11
gunicorn==19.4.5
pytest==2.8.5
7. Run server in the background

** The reason for running it in the background is that if you don't run it in parallel, the process will stop waiting for the runserver to finish and the test will not end. ** **

In order to run the python script on CircleCI, it is necessary to grant permissions with chmod and set PYTHONPATH. Flask but manage.py is the effect of customizing with Flask-Script. Flask-Script is useful for experienced Django users. It is convenient to write a curl command in test: override: to check if the server is running.

circle.yml


machine:
  python:
    version: 3.5.0

dependencies:
  pre:
    - chmod -R 777 ./example
    - pip install -r ./requirements.txt
    - export PYTHONPATH="/home/ubuntu/flask_template/example/application"
    - python ./application/manage.py runserver:
          background: true
    - sleep 5

test:
  override:
    - pwd ~
    - ifconfig
    - echo HelloWorld
    - curl http://127.0.0.1/example
    - exit 0
8. Pass the test

Write a test with py.test. It's as simple as checking the HttpStatus. This time it is a sample, so the contents are the same

■ Code for py.test

test_example.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import requests


def test_example():
    host = "127.0.0.1:5000"
    url = 'http://{}/example/'.format(host)
    print(url)
    response = requests.get(url)
    print(response.text)
    assert response.status_code == 200
    assert "Error" not in response.text

circle.yml


machine:
  python:
    version: 3.5.0

dependencies:
  pre:
    - chmod -R 777 ./example
    - pip install -r ./requirements.txt
    - export PYTHONPATH="/home/ubuntu/flask_template/example/application"
    - python ./application/manage.py runserver:
          background: true
    - sleep 5

test:
  override:
    - pwd ~
    - ifconfig
    - echo HelloWorld
    - curl http://127.0.0.1/example
    - py.test ./example/tests/test_example.py
    - exit 0

If you commit and the test passes, the work is completed

9. Notify HipChat of the results

You can set the notification destination in Project Settings> Notifications page. Is it easy to understand if you read notify official document?

■ Notified スクリーンショット 2016-02-26 20.47.16.png

Range not covered in this article

I haven't tried mysql, so I'd like to try it in the future. Official Document Database Settings

Impressions of CircleCI

At first, I didn't understand the reasoning, and the learning cost was heavy, so the first step was difficult. I thought it was very good because the official documents were substantial. For the limited use of testing Python web frameworks, I felt that if someone worked hard to write a configuration file, it would be possible to mass-produce it with reference to it.

Recommended Posts

[Building a CI environment in 2 hours] Procedure for building a Python Web server with CircleCI and passing an HTTP communication test
Build and test a CI environment for multiple versions of Python
Build a lightweight server in Python and listen for Scratch 2 HTTP extensions
Building an Anaconda environment for Python with pyenv
Launch a web server with Python and Flask
Put Docker in Windows Home and run a simple web server with Python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Building a Docker working environment for R and Python
Building an environment for natural language processing with Python
Procedure for building a CDK environment on Windows (Python)
Installation procedure for Python and Ansible with a specific version
Library for specifying a name server and dig with python
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
A modern environment building procedure for those who want to get started with Python right away
Building a Docker working environment for R and Python 2: Japanese support
How about Anaconda for building a machine learning environment in Python?
Building a Windows 7 environment for getting started with machine learning with Python
Write an HTTP / 2 server in Python
Building a virtual environment with Python 3
Procedure for building a kube environment on amazon linux2 (aws) ~ (with bonus)
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
[Pyenv] Building a python environment with ubuntu 16.04
[Python] Building an environment with Anaconda [Mac]
Building a Python3 environment with Amazon Linux2
Building a Python 3.6 environment with Windows + PowerShell
Building a Python development environment for AI development
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv
A complete guidebook to using pyenv, pip and python in an offline environment
Searching for an efficient way to write a Dockerfile in Python with poetry
Create a Vim + Python test environment in 1 minute
Building an environment for executing Python scripts (for mac)
Building a Python environment with WLS2 + Anaconda + PyCharm
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Start a simple Python web server with Docker
Set up a test SMTP server in Python.
Various commands for building an environment with Apache
[Python] Web development preparation (building a virtual environment)
Think about building a Python 3 environment in a Mac environment
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Procedure for creating a Python quarantine environment (venv environment)
Try building an environment for MayaPython with VisualStudioCode
Procedure when building Ubuntu as a virtual environment with Vagrant + Virtualbox for LPIC Level 1.
Automate background removal for the latest portraits in a directory with Python and API
[Python] Building an environment for competitive programming with Atom (input () can be used!) [Mac]
Create an exe file that works in a Windows environment without Python with PyInstaller
Write about building a Python environment for writing Qiita Qiita
Build an interactive environment for machine learning in Python
Recommendation of building a portable Python environment with conda
Build a python environment for each directory with pyenv-virtualenv
Build a python virtual environment with virtualenv and virtualenvwrapper
Create a web server in Go language (net / http) (1)
conda memorandum: Building a Python environment with supercomputer ITO
Create a fake Minecraft server in Python with Quarry
Memo for building a machine learning environment using Python
HTTP server and HTTP client using Socket (+ web browser) --Python3
Create an environment for test automation with AirtestIDE (Tips)
Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh