Search code examples
jenkinsjenkins-pipelinejenkins-groovy

java.nio.file.NoSuchFileException JENKINS


I have the following code which is not working for me.

  dir('anyName'){
    checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
      userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
    ]
  }

  def some = load "./pipelines/environment/some.groovy"

But I get the following error. How can I load a file and later use its internal function.

java.nio.file.NoSuchFileException: /mnt/data/jenkins/workspace//pipelines/environment/some.groovy


Solution

  • From the dir step documentation:

    Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path.

    The dir command changes the base directory only for code executed inside the dir {} block, therefore when you load your file after the dir block you are back to the original workspace and the file is not found.
    to solve it you can use the full path to load the file:

    dir('anyName'){
        checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
                       userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
        ]
    }
    // From here we are back to the default workspace
    def some = load "./anyName/pipelines/environment/some.groovy"
    

    Or alternatively load the file inside the dir block:

    dir('anyName'){
        checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
                       userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
        ]
        def some = load "./pipelines/environment/some.groovy"
    }