使用groovy的Jenkins参数

发布于 2025-01-16 03:25:45 字数 950 浏览 3 评论 0原文

我们创建了 jenkins 管道,并尝试借助主动选择参数插件获取特定项目的 GCP 实例映像列表,并在保存作业后使用下面的代码,它应该向我们显示特定项目的 GCP 实例映像列表,但它是保持空白,我们需要此下拉列表在构建管道之前可用。

当运行管道时,我们不会遇到错误,就像通过在 groovy 中传递此处提到的代码一样。

此外,我们还创建了服务帐户来将我们的 GCP 项目连接到 Jenkins。

任何线索,或任何这样做过的人,请告诉我们。

import groovy.json.JsonSlurper
def cmd = "gcloud compute images list --project="xyz" --filter="xyz" | grep 'redhat84-gi'"
def gi_images_json = cmd.execute()
def data = new JsonSlurper().parseText(gi_images_json.text)
def gi_images = [];
return gi_images.sort().reverse

我们将在下面模仿 GCP 的相同操作,但本文档针对的是 AWS。 https://medium.com/@ Different.vinod/access-of-ecr-images-tags-as-active-choice-in-jenkins-for-deployments-8dcdbdbff996

谢谢 Mahendra

我正在尝试在 groovy 的帮助下获取项目中提到的 GCP 实例图像列表,但我没有在下拉列表中获取该列表,它一直显示为空白

We have jenkins pipeline created and we trying to get list of GCP instance images for specific project with the help active choice parameter plugin and using below code after when we save the job it should show us list of gcp instance images for specific project but it is keep coming as blank , we need this drop down to be available as pre post building the pipeline.

We are not facing error when run the pipeline seems like by passing the code mentioned here in groovy.

Also we have created Service account to connect our GCP project to Jenkins.

Any clue , or any has done like this the let us know.

import groovy.json.JsonSlurper
def cmd = "gcloud compute images list --project="xyz" --filter="xyz" | grep 'redhat84-gi'"
def gi_images_json = cmd.execute()
def data = new JsonSlurper().parseText(gi_images_json.text)
def gi_images = [];
return gi_images.sort().reverse

We are following below to mimic the same for GCP but this document says for AWS.
https://medium.com/@different.vinod/access-of-ecr-images-tags-as-active-choice-in-jenkins-for-deployments-8dcdbdbff996

Thanks
Mahendra

I am trying to get list of GCP instance images mentioned in a project with the help of groovy but I am not getting the list in dropdown it is keep coming as blank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-01-23 03:25:45

我在尝试从 (AWS) ECR 存储库获取图像标签时遇到了类似的问题。我没有解释为什么,但 groovy .execute 似乎不喜欢字符串脚本中的 | 。无论我如何尝试逃避它们,我都无法让 grep 或 jq 的管道工作。我决定获取所有图像标签,然后在 groovy .findAll() 功能 如所述此处

另外,我刚刚注意到,但你真的必须更好地转义 cmd 字符串中的引号。正如上面在您的问题中所写的,字符串是“gcloudcomputeimageslist--project=”,然后是一堆乱码的文本。那肯定行不通。尝试:

def cmd = "gcloud compute images list --project=\"xyz\" --filter=\"xyz\" | grep 'redhat84-gi'"

或者完全将它们排除在外。在这种特殊情况下,实际上并不需要它们,因为它们不会将包含空格的复杂字符串组合在一起。没有它们,写作会容易得多。就像这样:

def cmd = "gcloud compute images list --project=xyz --filter=xyz | grep redhat84-gi"

I have had a similar problem trying to get image tags from (AWS) ECR repositories. I don't have an explanation for why but groovy .execute doesn't seem to like |s in the string scripts. No matter how I tried to escape them I couldn't get a pipe to grep or jq to work. I settled for getting all the image tags and then parsing them with regex in a groovy .findAll() function as described here.

Also, I only just noticed but you would really have to escape your quotes in the cmd string better. As written above in your question the string is "gcloud compute images list --project=" and then a garbled mess of text after it. That is definitely not going to work. Try:

def cmd = "gcloud compute images list --project=\"xyz\" --filter=\"xyz\" | grep 'redhat84-gi'"

Or leave them out altogether. They are not really needed in this particular circumstance because they aren't holding together complex strings which include white space. It's much easier to write without them. Like so:

def cmd = "gcloud compute images list --project=xyz --filter=xyz | grep redhat84-gi"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文