如何在归档构建后删除原始 Jenkins 构建

huangapple go评论78阅读模式
英文:

How to delete original jenkins build after archiving the build

问题

我正在使用以下的Jenkinsfile来创建Maven构建,并在构建后进行存档。

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                withMaven(maven: 'mvn installed') {
                    sh "mvn clean package"
                }
            }
            post {
                success {
                    archiveArtifacts 'target/*.jar'
                }
            }
        }
    }
}

在存档构建之后,它会被保存在target文件夹中,其中包含我想要使用的jar文件。然而,构建文件夹还包含了原始的com..文件夹,其中包含相同的jar文件。我是否有办法在存档后删除这个com文件夹?因为它只是在我的服务器上占用了额外的空间,我根本没有在使用。

编辑:我不希望清理工作区,而是要删除builds/<build_no>/archive/文件夹内部的原始com文件夹。

英文:

I am using below Jenkinsfile to create maven build and archive it after.

pipeline {
agent any

stages {
    stage(&#39;Build&#39;) {
        steps {
            withMaven(maven: &#39;mvn installed&#39;) {
                sh &quot;mvn clean package&quot;
            }
        }
        post {
            success {
                archiveArtifacts &#39;target/*.jar&#39;
            }
        }
    }
}

After archiving the build it gets saved to target folder which has the jar file I want to use. However the build folder also contains the original com.. folder that contains the same jar. Is there any way I can delete this com folder after archiving, because it is just taking extra space in my server that I am not using at all.

Edit: I am not looking to clean the workspace but rather the original com folder inside the builds/<build_no>/archive/ folder

答案1

得分: 1

基于 @dev-joshi 的编辑,

不是要清理工作区,而是原始的 com 文件夹
在 builds/<build_no>/archive/ 文件夹内

我猜你指的是**“归档构建产物”**后构建步骤的目标位置,而不是 maven 编译步骤的 ./target 目录输出。

你的答案在于**“[ X ] 丢弃旧的构建”**选项
如何在归档构建后删除原始 Jenkins 构建

“保留带有构建产物的最大构建数” [ 1 ] <br>
[如果不为空,仅保留最多这数量的构建带有它们的构建产物]<br>
将其设置为 [ 1 ] 应该能实现你的目标。

如果使用管道,这个 S/O 答案语法文档 提供了语法选项:

options { 
    buildDiscarder(logRotator(artifactNumToKeepStr: '1')) 
}

你需要执行另一个构建才能清理现有的构建产物。

我还建议你设置归档选项:<br>
"[ X ] 仅在构建成功时归档构建产物"<br>
你不希望在部分完成的构建中清除成功的构建产物。

还要注意mvn install 会按照标准的 maven 规则将生成的构建产物复制到本地仓库。通常这在本地工作区之外(除非选择 [ x ] 私有仓库)。然后,你可以运行 mvn clean 来删除本地工作区中的中间构建产物(例如 *.class*.jar 等),使其回到构建前的状态。你可以使用**“删除工作区”**后构建步骤,并使用“要删除的文件的模式”来实现类似的效果(如果你知道你在做什么,或者只是简单地删除整个工作区)。

英文:

Based on @dev-joshi's edit,

> not looking to clean the workspace but rather the original com folder
> inside the builds/<build_no>/archive/ folder

I am guessing you are referring to the the target location for the "Archive the artifacts" post-build step and not the ./target directory, output of the maven compile step.

Your answer lies in the "[ X ] Discard old builds" option
如何在归档构建后删除原始 Jenkins 构建

"Max # of builds to keep with artifacts" [ 1 ] <br>
[if not empty, only up to this number of builds have their artifacts retained]<br>
Setting that to [ 1 ] should achieve your objective.

If using a pipeline, this S/O answer and the syntax docs provides the syntax options:

 options { 
	buildDiscarder(logRotator(artifactNumToKeepStr: &#39;1&#39;)) 
 }

You will have to execute another build for the existing artifacts to be cleaned up.

I'd also suggest you set Archive option:<br>
"[ X ] Archive artifacts only if build is successful"<br>
You don't want to blow away successful artifacts with a partially completed build.

Note too: mvn install will copy the generated artifacts to the local repo, following standard maven rules. That is normally outside the local workspace (unless choosing [ x ] private repo). You can then run mvn clean to delete the intermediary artifacts (*.class and *.jar, etc.) from the local workspace, returning you back to pre-build state. You can use the "Delete workspace" post-build step with "Patterns for files to be deleted" to achieve a similar effect (if you know what you are doing, or to simply remove the workspace entirely.

答案2

得分: -1

你需要安装Workspace Cleanup插件并添加到post部分。

post {
  success {
    archiveArtifacts 'target/*.jar'
    cleanWs()
  }
  failure {
    cleanWs()
  }
}

你可以将cleanWs添加到always块中,但在这种情况下,always始终会首先执行。

英文:

You need to install Workspace Cleanup plugin and add to the post section

post {
  success {
    archiveArtifacts &#39;target/*.jar&#39;
    cleanWs ()
  }
  failure {
    cleanWs ()
  }
}

You can add cleanWs to always block, but in this case always is executing first all the time.

huangapple
  • 本文由 发表于 2020年8月28日 14:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定