简体   繁体   English

当 jenkins 作业在从机上运行时,如何使用 Jenkinsfile 管道脚本从主机读取 yaml 文件?

[英]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.

相关问题 Jenkins-如何在从属机器上建立主作业? - Jenkins - How to build a master job in slave machine? 在 Pipeline 中将文件从 Jenkins master 复制到 slave - Copy file from Jenkins master to slave in Pipeline Jenkins Job DSL:如何从文件中读取Pipeline DSL脚本? - Jenkins Job DSL: How to read Pipeline DSL script from file? 如何在jenkins管道脚本中从jenkins master指定jenkins slave git地址 - how to specify jenkins slave git address from jenkins master in jenkins pipeline script 如何在一个 jenkins 管道中使用 SCM 中的多个存储库时从触发 jenkins 作业的 repo 构建 jenkinsfile? - How to build jenkinsfile from a repo which triggered jenkins job when using multiple repositories in SCM in one jenkins pipeline? 是否可以选择从 Jenkins 作业调用 Jenkinsfile(例如我们如何在管道中指定脚本路径)? - Is there an option to call Jenkinsfile from Jenkins job (like how we specify script path in pipeline)? 使用来自master(jenkins)的jenkins在slave(unix)机器上运行挤压测试 - Running squish test on slave (unix) machine using jenkins from master (windows) Jenkins - 在 master 和 slave 中运行单个作业 - Jenkins - Running a single job in master as well as slave 如何使用jenkins将文件从一台从属计算机复制到另一台从属计算机 - how to copy file from one slave machine to another using jenkins 在管道中从jenkinsfile触发另一个jenkins作业 - triggering another jenkins job from jenkinsfile in pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM