Pipeline Script
If we choose a pipeline script, here we can write our Groovy script for the pipeline steps. However, this process is not recommended due to a loss of configuration while the Jenkins server is down or crashed. Therefore, I am choosing the second option, Pipeline script from SCM, then providing a few inputs like below.
Note
We should not specify any credentials directly. Instead, we can add it in the Jenkins server itself by clicking the Credentials option. There are various types to create credentials.
I chose:
- GitHubCredentials - Using a username and password option.
- DockerPassoword - Using a secret text option.
Step 6
Next, let's visit the project structure and its configuration.
Here, you can see the four files that must be placed on this location. If you want, you can change the location, but you need to change the configuration as well. So let's follow the default.
Demo.sln - Solution file of our project.
Dockerfile - Here we write the necessary steps for building a docker image of our application.
JenkinsFile - It consists of pipeline steps.
deployment-service.yaml - This file has a Kubernetes deployment configuration.
Step 7
We discussed more about these two files (DockerFile and deployment-service.yaml) in my previous articles. Also, you can refer to the below links:
So, let's move on with Jenkins file. Please refer
Jenkins stage reference link for more detail. Now let's go with our actual implementation of JenkinsFile. There are various stages to complete our pipeline.
For more details about the below script, sign in to the configured GitHub repository and get the latest code. Next, it will build the docker image of that application and push it into DockerHub. After that, it will deploy the docker image into the Kubernetes cluster. The Kubernetes deployment YAML file will get the latest image from DockerHub and deploy the latest one.
In the Github Checkout stage, you can see some syntax. Don't worry, Jenkins provides an option to create pipeline syntax. Click on Pipeline Syntax and create it.
For GitHub syntax:
For pushing a Docker Image into Docker Hub, It is required credentials to access the particular repository of the docker hub. To do that, you can generate a script like below.
Step 8
Awesome! That's it. We have almost completed our settings. Now click the Save button at bottom of the configuration page. You will see the pipeline dashboard like below.
Step 9
Now click Build Now option, Make sure your local Kubernetes cluster is in a running state. Once the deployment has completed successfully, you can see light green color filled against the corresponding build no.
Note
As we know, Jenkins is running in our local machine and I mentioned that we are doing an experiment in the local system. You may wonder why do we need to trigger the Build Now option manually? Whenever we check in the code, the build should be triggered automatically right?" The issue is that we cannot configure
http://localhost:8080/ with Online GitHub WebHook Server, so we perform this action manually.
Step 10
Now you can navigate to your Kubernetes dashboard and check out the pods and navigate to the application URL. There you can see your latest changes deployed in the server.
If you are facing any issue while deploying any stage, please click the console output of the corresponding build number in the build history tab. There you can find some logs.
Step 12
If you want to re-configure any steps, you can do by clicking Configure option in the left menu.
Note
If you want to install any plugins, you can install it by hitting the Manage Jenkins option.
I hope this was helpful to you. Enjoy ;)