rest - 如何将 API 参数传递给 GCP 云构建触发器

标签 rest google-cloud-platform google-cloud-build build-triggers

我有一大组 GCP 云构建触发器,我通过云调度程序调用它们,所有这些都运行良好。 现在我想通过外部 API 调用来调用这些触发器,并向它们传递值和参数数量各不相同的动态参数。

我能够通过运行 API 请求来启动触发器,但我发送的 API 请求中的任何 JSON 参数都被忽略了。 Google 在 https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values 谈论替代参数.我在 cloudbuild.yaml 文件中定义了这些变量,但它们并未从 API 请求传播到我的 shell 脚本中。 我没有任何身份验证或授权错误,因此安全性可能不是问题。

我的想法是否完全得到支持,或者我是否需要求助于其他解决方案,例如使用会公开其 API 的容器运行 GKE 集群(一种非常繁重的解决方案)。

最佳答案

我们做了类似的事情——我们从 Jenkins 迁移到 GCB,但对于某些人来说,我们仍然需要一个更好的“UI”来开始构建/传递变量。

我从这里得到脚本并根据我们自己的需要修改它们:https://medium.com/@nieldw/put-your-build-triggers-into-source-control-with-the-cloud-build-api-ed0c18d6fcac

这是他们的 REST API:https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.triggers/run

对于下面的脚本,请记住您需要要运行的内容的触发器 ID。 (您也可以通过解析另一个 REST API 的输出来获得它。)

TRIGGER_ID=1
# we need to specify ATLEAST the branch name or commit id (check after)
BRANCH_OR_SHA=$2

# check if branch_name or commit_sha
if [[ $BRANCH_OR_SHA =~ [0-9a-f]{5,40} ]]; then
    # is COMMIT_HASH
    COMMIT_SHA=$BRANCH_OR_SHA
    BRANCH_OR_SHA="\"commitSha\": \"$COMMIT_SHA\""
else
    # is BRANCH_NAME
    BRANCH_OR_SHA="\"branchName\": \"$BRANCH_OR_SHA\""
fi

# This is the request we send to google so it knows what to build
# Here we're overriding some variables that we have already set in the default 'cloudbuild.yaml' file of the repo
cat <<EOF > request.json
{
  "projectId": "$PROJECT_ID",
  $BRANCH_OR_SHA,
  "substitutions": {
    "_MY_VAR_1": "my_value",
    "_MY_VAR_2": "my_value_2"
   }
}
EOF

# our curl post, we send 'request.json' with info, add our Token, and set the trigger_id
curl -X POST -T request.json -H "Authorization: Bearer $(gcloud config config-helper \
    --format='value(credential.access_token)')" \
        https://cloudbuild.googleapis.com/v1/projects/"$PROJECT_ID"/triggers/"$TRIGGER_ID":run

关于rest - 如何将 API 参数传递给 GCP 云构建触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804109/

相关文章:

c# - 将 Web API 添加到现有 MVC Web 应用程序后出现 404 错误

scala - Play 应用程序中的全局异常处理

docker - 在带有jenkins的kubernetes实例上使用docker-compose-安装空卷

node.js - 在 Node.js 中使用 Cloud Spanner 插入查询

git - 将 git 命令与 Google Cloud Builder 结合使用

python - Google 云函数 : CI/CD for Python 3. 7 运行时

rest - 如何使用 Dart 创建 Http 响应

wcf - Excel-VBA REST WCF 在第一次调用时工作,但后续调用返回缓存的(非当前)数据

docker - 为什么我无法使用 Google Cloud Build 提取 Google ArtifactRegistry Docker 镜像?

amazon-web-services - Terraform main.tf 为不同的名称约定