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

问题描述 投票:22回答:4

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

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

groovy jenkins-workflow jenkinsfile
4个回答
25
投票

如果构建没有在同一个管道中运行,你可以使用直接的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
}

28
投票

使用声明性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'
            }
        }
    }
}

在CopyArtifact的版本1.39之前,您必须用以下替换第二阶段(感谢@Yeroc):

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'
    }
}

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

CopyArtifact使用的默认选择器使用最后一个成功的项目构建号,从不使用当前构建号(因为它还没有成功,或者没有)。使用SpecificBuildSelector,您可以选择“$ {BUILD_NUMBER}”,其中包含当前正在运行的项目内部版本号。

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

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


22
投票

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

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

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


1
投票
name = "/" + "${env.JOB_NAME}"
def archiveName = 'relNum'
try {
    step($class: 'hudson.plugins.copyartifact.CopyArtifact', projectName: name, filter: archiveName)
} catch (none) {
    echo 'No artifact to copy from ' + name + ' with name relNum'
    writeFile file: archiveName, text: '3'
}

def content = readFile(archiveName).trim()
echo 'value archived: ' + content

尝试使用复制工件插件

© www.soinside.com 2019 - 2024. All rights reserved.