Jenkins Level 3: Jenkins Deploy Pipeline - Working Sample

I tried the Jenkins Deploy Pipeline task of Jenkins Level 3 for couple of days in different ways that worked perfectly but couldn’t get KodeKloud to accept it as the correct configuration. I finally landed on the script below that was accepted. Jenkins is new to me but KodeKloud’s success criteria for this task is still a mystery to me since it was not adequate that the pipeline simply worked and achieved the goal. I wanted to share this code to give closure to other poor souls like me who keep pulling their hair thinking what did they miss.

pipeline {
    agent {
        label 'ststor01'
    }
    stages {
        stage('Deploy') {
            steps {
                script {
                    // Define variables
                    def repoUrl = 'http://git.stratos.xfusioncorp.com/sarah/web_app.git'
                    def destination = '/var/www/html'

                        // Clone the specified branch from the repository
                        sh "echo 'password_of_natasha' | sudo -S chown -R natasha:natasha ${destination}"
                        dir ("${destination}") {
                            git (
                                branch: "${params.BRANCH}",
                                url: "${repoUrl}"
                            )
						}

                }
            }
        }
    }
    post {
        // Clean after build
        always {
            cleanWs()
        }
    }
}

Required plugins:

  1. Git
  2. SSH Build Agents
  3. Pipeline
  4. Workspace Cleanup

Hi @nimanthanw,

Just to confirm, you’re sharing the solution and there’s no problem with the task?

Hi Raymond,

Good question! I think there is a problem with the task as it was not accepting any other versions of the solution that was achieving the goal. The problem I believe is in the task description where it does not specifically match the validation steps that are looking for specific success criteria. Either the task needs to be more descriptive detailing the success criteria that the validation steps are looking for or the validation steps need to be more flexible to accept a wider range of solutions that are able to achieve the goal.

Got it. I’ll look into another solution that still meets the goals and see if it gets approved.

Hi @nimanthanw

Here is my Jenkins pipeline YAML, and I was still able to complete the task. I don’t think this is a valid issue.

pipeline {
  agent { label 'ststor01' }

  stages {
    stage('Deploy') {
      steps {
        // ensure a clean directory
        deleteDir()
        // checkout directly into /var/www/html
        checkout([
          $class: 'GitSCM',
          branches: [[name: '*/master']],    // or whichever branch
          userRemoteConfigs: [[
            url: 'http://git.stratos.xfusioncorp.com/sarah/web_app.git',
            credentialsId: 'gitea-sarah'
          ]],
          extensions: [
            // place files in the workspace root (which is /var/www/html)
            [$class: 'RelativeTargetDirectory', relativeTargetDir: '.']
          ]
        ])
        // (Optional) adjust permissions
        sh 'chmod -R a+rX .'
      }
    }
  }
    post {
        // Clean after build
        always {
            cleanWs()
        }
    }
}

1 Like

Hi @raymond.baoly
I put the same pipeline yaml and it has run successfully. I was able to reach the App website, I tested also locally with curl command on different stapps it was ok.
However, when checking the task, it fails when the message : Seems like website is not working.
Do you have any idea about the root cause of this error please?

Hi @moez.benabdallah

It’s hard for me to help without more details. Please share your Jenkins pipeline YAML, the steps you took to set up the pipeline, and the curl command you used to check the results. A screenshot would also be very helpful. This way, I can support you better because when I tested it on my side, I didn’t see any issues and couldn’t reproduce your problem.

Hi,

Thanks for your answer. I finally got the root cause of my error.
It was the permissions on /var/www/html directory. I fixed by just chown -R natasha /var/www
Thanks again!
BR
Moez