Set Up CI/CD with Github Actions for Flask

Hello, welcome back to the 9th post of my Flask series, this post would be a continuation of my previous post, please click on any link below to view any post you missed

OUTLINE OF FLASK SERIES

 

At the end of this post, you will learn the following.

  • Automate our Flask Application Testing With GitHub Actions

Before we begin, let’s briefly discuss what CI/CD means.

CONTINUOUS DELIVERY

Is the practice of automating the integration of code changes from multiple contributors or a contributor to a single software project.

It involves the test automation of feature branches before they are merged to the main Git branch in a project. This ensures that a codebase does not get updated with changes that could break something.

CONTINUOUS INTEGRATION

Builds upon CI by automating releases of these branches.

Continuous Delivery is the ability to get changes such as new features, configuration changes, bug fixes, and experiments into production  and the end-users in a safe and quick way

setting up github action workflow template

Create a new folder .github in your directory

cd .github && mkdir workflows
cd  workflows && touch test.yml

GitHub provides a Python workflow template that works for most Python projects. Get the template from here 👉 Python workflow template. Paste this into your test.yml file

name: Python package

on:
  push:
    branches: [ $default-branch ]
  pull_request:
    branches: [ $default-branch ]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.5, 3.6, 3.7, 3.8]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        pytest

We are going to modify the template to match our need

name: Python application

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt; 
    - name: Test with unittest
      run: |
        python -m unittest

Please update your config.py, add the environment variables for the Testing environment.

class TestingConfig(Config):
    TESTING = True
    SECRET_KEY="GGggjjjfk887856$%kk"
    # Disable CSRF protection in the testing configuration
    WTF_CSRF_ENABLED = False
    SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join(basedir, "test.sqlite")

 

Now commit and push your files to Github

git add .
git commit -m "set up github actions"
git push

Let’s visit our Github page, click the Action tab

Click set up github actions

 

You can see our build was successful.

 

If you’re feeling like taking it a step further, you can learn more information here.

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *