Nowadays even for small web applications or microservices, we need an easier and faster way to deploy applications that is reliable and safe. These applications may be simple but they undergo rapid changes from the business requirement, to handle these changes we definitely need a CI/CD pipeline for deployment.
Jenkins is one such tool that can make your life easier when you want to build, test and deploy on the fly. This article will give you the complete or at least most of the things that you need for creating a CI/CD pipeline to deploy Flask Application using Jenkins.
There is the list of contents that I will be covering to get this up and working.
We will be performing the below-listed operation in this article:
- Building the Flask App (local build)
- Creating the JenkinsFile
- Spinning up Jenkins on Docker
- Integrating GitHub repo to Jenkins
- Testing out Jenkins Pipeline Trigger
1. Building the Flask App (local build)
First, we will try to build our Flask application locally before we push it to GitHub.
git clone https://github.com/Santhoshkumard11/Flask-Calculator-Beginner-Docker.git
Run the Flask app as below (after getting into the Flask-Calculator-Beginner-Docker)
the output of the above code
2. Creating the JenkinsFile:
We will be creating a three-stage pipeline, but you can have as many as your application requires.
- Build Stage – This is where we download the code from your code repository, but since we have already configured GitHub as the branch source Jenkins will do the work of downloading the code or at least in our case. It’s different if you have a React or Angular application.
- Test Stage – You run the unit tests for the Flask App and output the results using xmlrunner which will be picked up by Jenkins
- Deploy Stage – run the Flask Application on nohup and append the output to log.txt but you will use Gunicorn or WSGI in actual deployment.
We also have a post-stage which will do the clean-up or email notification of the pipeline’s success or failure.
'echo "building the repo"'
"deploying the application"
"sudo nohup python3 app.py > log.txt 2>&1 &"
'The pipeline completed'
"Flask Application Up and running!!"
'Build stage failed'
3. Spinning up Jenkins on Docker:
Pull the Docker Image from Docker Hub
docker pull santhoshkdhana/jenkins-python3.9
Now it’s time to spin up the container with the below command,
docker run --name myjenkins -p 8080:8080 -p 50000:50000 -p 5000:5000 -v /your/home:/var/jenkins_home -v /your/home/sock:/var/run/docker.sock jenkins-python3.9
Verify if the container is running using
docker container list -a
checking on the container
Once you have successfully seen the container is up and running, install the required packages
docker exec myjenkins pip3 install flask flask_restful xmlrunner
Type localhost:8080 in your favorite browser. You should see Jenkins asking for the password or use the below command to get the password. Use the initial password from the command line to create an admin user.
docker exec myjenkins cat /var/jenkins_home/secrets/initialAdminPassword
Note: make sure your docker daemon is running else use service docker start to start the daemon (Ubuntu/Debian)
4. Integrating GitHub repo to Jenkins:
- Start by creating a new item, give it the name Flask Jenkins Build (it’s your choice)
- Select the multibranch pipeline style and click ok
- Select GitHub from Branch Sources
- Paste https://github.com/Santhoshkumard11/Flask-Calculator-Beginner-Docker and click on validate (you need to add credentials if it’s not a publicly accessible repo)
- Once it says connected, scroll down to the bottom and click apply and save
5. Testing out Jenkins Pipeline Trigger:
Now you have integrated your GitHub repo to Jenkins, from now on whenever you commit to the qa branch the pipeline would start running.
Go to Jenkins home and click on the Pipeline name you created and then the branch name.
To run this manually, click on Build Now from the sidebar on your left.
After the test stage, you will have to manually click deploy to get the deployment stage running.
Click on Open Blue Ocean to see a new UI with the build stages.
Share your thoughts in the comments
Please Login to comment...