Google Cloud Build 的 POST 正文 - Webhook 触发器

问题描述 投票:0回答:2

Google Cloud Build - Webhook 触发器创建触发器文档显示了 POST 调用构建触发器的正确 URL。然而,文档没有描述 POST 主体,这似乎是必需的。我已经使用

content-type: application/json
标头和 POST 正文
{}
成功触发了云构建 Webhooks,但很高兴知道:

  • POST 正文应该是什么?
  • 我们能够在 POST 正文中传递替换变量吗?

Google Cloud Build - REST API 文档提供了一些额外的提示,表明 HttpBody 负载已被接受,但据我所知,除此之外没有其他信息。

webhooks google-cloud-build
2个回答
4
投票

身体就是你想要的!事实上,在触发器中,您可以像这样自定义替换变量(来自文档)

 --subtitutions=\
         _SUB_ONE='$(body.message.test)', _SUB_TWO='$(body.message.output)'

所以,你的身体需要是这样的

{
  "message": {
    "test": "test value",
    "ourput": "my output"
  }
}

数据会自动从您的身体内容中提取。因此,您可以添加更多替换或更改 JSON 的格式,从而更改替换值。


0
投票

以下是 GCP Console 中的示例:

enter image description here

我使用 Node.js 和 Cloud Build API 从 Cloud Function 调用 Cloud Build 触发器:

const fetch = require('node-fetch');
...
const webhookUrl = `https://cloudbuild.googleapis.com/v1/projects/${projectId}` +
        `/locations/europe-west2/triggers/hunter-${action}:webhook` +
        `?key=${encodeURIComponent(apiKey)}` +
        `&secret=${encodeURIComponent(webhookSecret)}` +
        `&trigger=hunter-${action}&projectId=${projectId}`;

    const body = {
        message: {
            'city': city,
        },
    };


    const response = await fetch(webhookUrl, {
        method: 'POST',
        body: JSON.stringify(body),
    });
© www.soinside.com 2019 - 2024. All rights reserved.