DEPLOY WEBSERVER ON KUBERNETES USING JENKINSFILE APPROACH




Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.


This Story explains deployment of a website using apache webserver on Kubernetes. There are a lot of other ways to do the same but, here I have used Jenkinsfile approach.


Demo

Pre-requisites:

  1. Jenkins

  2. Minikube

  3. Job-DSL plugin for Jenkins

  4. Build-Result Trigger plugin for Jenkins

  5. Github plugin for Jenkins

This story does not cover the installation of these softwares and plugins.


Process:

1. First we need to create the DSL script for the job. The DSL script will create 4 jobs. DSL scripts use groovy syntax and the script has been saved to a .groovy file and uploaded to Github. Create a seed job to pull the script from github repo and use the job DSL plugin to deploy the setup. On deploying the seed job for 1st time you may face an error that say Script not approved. Go to Manage Jenkins>In-Process Script Approval and approve the script. You have to do this each time you update the script. DSL script:-

job("Pull code from Github Repo"){
              description("The job will pull the code from github repo and save it to a folder")              scm{
                    github('chirag0202/project','master')
                  }              triggers{
                        githubPush()
                        }
                steps{
                      shell("sudo cp -rvf index.html /task6")
                      }
         
}
job("Deploy code on web server"){
              description("The job will deploy the code on a webserver")                  scm{
                    github('chirag0202/webserver-kube','master')
                  }                  triggers{
                        upstream("Pull code from Github Repo")
                        }                  steps{
                      shell(readFileFromWorkspace