java.nio.file.NoSuchFileException 詹金斯

java.nio.file.NoSuchFileException JENKINS

我有以下代码不适合我。

  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"

但是我收到以下错误。如何加载文件并稍后使用其内部函数。

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

来自 dir 步骤文档:

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.

dir 命令仅更改在 dir {} 块内执行的代码的基本目录,因此当您在 dir 块之后加载文件时,您将返回到原始工作区并且找不到文件。
要解决它,您可以使用完整路径来加载文件:

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"

或者加载 dir 块中的文件:

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"
}