Ever built a Flask app and thought, "This is amazing!", but then cried a little inside every time you manually deployed it?
Yeah, me too.
So I decided to automate the entire process, throw in a Jenkins pipeline, Docker, and—because I'm needy—get Slack to tell me when stuff breaks (or doesn’t!).
What are we doing here?
We’re setting up a CI/CD pipeline for a simple Flask application using:
🐳 Docker
🤖 Jenkins
🧪 Bash Scripts
🛠 GitLab
💬 Slack
Project Structure:
.
├── Jenkinsfile
├── flask/
│ ├── build/
│ │ ├── app/
│ │ │ ├── app.py
│ │ │ └── requirements.txt
│ │ ├── build.sh
│ │ ├── Dockerfile
│ │ └── docker-compose.yml
│ ├── test/
│ │ └── test.sh
│ ├── push/
│ │ └── push.sh
│ └── deploy/
│ ├── deploy.sh
│ └── publish
Jenkins Pipeline Stages:
Build
Builds a Docker image of the Flask app.Test
Runs tests (if you wrote any).Push
Pushes image to Docker Hub like a proud parent.Deploy
Remotely deploys your app using SSH.Slack Notify
Sends a message:
"Pipeline succeeded"
"Pipeline failed"
Setting up Slack Notifications (because emails are boring):
- Create a Slack app.
- Enable Incoming Webhooks
- Add it to a channel
- Copy the webhook URL
- Add it as a Secret Text Credential in Jenkins with ID: SlackWebhook
Jenkinsfile Post Block:
post {
success {
sh '''
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Pipeline succeeded for $BUILD_TAG"}' $SLACK_WEBHOOK
'''
}
failure {
sh '''
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Pipeline failed for $BUILD_TAG"}' $SLACK_WEBHOOK
'''
}
}
Auto Trigger Pipeline on Git Push:
Automation is key—and this step makes sure Jenkins jumps into action every time you push to GitLab.
Steps I followed:
- Installed the GitLab plugin in Jenkins
- Went to Configure on the Jenkins job
- Enabled: Build when a change is pushed to GitLab
- Jenkins gave me this webhook: http://localhost:8080/project/pipeline-docker-python Generated a secret token
- Headed to GitLab → Settings → Webhooks Pasted: the webhook URL (replacing localhost with host.docker.internal if needed) the secret token
- Clicked Save and pushed to the repo.
Now Jenkins automatically reacts to your Git pushes like a loyal robot minion. 🤖
Jenkinsfile:
pipeline {
agent any
environment {
PASS = credentials('Dokcer-registry')
SLACK_WEBHOOK = credentials('SlackWebhook')
}
stages {
stage('Build') {
steps {
sh 'bash jenkins/build/build.sh'
}
}
stage('Test') {
steps {
sh 'bash jenkins/test/test.sh'
}
}
stage('Push') {
steps {
sh 'bash jenkins/push/push.sh'
}
}
stage('Deploy') {
steps {
sh 'bash jenkins/deploy/deploy.sh'
}
}
}
post {
success {
sh '''
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"CI/CD Pipeline succeeded for build: '$BUILD_TAG'"}' $SLACK_WEBHOOK
'''
}
failure {
sh '''
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"CI/CD Pipeline failed for build: '$BUILD_TAG'"}' $SLACK_WEBHOOK
'''
}
}
}
Final Thoughts:
Setting up a CI/CD pipeline might seem complex at first, but once configured, it significantly improves your development workflow. Automation ensures faster, more consistent deployments and immediate feedback on code changes.
Integrating Slack for notifications adds visibility to your pipeline outcomes, making it easier to track success and failures in real-time.
This setup provides a solid foundation for managing Flask deployments with Jenkins, Docker, and GitLab. For teams or solo developers aiming for smoother releases, this structure is a good place to start.
Screenshots:
Top comments (0)