问题描述
我想做的是这样的:
- 构建应用/包并将其推送到工件
- 部署该工件(不使用 jfrog 管道)
- 部署后向部署的工件添加元数据(自定义属性集),其中包括诸如部署时间、部署失败或成功时部署位置等信息,以及诸如“LatestDeploymentAttempt:true|false”之类的信息
- 构建一个脚本/仪表板来列出所有部署的工件和元数据
所有这些都是可行的,但我有一个关于如何实现 LatestDeploymentAttempt:true
属性的问题
我想要的是一种行为,我可以在其中将属性应用于工件,并立即从该工件的所有其他版本中删除相同的属性。因此,我可以为工件版本添加一个类似 LatestDeploymentAttempt: true 的属性,而 jfrog 会自动从该包的所有其他版本中删除 LatestDeploymentAttempt 属性 - 如果我可以过滤单个属性,这将使列出部署的版本更容易
这可能吗?
如果有一种方法可以使用 REST API 说“从所有版本中删除此属性”,我可以在将其添加到已部署的工件之前手动删除它,但如果 jfrog 具有某种内置功能,那就太好了这样做的方式。
解决方法
您可以通过使用 beforePropertyCreate
或 afterPropertyCreate
事件回调实现 user plugin 来实现这一点。
插件可以使用 Artifactory public API 来搜索已经使用 AQL(Artifctory Query Language)或 by property 标记的工件。
您可以在 JFrog GitHub 用户插件 repository 中找到 Artifactory 用户插件的示例。
以下是此类插件的基本示例:
import org.artifactory.repo.RepoPath
import org.artifactory.search.aql.AqlResult
import org.artifactory.repo.RepoPathFactory
import java.util.Map
storage {
beforePropertyCreate { item,name,values ->
if (name == 'LatestDeploymentAttempt') {
String aqlQuery = 'items.find({"$and" : [{"repo" : "my-repo-local"},{"@LatestDeploymentAttempt" : {"$eq" : "true"}}]})'
searches.aql(aqlQuery) { AqlResult result ->
result.each { Map artifact ->
String itemPath = artifact.path + "/" + artifact.name
RepoPath repoPath = RepoPathFactory.create(artifact.repo,itemPath)
repositories.deleteProperty(repoPath,'LatestDeploymentAttempt')
}
}
}
}
}