如何在管道(jenkinsfile)中使用Jenkins Copy Artifacts插件?

sor*_*rin 27 groovy jenkins-workflow jenkinsfile

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

任何人都可以指向使用它的示例Groovy代码吗?

Nel*_* G. 31

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

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'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有了CopyArtifact,我使用'$ {JOB_NAME}'作为项目名称,这是当前正在运行的项目.

CopyArtifact使用上次成功的项目内部版本编号时使用的默认选择器,从不使用当前版本(因为它尚未成功或不成功).有了SpecificBuildSelector你可以选择"$ {} BUILD_NUMBER"包含当前正在运行的项目版本号.

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

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

  • 从CopyArtifact插件版本1.39开始,您可以使用更清晰的语法:`copyArtifacts filter:'test.zip',fingerprintArtifacts:true,projectName:'$ {JOB_NAME}',selector:specific('$ {BUILD_NUMBER}')` (6认同)
  • 如果有人对 Jenkins 存在问题,指出无法找到从中复制工件的项目,请将 `projectName: '${JOB_NAME}', 选择器: Specific('${BUILD_NUMBER}')` 更改为 `projectName: env。 JOB_NAME,选择器:特定(env.BUILD_NUMBER)`为我工作(另请参阅/sf/answers/3876452961/)。 (3认同)

kry*_*nio 26

如果构建没有在同一个管道中运行,你可以使用直接CopyArtifact插件,例如:https://www.cloudbees.com/blog/copying-artifacts-between-builds-jenkins-workflow和示例代码:

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
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*dez 25

如果您在主服务器中使用从服务器并且希望在彼此之间复制工件,则可以使用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' 
}
Run Code Online (Sandbox Code Playgroud)

您可以在此链接中看到此示例:

https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow