From 818e4670b8034732ed2482130a048917706c9eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=8C=AF=E5=AE=87?= <> Date: Sat, 8 Feb 2025 11:38:50 +0800 Subject: [PATCH] fix(pipeline): refine buildArtifacts handling to support directory and file stashing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 孙振宇 <> --- first-class-pipeline/tests/Jenkinsfile | 4 +-- .../vars/executeFreeleapsPipeline.groovy | 27 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/first-class-pipeline/tests/Jenkinsfile b/first-class-pipeline/tests/Jenkinsfile index 8071f451..7d8b0dd8 100644 --- a/first-class-pipeline/tests/Jenkinsfile +++ b/first-class-pipeline/tests/Jenkinsfile @@ -42,7 +42,7 @@ executeFreeleapsPipeline { // buildCommand used to specify the build command of the component buildCommand: 'npm run build', // buildArtifacts used to specify the build artifacts that needs to be stores and shares between components - buildArtifacts: ['dist/**'], + buildArtifacts: ['dist'], // lintEnabled used to specify whether to enable code lint lintEnabled: false, // linter used to specify the code linter @@ -91,7 +91,7 @@ executeFreeleapsPipeline { // buildAgentImage used to specify the build environment container image buildAgentImage: 'python:3.8-slim-buster', // buildArtifacts used to specify the build artifacts that needs to be stores and shares between components - buildArtifacts: ['./**'], + buildArtifacts: ['.'], // buildCacheEnabled used to specify whether to enable build dependencies cache buildCacheEnabled: true, // buildCommand used to specify the build command of the component diff --git a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy index 4fde3d3e..49458c78 100644 --- a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy +++ b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy @@ -319,7 +319,26 @@ def generateComponentStages(component, configurations) { sh component.buildCommand } component.buildArtifacts.each { artifact -> - stash includes: artifact, name: "${component.name}-${artifact}" + steps.log.info("Pipeline", "Stashing artifact ${artifact} for ${component.name}...") + def targetPathType = sh( + script: """ + if [ -d "${artifact}" ]; then + echo "dir" + elif [ -f "${artifact}" ]; then + echo "file" + else + echo "unknown" + fi + """, + returnStdout: true + ) + if (artifact == '.' || artifact == './') { + stash includes: ".", name: "${component.name}-root" + } else if (targetPathType.trim() == "dir") { + stash includes: "${artifact}/**", name: "${component.name}-${artifact}" + } else { + stash includes: artifact, name: "${component.name}-${artifact}" + } } } } @@ -377,7 +396,11 @@ def generateComponentStages(component, configurations) { dir(env.workroot + "/" + component.root) { component.buildArtifacts.each { artifact -> - unstash "${component.name}-${artifact}" + if (artifact == '.' || artifact == './') { + unstash "${component.name}-root" + } else { + unstash "${component.name}-${artifact}" + } } if (component.dockerfile != null && !component.dockerfile.isEmpty()) {