Groovy 如何从管道中使用Jenkins复制工件插件(Jenkins文件)?

Groovy 如何从管道中使用Jenkins复制工件插件(Jenkins文件)?,groovy,jenkins-workflow,jenkinsfile,Groovy,Jenkins Workflow,Jenkinsfile,我试图从Jenkins管道(工作流)中找到一个使用Jenkins复制工件插件的例子 有人能指出正在使用它的示例Groovy代码吗?如果您在主程序中使用从属程序,并且希望在彼此之间复制工件,则可以使用stash/unstash,例如: stage 'build' node{ git 'https://github.com/cloudbees/todo-api.git' stash includes: 'pom.xml', name: 'pom' } stage name: 'test

我试图从Jenkins管道(工作流)中找到一个使用Jenkins复制工件插件的例子


有人能指出正在使用它的示例Groovy代码吗?

如果您在主程序中使用从属程序,并且希望在彼此之间复制工件,则可以使用stash/unstash,例如:

stage 'build'
node{
   git 'https://github.com/cloudbees/todo-api.git'
   stash includes: 'pom.xml', name: 'pom'
}

stage name: 'test', concurrency: 3
node {
   unstash 'pom'
   sh 'cat pom.xml' 
}
您可以在此链接中看到此示例:


尝试使用复制工件插件

如果构建未在同一管道中运行,您可以使用direct
CopyArtifact
插件,下面是示例:和示例代码:

node {
   // setup env..
   // copy the deployment unit from another Job...
   step ([$class: 'CopyArtifact',
          projectName: 'webapp_build',
          filter: 'target/orders.war']);
   // deploy 'target/orders.war' to an app host
}

使用声明性文件,您可以使用以下管道:

pipeline {
    agent any
    stages {
        stage ('push artifact') {
            steps {
                sh 'mkdir archive'
                sh 'echo test > archive/test.txt'
                zip zipFile: 'test.zip', archive: false, dir: 'archive'
                archiveArtifacts artifacts: 'test.zip', fingerprint: true
            }
        }

        stage('pull artifact') {
            steps {
                copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')
                unzip zipFile: 'test.zip', dir: './archive_new'
                sh 'cat archive_new/test.txt'
            }
        }
    }
}
在CopyArtifact1.39版本之前,您必须将第二阶段替换为以下内容(感谢@Yeroc):

对于
CopyArtifact
,我使用“${JOB\u NAME}”作为项目名称,它是当前正在运行的项目

CopyArtifact
使用的默认选择器使用上次成功的项目生成编号,而不是当前编号(因为它尚未成功或未成功)。使用
SpecificBuildSelector
可以选择“${BUILD\u NUMBER}”,其中包含当前正在运行的项目内部版本号

这个管道与并行阶段一起工作,可以管理巨大的文件(我使用的是一个300Mb的文件,它不能与stash/unstash一起工作)

这个管道与我的Jenkins 2.74完美配合,只要你有所有需要的插件


你知道这一步在哪里吗([$class:'CopyArtifact',语法有文档记录吗?我想我记得它是由代码段生成器生成的,现在在那里找不到它。我特别想知道如何使用参数化的构建选择器。我看到了一个示例。关于
SpecificBuildSelector
,我通过分析CopyArtifact插件1.39版之后的版本来猜测它您可以使用更简洁的语法:
copyArtifacts过滤器:'test.zip',fingerprintArtifacts:true,projectName:'${JOB\u NAME}',selector:specific('${BUILD\u NUMBER}')
如果有人对Jenkins表示无法找到要从中复制工件的项目有问题,请更改
projectName:'${JOB\u NAME}',selector:specific('code>)${BUILD_NUMBER}')
to
projectName:env.JOB_NAME,selector:specific(env.BUILD_NUMBER)
对我有效(另请参见)。copyArtifacts(projectName:/')对meDid有效你是说“stash”而不是“stage”?我在stages中使用了stash,我不明白
pipeline {
    agent any
    stages {
        stage ('push artifact') {
            steps {
                sh 'mkdir archive'
                sh 'echo test > archive/test.txt'
                zip zipFile: 'test.zip', archive: false, dir: 'archive'
                archiveArtifacts artifacts: 'test.zip', fingerprint: true
            }
        }

        stage('pull artifact') {
            steps {
                copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')
                unzip zipFile: 'test.zip', dir: './archive_new'
                sh 'cat archive_new/test.txt'
            }
        }
    }
}
stage('pull artifact') {
    steps {
        step([  $class: 'CopyArtifact',
                filter: 'test.zip',
                fingerprintArtifacts: true,
                projectName: '${JOB_NAME}',
                selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}']
        ])
        unzip zipFile: 'test.zip', dir: './archive_new'
        sh 'cat archive_new/test.txt'
    }
}