0

When Jenkins clone my repo in my server, where it puts the project? And after building, does it delete everything automatically, or should I add something in my Jenkinsfile to do that?

I'm trying to list the files using:

pipeline {
    agent any

    stages {
        stage('just testing') {
            steps {
                ls -l
            }
        }
    }
}

But it returns invalid syntax. I've also tried sh "ls -l".

Thank you very much.

3
  • Jenkins does not just clean up things automagically. You'll have to add things to make that happen. Have you seen this yet?
    – John
    Oct 1, 2018 at 23:20
  • Just guessing right now, but the contents of a steps block likely needs to be a valid Jenkins pipeline step. Have you tried wrapping your ls -l inside a script block?
    – John
    Oct 1, 2018 at 23:27
  • @John no need for a script block, the mentioned sh "ls -l" is sufficient. But yes, the big code snippet posted here is, of course, invalid. Oct 2, 2018 at 4:07

1 Answer 1

2

When you run your Jenkinsfile and it is obtained from a source repository, Jenkins automatically creates a workspace dynamically for the job and by default it places any other files in your project into that workspace.

In your example, you have used "agent any" then in your stage you are using Linux specific commands like "sh 'ls -l" to list the files. The first thing to be aware of is that "agent any" can run on any slave configured on your Jenkins server, hence it may run on a Linux or Windows slave depending upon the configuration. So the "sh" step may fail if that tries to run on a Windows slave. You can use agent with a label to be more specific on the node/slave selection like this (the labels available depend on your Jenkins slave config):

agent {
    label "LINUX"
}

After building, it does not delete everything automatically as it keeps the workspace for each build of the job on the Jenkins master. You can solve this in 2 ways:

1) Use an options section in your pipeline to discard old builds:

  options {
    // Keep 4 builds maximum
    buildDiscarder(logRotator(numToKeepStr: '4'))
  }

2) Use a post-handler "always" section to clean up:

  post {
    always {
      deleteDir()
    }
  }

Here is a pipeline that may help to get you started:

pipeline {

  agent any

  options {
    buildDiscarder(logRotator(numToKeepStr: '4'))
  }

  stages {

    stage('List files in repo on Unix Slave') {

      when {
        expression { isUnix() == true }
      }

      steps {      
        echo "Workspace location: ${env.WORKSPACE}"    
        sh 'ls -l'
      }
    }

    stage('List files in repo on Windows Slave') {

      when {
        expression { isUnix() == false }
      }

      steps {      
        echo "Workspace location: ${env.WORKSPACE}"  
        bat 'dir'
      }
    }
  }

  post {
    always {
      deleteDir()
    }
  }
}

Here is the output I get with some of the Git chatter redacted:

Pipeline] node
Running on master in /var/jenkins_home/jobs/macg33zr/jobs/testRepo/branches/master/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://github.com/xxxxxxxxxx

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (List files in repo on Unix Slave)
[Pipeline] isUnix
[Pipeline] echo
Workspace location: /var/jenkins_home/jobs/macg33zr/jobs/testRepo/branches/master/workspace
[Pipeline] sh
[workspace] Running shell script
+ ls -l
total 8
-rw-r--r-- 1 jenkins jenkins 633 Oct  1 22:07 Jenkinsfile
-rw-r--r-- 1 jenkins jenkins  79 Oct  1 22:07 README.md
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (List files in repo on Windows Slave)
Stage 'List files in repo on Windows Slave' skipped due to when conditional
[Pipeline] isUnix
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
1
  • I'd recommend using the cleanup condition in your post section, so that it gets called after everything else just in case you want to use any of the other post conditions. And while deletDir() is great, sometimes it's good to use the cleanWs() from the workspace cleanup plugin as it has options (such as cleanWhenFailure, which you can set to false to keep the workspace for troubleshooting when the build fails.
    – John
    Oct 1, 2018 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.