I have written Python Script to create a Trigger in Google Cloud Build. So, I'm able to create it but, for Type it is setting to "None (no builds will be triggered)" and Filter "--"
Python Script
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)
payload
{"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
I have to manually go to the Cloud Build trigger console and set it there. Can anyone suggest me how to set TYPE and FILTER through the REST API?
Thanks in Advance
Notice that the feature is still in Beta and might change or have limited support. Add the triggerTemplate field and modify your payload accordingly.
For example if you want to build a trigger on your Google Cloud Platform's project default repository you could do the following changes to your script:
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)