如何在云构建中将触发器类型作为有效负载传递给“方法:projects.triggers.create”
How to pass Trigger type as a payload to “Method: projects.triggers.create " in cloud build
我已经编写了 Python 脚本来在 Google Cloud Build 中创建触发器。所以,我能够创建它,但是对于类型,它设置为 "None (no builds will be triggered)" 和过滤器“--”
Python 脚本
bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print(error)
headers = {
'Authorization' : 'Bearer '+str(output)[2:-3],
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
cloudbuild = {"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
data = json.dumps(cloudbuild)
response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers', headers=headers, data=data)
results_output = response.json()
pprint(results_output)
有效负载
{"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
我必须手动转到 Cloud Build 触发器控制台并将其设置在那里。
谁能建议我如何设置 TYPE 和 FILTER通过 REST API?
提前致谢
注意 feature is still in Beta and might change or have limited support. Add the triggerTemplate 字段并相应地修改您的负载。
例如,如果您想在 Google Cloud Platform 的项目默认存储库上构建触发器,您可以对脚本进行以下更改:
import subprocess
import json
import requests
bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print(error)
headers = {
'Authorization' : 'Bearer '+str(output)[2:-3],
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
cloudbuild = {
"name": "[TRIGGER NAME]",
"description": "[TRIGGER DESCRIPTION]",
"triggerTemplate": {
"projectId": "[PROJECT_ID]",
"repoName": "gcr.io/[PROJECT_ID]/default",
"branchName": ".*"
},
"build": {
"source": {
"repoSource": {
"projectId": "[PROJECT_ID]",
"repoName": "gcr.io/[PROJECT_ID]/default",
"branchName": ".*"
}
}
}
}
data = json.dumps(cloudbuild)
response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers', headers=headers, data=data)
results_output = response.json()
print(results_output)
我已经编写了 Python 脚本来在 Google Cloud Build 中创建触发器。所以,我能够创建它,但是对于类型,它设置为 "None (no builds will be triggered)" 和过滤器“--”
Python 脚本
bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print(error)
headers = {
'Authorization' : 'Bearer '+str(output)[2:-3],
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
cloudbuild = {"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
data = json.dumps(cloudbuild)
response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers', headers=headers, data=data)
results_output = response.json()
pprint(results_output)
有效负载
{"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
我必须手动转到 Cloud Build 触发器控制台并将其设置在那里。 谁能建议我如何设置 TYPE 和 FILTER通过 REST API?
提前致谢
注意 feature is still in Beta and might change or have limited support. Add the triggerTemplate 字段并相应地修改您的负载。
例如,如果您想在 Google Cloud Platform 的项目默认存储库上构建触发器,您可以对脚本进行以下更改:
import subprocess
import json
import requests
bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print(error)
headers = {
'Authorization' : 'Bearer '+str(output)[2:-3],
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
cloudbuild = {
"name": "[TRIGGER NAME]",
"description": "[TRIGGER DESCRIPTION]",
"triggerTemplate": {
"projectId": "[PROJECT_ID]",
"repoName": "gcr.io/[PROJECT_ID]/default",
"branchName": ".*"
},
"build": {
"source": {
"repoSource": {
"projectId": "[PROJECT_ID]",
"repoName": "gcr.io/[PROJECT_ID]/default",
"branchName": ".*"
}
}
}
}
data = json.dumps(cloudbuild)
response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers', headers=headers, data=data)
results_output = response.json()
print(results_output)