如何在声明性管道中的 Jenkinsfile 中跨阶段存储和取消存储人工制品

时间:2021-05-04 10:30:31

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

我在带有 decarative 管道的 Jenkinsfile 中有 2 个阶段

stage("Build_Stage") {
  steps {
    sh './build.sh 2>&1 ; test ${PIPESTATUS[0]} -eq 0'
    // The output directory is ./build. How to stash this here
  }
}
stage("Upload_Artefacts_Stage") {
  steps {
    // How to unstash the build directory which was stashed in Build_Stage
    sh './prepare_build_artefacts.sh ios 2>&1 ; test ${PIPESTATUS[0]} -eq 0'
  }
}

prepare_build_artefacts.sh 接收跨阶段调用的 build.sh 的输出。

如何在构建阶段之后的第二阶段存储输出构建目录并取消存储它?

1 个答案:

答案 0 :(得分:1)

请参阅下面的代码,它可以帮助您存储和取消存储。请注意,存储和取消存储步骤是为小文件设计的。对于大型数据传输,请使用外部工作区管理器插件,或使用外部存储库管理器,例如 Nexus 或 Artifactory

stage("Build_Stage") {
  steps {
    sh './build.sh 2>&1 ; test ${PIPESTATUS[0]} -eq 0'
    // The output directory is ./build. How to stash this here
    // Below method will stash build directory 
    stash includes: "build/" , name: "Build_stash"
  }
}
stage("Upload_Artefacts_Stage") {
  steps {
    // How to unstash the build directory which was stashed in Build_Stage
    // Give path of the directory where you would like to unstash it.
    dir("C:\\Data\\CI_Workspace\\${env.JOB_NAME}")
    {
        unstash "Build_stash"
    }
    sh './prepare_build_artefacts.sh ios 2>&1 ; test ${PIPESTATUS[0]} -eq 0'
  }
}