groovy 如何在管道(jenkinsfile)中使用Jenkins复制工件插件?

rqqzpn5f  于 2022-11-01  发布在  Jenkins
关注(0)|答案(4)|浏览(147)

我试图找到一个在Jenkins管道(工作流)中使用Jenkins复制工件插件的示例。
有人能指出一个使用它的示例Groovy代码吗?

fjaof16o

fjaof16o1#

通过声明性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: env.JOB_NAME, selector: specific(env.BUILD_NUMBER)
                unzip zipFile: 'test.zip', dir: './archive_new'
                sh 'cat archive_new/test.txt'
            }
        }
    }
}

在CopyArtifact的1.39版本之前,您必须将第二阶段替换为以下内容(感谢@ Yeoc):

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}”。
此管道可与并行阶段一起工作,并可管理大型文件(我使用的是300 Mb文件,它不适用于stash/unstash)
这个管道与我的Jenkins 2.74完美地工作,前提是你有所有需要的插件

kgsdhlau

kgsdhlau2#

如果您在控制器中使用代理程式,并且想要在彼此之间复制产物,则可以使用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

uz75evzq

uz75evzq3#

如果构建不在同一管道中运行,您可以使用直接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
}
qqrboqgw

qqrboqgw4#

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

尝试使用复制工件插件

相关问题