Lab: Deploying with Multi Stage Pipeline

Could someone please help me understand what is wrong in the jenkinsfile? Also, as per the solution I am supposed to just use sh 'git pull' which doesnt seem to work as well. As far as I understand I need to somehow get the git credentials also in the jenkinsfile, but I assume it is being handled already by the lab environment.

Error encountered:

Started by user bob
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Build Dev)
[Pipeline] node
Running on dev in /opt/workspace/go-app-deployment
[Pipeline] {
[Pipeline] ws
Running in /opt/opt/go-app
[Pipeline] {
[Pipeline] sh
+ git pull http://git-server:3000/bob/go-app.git dev
fatal: not a git repository (or any of the parent directories): .git
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test Dev)
Stage "Test Dev" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy Dev)
Stage "Deploy Dev" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build Prod)
Stage "Build Prod" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test Prod)
Stage "Test Prod" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy production)
Stage "Deploy production" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: script returned exit code 128
Finished: FAILURE

> Blockquote

Jenkinsfile:

pipeline {
  agent none

  stages {
    // DEV
    stage('Build Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "opt/go-app"
        }
      }
      steps {
        sh 'git pull http://git-server:3000/bob/go-app.git dev'
      }
    }
    stage('Test Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'go test ./...'
      }
    }

    stage('Deploy Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        script {
          withEnv(['JENKINS_NODE_COOKIE=do_not_kill']) {
            sh 'go run main.go &'
          }
        }
      }
    }
    //PROD
    stage('Build Prod') {
      agent {
        label {
          label 'prod'
          customWorkspace "opt/go-app"
        }
      }
      steps {
        sh 'git pull http://git-server:3000/bob/go-app.git master'
      }
    }
    stage('Test Prod') {
      agent {
        label {
          label 'prod'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'go test ./...'
      }
    }

    stage('Deploy production') {
      agent {
        label {
          label 'prod'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        script {
          withEnv(['JENKINS_NODE_COOKIE=do_not_kill']) {
            sh 'go run main.go &'
          }
        }
      }
    }

  }
}

figured out the issue. Jenkinsfile syntax was incorrect. Missing / in customWorkspace. Yet I am unable to understand how the git pull works without providing a credential?

Corrected Jenkinsfile:

pipeline {
  agent none

  stages {
    // DEV
    stage('Build Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'git pull http://git-server:3000/bob/go-app.git dev'
      }
    }
    stage('Test Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'go test ./...'
      }
    }

    stage('Deploy Dev') {
      agent {
        label {
          label 'dev'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        script {
          withEnv(['JENKINS_NODE_COOKIE=do_not_kill']) {
            sh 'go run main.go &'
          }
        }
      }
    }
    // PROD
    stage('Build Prod') {
      agent {
        label {
          label 'prod'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'git pull http://git-server:3000/bob/go-app.git master'
      }
    }
    stage('Test Prod') {
      agent {
        label {
          label 'prod'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        sh 'go test ./...'
      }
    }

    stage('Deploy Prod') {
      agent {
        label {
          label 'prod'
          customWorkspace "/opt/go-app"
        }
      }
      steps {
        script {
          withEnv(['JENKINS_NODE_COOKIE=do_not_kill']) {
            sh 'go run main.go &'
          }
        }
      }
    }
  }
}

It will be because there is already an initialized git repo in /opt/go-app and the credentials must already be present in the jenkins server.

The original error is because opt/go-app is a directory relative to the working directory of the build agent (which isn’t /) and that directory more than likely doesn’t exist, let alone being an initialized repo directory.

1 Like