我们做了类似的事情——我们从 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