[英]How to read yaml file from master using Jenkinsfile pipeline script when jenkins job is running on slave machine?
I am building a Shared Library(I'm not a CI engineer) for Jenkins pipeline.我正在为 Jenkins 管道构建一个共享库(我不是 CI 工程师)。 As a part of it, we are thinking of giving configurations in yaml file and have Jenkinsfile pipeline script read from the yaml file.
作为其中的一部分,我们正在考虑在 yaml 文件中提供配置,并从 yaml 文件中读取 Jenkinsfile 管道脚本。
So, we are planning to commit Jenkinsfile and yaml file in one git repository (let's say repo A) and the job is going to run on a slave machine utilising another git repository (let's say repo B).因此,我们计划将 Jenkinsfile 和 yaml 文件提交到一个 git 存储库(假设为 repo A)中,并且该作业将在使用另一个 git 存储库(假设为 repo B)的从属机器上运行。 The Jenkinsfile will be executed from master after it clones repo A. The yaml file is also in master workspace.
Jenkinsfile 将在克隆 repo A 后从 master 执行。 yaml 文件也在 master 工作区中。 In the slave, repo B will be cloned and a build will take place as defined in the Jenkinsfile.
在从站中,将克隆 repo B 并按照 Jenkinsfile 中的定义进行构建。 But the question I have is, how do I read yaml file from Jenkinsfile script without having to clone repo A in slave ie, how I reference a file present in master and not in slave from Jenkinsfile?
但我的问题是,如何从 Jenkinsfile 脚本中读取 yaml 文件,而不必在从站中克隆 repo A,即,我如何从 Jenkinsfile 中引用 master 中存在的文件而不是 slave 中的文件? This question arises because, whatever file I am trying to open is being opened from slave and not from master.
出现这个问题是因为,我尝试打开的任何文件都是从从属设备而不是从主设备打开的。
Thanks in advance.提前致谢。
EDIT: Soemthing I forgot to mention.编辑:我忘记提及的事情。 We actually thought of using stash from master and unstash in slave.
我们实际上考虑过在 master 中使用 stash 并在 slave 中使用 unstash。 But the problem is that we do only the job configurations and the environment is provided to us by some other team which also provides to many other teams.
但问题是我们只做工作配置,环境是由其他一些团队提供给我们的,这些团队也提供给许多其他团队。 Because of that, master rarely has executors free.
因此,master 很少有空闲的 executors。 So, even when we run the job, it hangs waiting for the master to be free.
因此,即使我们运行该作业,它也会挂起等待 master 空闲。 Is there any other way to load the yaml file when the pipeline script is being loaded in master's memory?
当管道脚本加载到主内存中时,还有其他方法可以加载 yaml 文件吗?
I think the most straightforward way is to use the stash
.我认为最直接的方法是使用
stash
。 Stashes are temporary packages that can be created on one node and copied to any other node. Stashes 是可以在一个节点上创建并复制到任何其他节点的临时包。
There is some overhead on master for the package creation so stashes are not recommended for really big files, but they are ideally suited for your usecase of transfering a small configuration file. master 上有一些用于包创建的开销,因此不建议将 stashes 用于真正的大文件,但它们非常适合您传输小配置文件的用例。
node('master') {
// Create temporary stash package on master
stash name: 'MyConfiguration', includes: 'SomeFile.yaml'
}
node('MySlave') {
// Copy to and extract the stash package on slave
unstash 'MyConfiguration'
}
This expects 'SomeFile.yaml' to be in the workspace on master and will also extract it to workspace on slave.这期望 'SomeFile.yaml' 位于 master 上的工作区中,并且还将其解压缩到 slave 上的工作区。 In case you want a sub directory of
WORKSPACE
, simply wrap the stash
and/or unstash
steps in a dir
step.如果您想要
WORKSPACE
的子目录,只需将stash
和/或unstash
步骤包装在dir
步骤中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.