groovy 如何修改artifactlist插件以仅保留repo中的最后10个工件?

kx7yvsdv  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(70)

我发现了artifacttencoder插件(https://github.com/jfrog/artifactory-user-plugins/blob/master/cleanup/artifactCleanup/artifactCleanup.groovy
我设法在我的jenkins服务器中实现了它,但是现在我需要稍微更新一下。
举例来说:

Main_Repo
   -folder1 (has 15 artifacts)
   -folder2 (has 20 artifacts)
   -art1
   -art2
   -...
   -art25

字符串
我需要保留以下内容:

Main_Repo
   -folder1 (has 10 artifacts)
   -folder2 (has 10 artifacts)
   -art1
   -art2
   -...
   -art10


有可能吗?
主要功能是这样的:

private def artifactCleanup(String timeUnit, int timeInterval, String[] repos, log, paceTimeMS, dryRun = false, disablePropertiesSupport = false) {
    log.info "Starting artifact cleanup for repositories $repos, until $timeInterval ${timeUnit}s ago with pacing interval $paceTimeMS ms, dryrun: $dryRun, disablePropertiesSupport: $disablePropertiesSupport"

    cancelOnNullRepos(repos,'repos parameter must be specified before initiating cleanup.')

    // Create Map(repo, paths) of skiped paths (or others properties supported in future ...)
    def skip = [:]
    if ( ! disablePropertiesSupport && repos){
        skip = getSkippedPaths(repos)
    }

    def calendarUntil = Calendar.getInstance()

    calendarUntil.add(mapTimeUnitToCalendar(timeUnit), -timeInterval)

    def calendarUntilFormatted = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(calendarUntil.getTime());
    log.info "Removing all artifacts not downloaded since $calendarUntilFormatted"

    Global.stopCleaning = false
    int cntFoundArtifacts = 0
    int cntNoDeletePermissions = 0
    long bytesFound = 0
    long bytesFoundWithNoDeletePermission = 0
    def artifactsCleanedUp = searches.artifactsNotDownloadedSince(calendarUntil, calendarUntil, repos)
    artifactsCleanedUp.find {
        try {
            while ( Global.pauseCleaning ) {
                log.info "Pausing by request"
                sleep( 60000 )
            }

            if ( Global.stopCleaning ) {
                log.info "Stopping by request, ending loop"
                return true
            }

            if ( ! disablePropertiesSupport && skip[ it.repoKey ] && StringUtils.startsWithAny(it.path, skip[ it.repoKey ])){
                if (log.isDebugEnabled()){
                    log.debug "Skip $it"
                }
                return false
            }

            bytesFound += repositories.getItemInfo(it)?.getSize()
            cntFoundArtifacts++
            if (!security.canDelete(it)) {
                bytesFoundWithNoDeletePermission += repositories.getItemInfo(it)?.getSize()
                cntNoDeletePermissions++
            }
            if (dryRun) {
                log.info "Found $it "
                log.info "\t==> currentUser: ${security.currentUser().getUsername()}"
                log.info "\t==> canDelete: ${security.canDelete(it)}"

            } else {
                if (security.canDelete(it)) {
                    log.info "Deleting $it"
                    repositories.delete it
                    log.info "Deleted..."
                } else {
                    log.info "Can't delete $it (user ${security.currentUser().getUsername()} has no delete permissions), "
                }
            }
        } catch (ItemNotFoundRuntimeException ex) {
            log.info "Failed to find $it, skipping"
        }

        def sleepTime = (Global.paceTimeMS > 0) ? Global.paceTimeMS : paceTimeMS
        if (sleepTime > 0) {
            sleep( sleepTime )
        }

        return false
    }

    if (dryRun) {
        log.info "Dry run - nothing deleted. Found $cntFoundArtifacts artifacts consuming $bytesFound bytes"
        if (cntNoDeletePermissions > 0) {
            log.info "$cntNoDeletePermissions artifacts cannot be deleted due to lack of permissions ($bytesFoundWithNoDeletePermission bytes)"
        }
    } else {
        log.info "Finished cleanup, deleting $cntFoundArtifacts artifacts that took up $bytesFound bytes"
        if (cntNoDeletePermissions > 0) {
            log.info "$cntNoDeletePermissions artifacts could not be deleted due to lack of permissions ($bytesFoundWithNoDeletePermission bytes)"
        }
    }
}


但是我不确定在哪里可以找到例如“search”对象,或者它在下面一行中调用的函数:

def artifactsCleanedUp = searches.artifactsNotDownloadedSince(calendarUntil, calendarUntil, repos)


希望你能帮助我。谢谢你的时间

yzxexxkh

yzxexxkh1#

您的构建工件最好以一种独特的方式进行标记,例如使用Artifactory属性。然后您可以访问具有给定属性的工件。finding artifacts based on property这样,就可以确保您保存以访问正确的工件,而不会有混淆它们的风险。
如果上面提到的是不可能的,我会尝试通过使用时间戳和AQL语句来解决这个问题。你需要获得10的时间戳。构建,你可以考虑将它与Artifactory上的时间戳进行比较。我怀疑这是失败安全的,因为你可以弄乱时间戳。请参阅:Date and Time Format in AQL

相关问题