如何将API参数传递给GCP云构建触发器 [英] How to pass API parameters to GCP cloud build triggers

查看:56
本文介绍了如何将API参数传递给GCP云构建触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的GCP Cloud构建触发器,可以通过Cloud调度程序调用,并且运行良好. 现在,我想通过外部API调用来调用这些触发器,然后将它们传递给动态参数,这些参数的值和数量会有所不同.

I have a large set of GCP Cloud Build Triggers that I invoke via a Cloud scheduler, all running fine. Now I want to invoke these triggers by an external API call and pass them dynamic parameters that vary in values and number of parameters.

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

I was able to start a trigger by running an API request but any JSON parameters in the API request that I sent were ignored. Google talks about substitution parameters at https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values. I define these variables in the cloudbuild.yaml file, however they were not propagated into my shell script from the API request. I don't any errors with authentication or authorization, so security may not be an issue.

我的想法是否完全受支持,还是我需要求助于另一种解决方案,例如使用带有暴露其API的容器的GKE集群运行(非常繁琐的解决方案).

Is my idea supported at all or do I need to resort to another solution such as running a GKE cluster with containers that would expose its API (a very heavy-boxing solution).

推荐答案

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

We do something similar -- we migrated from Jenkins to GCB but for some people we still need a nicer "UI" to start builds / pass variables.

我从这里获得了脚本,并根据自己的需要对其进行了修改:

I got scripts from here and modified them to our own needs: 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

Here is their REST API: https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.triggers/run

对于以下脚本,请记住,您需要要运行的内容的trigger-id. (您也可以通过解析另一个REST API的输出来获得此信息.)

For the script below, keep in mind you need the trigger-id of what you want to run. (you can also get this by parsing the output of another 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

这篇关于如何将API参数传递给GCP云构建触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆