gpt4 book ai didi

maven - Jenkins 构建参数化,可选择 Nexus Artifact 版本(所有 GAV)

转载 作者:行者123 更新时间:2023-12-01 10:40:09 24 4
gpt4 key购买 nike

是否有 Jenkins 插件可以对我的 Nexus 存储库进行组 Artifact 版本 (GAV) 搜索并列出结果?我希望结果在参数化构建中作为一个选项(下拉列表)可用。

最佳答案

我在 Dynamic Choice Parameter 中添加了一个 groovy 脚本(参见 Jenkins Plugins)

一些障碍是:

  1. 我们的 Nexus 服务器发出了基本的身份验证质询,所以我不能简单地使用 groovy "http://blah ".toURL().text
  2. 我不想加载缺少的 groovy jar,比如 httpbuilder,所以我只是使用 Java URLConnection 类和编码的 user:pass 作为 header 。
  3. 使用 Nexus 的 REST API 获取版本,但必须区分发布和快照。我添加了基于组的身份验证,这样只有开发人员才能访问快照。
  4. GAV 排序不是直截了当的。有一种更好的 GAV 排序方法(使用 org.apache.maven.artifact.versioning.ComparableVersion ),但我还没有实现它,所以现在,我只是简单地排序,让较小的字符串排在第一位。
import hudson.model.*
import jenkins.model.*

def versions=[ ]
def snapshots=[ ]

// The artifactName could be passed in from another parameter (eg. Extended Choice Parameter) linked to this 'dynamic choice' parameter.
def address = "https://xyzcompany.com/nexus/service/local/lucene/search?r=releases&g=com.xyzcompany&a=artifactName&c=features&p=xml"
def urlInfo = address.toURL()

// Consider using tokenstring technique instead of basic auth if pro version of Nexus.
def authString = "user:pass"; // replace 'user' with username, 'pass' with password.
def authStr="Basic " + authString.bytes.encodeBase64().toString()

// Using URLConnection instead of HTTPBuilder et al.
def connection = urlInfo.openConnection()
connection.setRequestProperty( "Authorization" , authStr)

def xml="${connection.content.text}"
def root = new XmlParser().parseText( xml )
root.data.artifact.each {
if (it.artifactHits.artifactHit.repositoryId.text() == "releases")
versions.add("${it.version.text()}");
else
snapshots.add("${it.version.text()}");
}

// There is a better way to GAV sort (using org.apache.maven.artifact.versioning.ComparableVersion) but I have not implemented it yet so for now, I'm simply sorting so the smaller strings line up first.
versions.sort { -it.size() }
Collections.reverse(versions)

// Only certain users should be able to see the SNAPSHOT versions
def userid = User.current().id
def auths = Jenkins.instance.securityRealm.loadUserByUsername(userid).authorities.collect{a -> a.authority}
if (["OffShoreDevGroup", "DevGroup"].intersect(auths))
{
snapshots.sort { -it.size() }
Collections.reverse(snapshots)
versions+=snapshots
}

versions.add(" "); // My build uses a blank version string to simply report what is already deployed to the container.
return versions;

关于maven - Jenkins 构建参数化,可选择 Nexus Artifact 版本(所有 GAV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com