【问题标题】:How to copy a file from Jenkins Agent node to a remote server using Jenkinsfile如何使用 Jenkinsfile 将文件从 Jenkins 代理节点复制到远程服务器
【发布时间】:2019-12-20 12:54:30
【问题描述】:

我有一个 Jenkinsfile,它实现了一个管道,并且在一个阶段和一个节点下,如果我说 unstash 'myfile',这个 'myfile' 将在节点上的哪个位置可用?我的要求是作为 Jenkinsfile 脚本的一部分,我需要访问这个文件并复制到一个已知的远程服务器(这个远程服务器不是 Jenkins 池的一部分)。

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    您可以使用SSH Pipeline Steps 将文件复制到远程服务器。以下是如何将文件从作业工作区发送到远程主机的示例:

    remote = [:]
    
    remote.name = "name"
    remote.host = "remote_ip"
    remote.allowAnyHosts = true
    remote.failOnError = true
    withCredentials([usernamePassword(credentialsId: 'credentials_name', passwordVariable: 'password', usernameVariable: 'username')]) {
        remote.user = username
        remote.password = password
    }
    
    sshPut remote: remote, from: 'myfile', into: 'folder_on_remote_host'
    

    【讨论】:

      【解决方案2】:

      我说 unstash 'myfile',这个 'myfile' 将在节点上的哪个位置可用?

      您不使用unstash 'myfile',而是使用unstash 'my_stash',其中my_stash 是您之前保存存储时使用的名称。存储可以包含一个文件,也可以包含整个目录树。它的内容是在您存储它的那一刻定义的(相对于运行stash 的节点上的${WORKSPACE})并且它以完全相同的方式取消存储,相对于运行unstash 的节点上的${WORKSPACE}

      工作区是根据您的代理配置定位的(在我这里,它位于 /Users/jenkins/workspace/<a folder Jenkins creates>),但出于所有实际目的 - 因为您在节点上的步骤也在该文件夹中运行 - 您可以参考它是.,例如

      stage ('stash') {
         node { label  'one' }
         steps {
             script {
                 sh "echo 1 > myfile.txt" // runs in $WORKSPACE, creates $WORKSPACE/myfile.txt
                 stash name: "my_stash", includes: "myfile.txt" // relative to $WORKSPACE
             }
         }
      }
      
      stage ('unstash') {
         node { label  'two' }
         steps {
             script {
                 unstash name: "my_stash"  // runs in $WORKSPACE, creates $WORKSPACE/myfile.txt
                 sh "cat ./myfile.txt"
             }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-25
        • 2021-09-16
        • 2021-09-28
        • 1970-01-01
        • 2015-05-08
        • 1970-01-01
        • 2019-01-20
        相关资源
        最近更新 更多