正确使用 stashunstash 到不同的目录 [英] Correct usage of stashunstash into a different directory

查看:19
本文介绍了正确使用 stashunstash 到不同的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个阶段中,我需要在构建完成后复制两个文件夹的内容并复制到不同的目录.

In one of my stages I need to copy the contents of two folders after a build is completed and copy to a different directory.

我实际上正在将自由式作业转换为管道,并且一直在使用工件部署器插件.仔细阅读,看起来 stash 和 unstash 命令应该有助于实现我想要实现的目标.

I am actually converting a freestyle job to pipeline, and have been using the artifact deployer plugin. Reading around, it looks like stash and unstash commands should help with what I want to achieve.

有人可以验证这是否是下面的正确方法吗?

Can someone verify if this is the correct approach below please?

stage('Build') {
      steps {
        sh '''
          gulp set-staging-node-env
          gulp prepare-staging-files
          gulp webpack
        '''
        stash includes: '/dist/**/*', name: 'builtSources'
        stash includes: '/config/**/*', name: 'appConfig'
        dir('/some-dir') {
          unstash 'builtSources'
          unstash 'appConfig'
        }
      }
    }

如果我在一个阶段更改 dir,这是否意味着此后的所有其他阶段都将尝试从该目录执行命令,或者它们会重新使用工作区默认位置?

If I change dir in one stage, does that mean all other stages thereafter will try to execute commands from that directory, or do they do back to using the workspace default location?

谢谢

编辑

我意识到我真正想做的是将构建的源代码复制到不同的节点(运行不同的操作系统).因此,在我共享的片段中,我正在切换目录,该目录实际上位于我设置的另一台机器(节点)上.

I have realised what I actually want to do is to copy built sources to a different node (running a different OS). So in my snippet I have shared, where I am switching directories, that directory is actually to be on a different machine (node) that I have setup.

我需要用 node('my-node-name') 块包装 dir() 块吗?我正在努力寻找示例.

Would I need to wrap the dir() block with a node('my-node-name') block? Im struggling to find examples.

谢谢

推荐答案

希望是这样的:

stash includes: 'dist/**/*', name: 'builtSources'

stash includes: 'config/**/*', name: 'appConfig'

其中 dist 和 config 是工作空间路径中的目录,所以应该是像上面这样的相对路径.

where dist and config are the directories in the workspace path, so it should be a relative path like above.

Rest 看起来不错,只是提到路径/some-dir"应该由 jenkins 用户(用于运行 jenkins 守护程序的用户)可写.

Rest seems alright, only to mention that path "/some-dir" should be writable by jenkins user (user used to run jenkins daemon).

是的,当它退出 dir 块时,它会退回到其封闭的工作区路径(在这种情况下为默认).

And yes it falls back to its then enclosing workspace path (in this case default) when it exits dir block.

编辑

因此,当您存储路径时,可以在管道中稍后的任何步骤取消存储它.所以是的,您可以将 dir 块放在 node('<nodename>') 块下.

So when you stash a path, it is available to be unstashed at any step later in the pipeline. So yes, you could put dir block under a node('<nodename>') block.

你可以添加这样的东西:

You could add something like this :

stage('Move the Build'){
  node('datahouse'){
    dir('/opt/jenkins_artifacts'){
      unstash 'builtSources'
      unstash 'appConfig'
    }
  }
}

这篇关于正确使用 stashunstash 到不同的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆