Setup CI/CD (Github Actions) for Flask to Azure App Service

Containerize Flask and Deploy to Azure App Service

Photo by Richy Great on Unsplash


I will not bring to much how you build web from Flask. I will give some main aspects from Flask and how we containerize them. Your project may have different structure with me but actually it will not really different. Please see my repo here for the last result.

Containerize Flask App

Photo by Angela Lo on Unsplash
  1. Setup binary for install dependencies from pip and download the dependencies
  2. Setup binary for runtime
  3. Copy all your code
  4. Setup entrypoint
  • Sometimes you need to setup the binary or install any binaries before install the dependencies, some dependencies maybe will need development tools installed first.
  • You need to know any binary that needed at runtime, as example needed the MySQL client or PostgreSQL client. It will depend on your apps.
  • After finished download the dependencies, yeah, the last step to copy your code. Why you bring this into last step? So you can use the caching strategy, when you just change the code, sometimes you will not need to run the step from beginning, but it will do the step from cache and build new layer from you updated code.
  • Last, you will need to define the Entrypoint, but someone use CMD, it’s ok, just use what you want.

Setup the CI

For setup the CI, I use the Github Action to do CI (Continuous Integration). Hemm… Anyway, you required to have any unit test and/or integration test for this. It means nothing to do when you not have one of them. If you check my repository, you will know, my unit test not cover all and very bad condition, but little bit better than nothing. Okay, I will guide you, I will not provide full code, since it will depend on your code too! I have 3 stages for CI/CD, 2 CI & 1 CD. 1 CI for test from python directly, 1 CI for test my build process, 1 CD to build Dockerfile into Github Package.

  1. Do linting, I recommend to do test the lint, so any codes will follow your convention
  2. Unit test
  3. Integration test (if you have)
  4. Docker build

Setup the CD

Okay, after you setup the CI correctly, maybe you image how you automate the build process. Anyway, you need to know, for some projects will required you to have multiple environment, in my case, I just have one environment, so it will deploy directly to my main Azure App Service. I will learn more about the multi environment and will share it too.

CD Process

Setup the Azure App Service (Existing)

In this step, assume you already created the Azure App Service with Container services.

Docker container
Container Image
New Deployment Center
Setup webhook
Payload URL
Registry packages
Photo by Federico Respini on Unsplash

Backend Developer — Focus at Microservices and Cloud Computing