Table of contents
In this article, you are going to learn the Flask python application to create a docker image and push that to a docker registry using GithubActions. The prerequisites for this tutorial are that you have a basic understanding of Dockerand Github.
GitHub actions:
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository or deploy merged pull requests to production. It gives a privilege to the repository owner to write individual actions and then combine them to create a custom workflow of their choice for their project.
Github actions allow to you define your pipeline in declarative actions.
If you are a spring boot java developer then click here for the spring boot Github actions Heroku article.
First Step:
Create a new repository on Github it should look like the below.
Once the repository is created then clone it into local using the git clone command. They open that folder in vscode editor or your favorite editor.
Create Dockerfile
FROM python:3.6
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Then let us create app.py
and paste the below code snippet, which will return a plain string response.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Flask github action hello world'
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8070, debug=False)
requirements.txt
file to install the required python packages for our flask application.
flask
Second Step:
Run in local.
Now code is ready, let us run it locally using the below command.
docker build -t flask-docker-image:latest .
Now, that your image is ready, Let us create a container using the below command.
docker run -d -p 8070:8070 push-docker-image:latest
Then hit localhost:8070 Everything looks good in your local but everything work in local that may work in production π.
Third Step:
Create a directory in your current project .github/workflows
and inside this workflows folder create the yaml file of flask-docker-image.yaml
.
name: Python Flask GitHub action for docker registry
on: [push] # event: When pushing to any branch then run this action
# Env variable
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
REPO_NAME: ${{secrets.REPO_NAME}}
jobs:
flask-image-to-docker-hub: # job name
runs-on: ubuntu-latest # runner name : (ubuntu latest version)
steps:
- uses: actions/checkout@v2 # first action : checkout source code
- name: docker login
run: | # log into docker hub account
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Get current date # get the date of the build
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d--%M-%S')"
- name: Build the Docker image # push The image to the docker hub
run: docker build . --file Dockerfile --tag $DOCKER_USER/$REPO_NAME:${{ steps.date.outputs.date }}
- name: Docker Push
run: docker push $DOCKER_USER/$REPO_NAME:${{ steps.date.outputs.date }}
In this file, there are placeholders like $REPO_NAME, $DOCKER_USER, and $DOCKER_PASSWORD these values you have to provide. The name of a docker image and tag are set dynamically like a name is username/repo name and tag is the current date, you can set this based on your organization's requirement.
Goto github and creates a secret to providing placeholder values.
go to settings β> secrets -> Actions
and create three secrets such as DOCKER_USER, DOCKER_PASSWORD, and REPO_NAME.
Now push your code to your github repo. Goto Actions tab on github and verify its success or fail, If it fails then check logs and try to resolve or add a comment I will try to help you. If it's successful then go to the docker hub account and check the docker image.
docker push maheshwarligade/flask-docker-githubaction:2022-05-29--54-09
You can pull this image and test it. Github Code:- github.com/maheshwarLigade/flask-docker-git...
Conclusion:
This article helps you to understand and create a simple CI pipeline using Github actions for the flask python application. Source code is also available to pull and try.
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
==========================**=========================
If this article adds any value for you then please clap and comment.
Letβs connect on Stackoverflow, LinkedIn, & Twitter.