Jenkins CI/CD pipeline with jenkinsfile in kubernetes
Jenkins is one of the best and popular CI/CD tools present which are used by multiple companies for their workflows.
In this blog we will see how we can develop a CI/CD pipeline to deploy our flask application in kubernetes cluster using jenkins and jenkinsfile.
We will develop our pipeline steps using jenkinsfile which allows us to define the tasks in each step we wish to take.
We will be using minikube as our cluster but this can be replicated in other types of clusters as well.
To configure the pipeline with your jenkinsfile in github create a new item of multi branch pipeline type.
Under the pipeline section , in definition select multi branch pipeline script from SCM , under SCM git and add your github repository url and if it is private add your credentials. Under script path select jenkinsfile.
For jenkinsfile we use the pipeline syntax we define a pipeline first and define the environment variables.
In this we start the pipeline and set the agent as any which means if we are configuring jenkins worker nodes , any of them can take up that pipeline. The environment variables define the docker hub registry and registry credentials which have been added in jenkins.
The COMMIT variable stores the github commit and Full version variable is the concatenation of the branch , build number and commit to form the tag for docker image.
Head over to manage jenkins and global credentials and add your dockerhub credentials and the id of the variable is used in jenkinsfile.
Next we define the stages in our pipeline , we start off with two simple pipeline steps which just echos on the screen .
All the stages are written under stages , the first stage build executes the echo commands only when the branch is main and the second stage testing executes the echo commands always.
Executing the pipeline yields the following result.
Let us start by defining rest of the other steps , to execute the complete pipeline there are four steps we have to work on
- Cloning the repository
- Building the docker image
- Publishing the image to a registry
- Deploying the applications to kubernetes
To clone the repository we define the step in our jenkinsfile
Here we define the branch we want , the credentials Id which is a jenkins secret containing our github credentials and the repository url.
The step when executed will clone our repository in the jenkins workspace .
The next step is to build the docker image
This step builds the docker image , the registry environment variable was defined above and the FULL_VERSION was defined above.
Finally the image is published to a registry in this case we will be using dockerhub.
This step will push the image to docker hub . The credentials Id — dockerhub is a jenkins secret containing the dockerhub credentials. The with DockerRegistry function facilitates publishing docker images to registries.
In the final deploy stage we deploy using helm where we specify image tag
For checking resources of this blog CLICK HERE