在同一 Jenkins 项目的后期重用工件

ris*_*en0 12 jenkins jenkins-pipeline

我有一个 Jenkins 管道,它的 Build 步骤有一个archiveArtifacts命令。

在构建步骤之后是单元测试、集成测试和部署。

在部署步骤中,我想使用其中一个工件。我以为我可以在 Build 步骤生成它的同一个地方找到它,但显然archiveArtifacts已经删除了它们。

作为一种解决方法,我可以在存档之前复制工件,但对我来说它看起来并不优雅。有没有更好的办法?

Spe*_*one 15

As I understand it, archiveArtifacts is more for saving artifacts for use by something (or someone) after the build has finished. I would recommend looking at using "stash" and "unstash" for transferring files between stages or nodes.

You just go...

stash include: 'globdescribingfiles', name: 'stashnameusedlatertounstash'
Run Code Online (Sandbox Code Playgroud)

and when you want to later retrieve that artifact...

unstash 'stashnameusedlatertounstash'
Run Code Online (Sandbox Code Playgroud)

并且隐藏的文件将放入当前工作目录中。

这是 Jenkinsfile 文档中给出的示例(https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents):

pipeline {
    agent none
    stages {
        stage('Build') {
            agent any
            steps {
                checkout scm
                sh 'make'
                stash includes: '**/target/*.jar', name: 'app' 
            }
        }
        stage('Test on Linux') {
            agent { 
                label 'linux'
            }
            steps {
                unstash 'app' 
                sh 'make check'
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
        stage('Test on Windows') {
            agent {
                label 'windows'
            }
            steps {
                unstash 'app'
                bat 'make check' 
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#stash-stash-some-files-to-be-used-later-in-the-build (3认同)
  • 对于大文件来说,这显然是一个坏主意(即,如果您在多个阶段构建/测试) (3认同)