Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build a CI/CD pipeline for Flask apps using GitHub Actions

Tags: heroku code flask

Posted on Oct 12 • Originally published at honeybadger.io This article was originally written by Muhammed Ali on the Honeybadger Developer Blog.The practice of continuous integration (CI) and continuous deployment (CD) involves creating an automated flow that governs how changes will be integrated into the main project and how these new changes will be deployed.GitHub Actions is a feature on GitHub that enables you to implement CI/CD in your project repository. It is usually a smart idea to use GitHub Actions because it makes it simple to automate tasks, such as testing and project deployment, directly in your GitHub repository.In this article, you will learn how to build a Flask API, write some unit tests for the API endpoints, and then develop the pipeline that will test the Flask API and deploy it to Heroku if all tests pass.You can find the code for the tutorial on GitHub.To follow along, you need basic knowledge of the following:In this section, you will create a to-do list API, which will be the app that will be deployed to Heroku. We will create a basic CRUD functionality for the to-do list.First, let’s start with the initial imports. Create a folder that will hold all the files for your application and then, in that folder, create a file named main.py and paste the following code. Here we are just importing Flask and SQLAlchemy (required for the database connection to work), as well as initializing the Flask app and database.Next, we need to create models for the database. This will indicate the fields in the database. To do this, paste the code below directly below the code section above.Next, we can create a function that will read all the items in the to-do list. JSON is the generalized format in which data can be accepted from the client, so we will need to also have a function that converts all the items retrieved from the database into JSON. You can do this by pasting the following code.Now we will develop the todo_create() function, which will create a new to-do item. The function will convert the JSON data retrieved from the client into a format Python can understand and then save the data in the database. You can do this by pasting the following code.Now to the update functionality. You will first extract each to-do based on the inputted id, and from there, you will be able to update the to-do. Paste the following code to implement this functionality.Finally, we will create the delete function. We will get the to-do based on the inputted id and then delete the to-do item from there. We will also close off with the line that will run the Flask server.Now, run the following command to start up your Flask app server:Open another terminal window and run the following command to create a new to-do item. Replace in the command below with the to-do item you want to add to the list.When you open the API on your browser with http://127.0.0.1:5000, you should see the to-do list in JSON format.Since we are done with building the API, we can write unit tests for the endpoints, which is what we will be doing in this section. To start, create a new file named test_main.py and then paste the following code. The following code creates some initial data in a test database that will be used to test the endpoints of the API.To test the list API endpoint, we will assert that the items we get from that particular endpoint is the same as what was saved in the test database and that the status code coming as the output is 200. You can do this by pasting the following code:For the creation and updating endpoints, we will be asserting that the status code in the main app is the same when running the tests. You can do this by pasting following the code:You can run the test by running the following command:First, we will create a Web Server Gateway Interface WSGI file, which will be used by Heroku to run our application on their server. You can do this by creating a file named [wsgi.py](http://wsgi.py) and pasting the following code:Now, we can create the Procfile for Heroku. The Procfile contains the command that will be run once the application is deployed on Heroku. You can do this by creating a new file named Procfile and pasting the following command. We will be using Gunicorn to run the application instead of the default Flask server.Now state the list of the dependencies required for this application by creating a new file named requirements.txt and pasting the following text. This will be used in your pipeline and Heroku.Before continuing further, you must create a Heroku account if you don’t have one already, and then install Heroku CLI.Now you can log into your Heroku CLI by running the following command:Next, create a Heroku app by running the following command. Replace todo-app-101 in the command below with any name that suits your app.Once the app is created, you will be given a URL that you can use to access your application when it is deployed. You can also see it on Heroku dashboard.Now we need to get the API key for our Heroku account. This is essential so that GitHub Action will know to which account to deploy. You can get it by going to your account settings. While on your account settings page, scroll down, and you will see “API Key”; make note of it, as it will be needed later in this article.Create a folder named .github in the root of your project, and inside it, create workflows/main.yml; the path should be .github/workflows/main.yml to get GitHub Actions working on your project.GitHub Actions have three levels:Now we can go ahead and develop the jobs and steps. This can be created by pasting the following configurations in the file you just created. The configuration below will install the dependencies and then run tests. If all the tests pass, it goes ahead to the next steps, which handle the deployment of the application.You can activate this pipeline by creating a repository on GitHub and pushing your code to that repository, and GitHub will handle the rest of the work. After your code is pushed go to the repository on GitHub, click on the “Actions” tab. Then, click on the commit you just made, and you will see that the workflow ran successfully.This means that your application has now been deployed to Heroku. You can access it by opening the URL you were given earlier in your browser. You can go ahead and test out the other endpoints.In this post, you’ve learned how to create a to-do list with Flask API. You also learned how to create unit tests for the API endpoints and create a pipeline that will test the Flask API and deploy it to Heroku if all tests pass.Furthermore, you can build up on this knowledge by building a pipeline that can deploy to a bare bones Linux server or maybe a platform similar to DigitalOcean.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Pavan Belagatti - Sep 22 Vidit Goel - Aug 24 Saurabh Rai - Sep 18 Morganna - Sep 20 Zero-instrumentation, 360 degree coverage of errors, outages and service degradation. Deploy with confidence and be your team's devops hero. Once suspended, honeybadger will not be able to comment or publish posts until their suspension is removed. Once unsuspended, honeybadger will be able to comment and publish posts again. Once unpublished, all posts by honeybadger will become hidden and only accessible to themselves. If honeybadger is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Honeybadger Staff. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag honeybadger: honeybadger consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging honeybadger will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build a CI/CD pipeline for Flask apps using GitHub Actions

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×