I am working on GitHub and Google Cloud Build integration for CI/CD.
I am able to link repository to Google Cloud build installation in GitHub using below code.
import requests
header = dict()
header['Authorization'] = 'token %s' % personal_token
header['Accept'] = 'application/vnd.github.machine-man-preview+json'
# Fetching repository id
url = f'https://api.github.com/repos/{organization_name}/{repo_name}'
output = requests.get(url, headers=header)
repo_id = output.json()['id']
# Adding repository to Google Cloud build installation
url = f'https://api.github.com/user/installations/{gcb_installation_id}/repositories/{repo_id}'
output = requests.put(url, headers=header)
When I try to create trigger with below code, I am getting an error google.api_core.exceptions.FailedPrecondition: 400 Repository mapping does not exist
from google.cloud.devtools import cloudbuild_v1
client = cloudbuild_v1.CloudBuildClient()
build_trigger_template = cloudbuild_v1.types.BuildTrigger()
build_trigger_template.description = 'test to create trigger'
build_trigger_template.name = 'github-cloudbuild-trigger1'
build_trigger_template.github.name = 'github-cloudbuild'
build_trigger_template.github.pull_request.branch = 'master'
build_trigger_template.filename = 'cloudbuild.yaml'
response = client.create_build_trigger('dev',
build_trigger_template)
Can anyone please help me on how to connect (map) a GitHub repository to Google Cloud build project using python or any other automated process. I am able to manually map repository but need to automate.
Thanks, Raghunath.