3

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.

Raghunath
  • 594
  • 4
  • 12
  • 27
  • You may want to cosider using Cloud Build GitHub app [1], as this will allow you to automatically build your code each time you push a new commit to GitHub. [1] https://cloud.google.com/cloud-build/docs/automating-builds/run-builds-on-github – A.Amiri Jul 10 '20 at 15:26
  • I have a similar question. Your problem is your only solving half of the issue. In addition to GitHub giving access to GCP the individual project needs to be connected. I have been unable to find any documentation from Google on a CLI/API that will do this. https://stackoverflow.com/questions/69834735/programmatically-connecting-a-github-repo-to-a-google-cloud-project – iicaptain Nov 05 '21 at 16:37

0 Answers0