Connect to Enterprise GitHub from Terraform cloud function - github

I am trying to create a Google Cloud function through Terraform. The source code for the function is in Enterprise GitHub. https://github.xyz.com/cf
The Terraform code is as below:
resource "google_cloudfunctions_function" "cfcluster" {
name = "cfcluster1"
project = "${var.project_id}"
region = "us-central1"
runtime = "python39"
source_repository {
//url="https://github.xyz.com/cf" #is this possible?
}
Is it possible to connect to Enterprise GitHub from Google CloudFunction in Terraform? How can I achieve it?
Note
I don't want to connect to cloud-source repository from Terraform.

I don't think you can pull data from a GitHub repository directly. What you can do is to mirror it to your project's Cloud Repository, and then you can use the data within the Google Cloud Platform as you wish. Here you can find a document on how to mirror your GitHub Repository [1].
You can also take a look at this tutorial [2], here you can find the complete steps to deploy an application in Google Cloud Platform from a Github Repository.
[1] https://cloud.google.com/source-repositories/docs/mirroring-a-github-repository
[2] https://medium.com/swlh/deploying-github-repository-to-google-cloud-platform-997d296547e6

Related

Programmatically Connecting a GitHub repo to a Google Cloud Project

I'm working on a Terraform project that will set up all the GCP resources needed for a large project spanning multiple GitHub repos. My goal is to be able to recreate the cloud infrastructure from scratch completely with Terraform.
The issue I'm running into is in order to setup build triggers with Terraform within GCP, the GitHub repo that is setting off the trigger first needs to be connected. Currently, I've only been able to do that manually via the Google Cloud Build dashboard. I'm not sure if this is possible via Terraform or with a script but I'm looking for any solution I can automate this with. Once the projects are connected updating everything with Terraform is working fine.
TLDR; How can I programmatically connect a GitHub project with a GCP project instead of using the dashboard?
Currently there is no way to programmatically connect a GitHub repo to a Google Cloud Project. This must be done manually via Google Cloud.
My workaround is to manually connect an "admin" project, build containers and save them to that project's artifact registry, and then deploy the containers from the registry in the programmatically generated project.

Automate mirroring GitHub to GCP Source Repository?

We run Google Cloud Functions (python), which require to be deployed from Google Cloud Source Repository. Since all the code is stored on GitHub we resort to first mirroring GitHub into Source Repository. Although this only requires a few mouse clicks, it becomes a burden to repeat over 3+ projects (dev, staging, production) times 5+ repos (5+ apps).
I am looking to automate the mirroring config, preferably to add into the Terraform automation we already use, into a hands-off project configuration. Does the Google API support this mirroring automation? So far on my Google Cloud expedition everything was available in their API!
I fail to find Terraform examples though, and would appreciate a tip.
Come to think of it, if I can take Source Repository out of the equation, that would be just fine with me too. After all, I only use it as a pass-through / empty shell.
The Cloud Source Repository API includes a Repo resource that has a Mirror Config object where you could type in your Github's URL, webhook and credentials to automate this procedure. I would initially test it with the create method, but if you have an existing Cloud Source Repository I believe the patch method will also be worth exploring.
Additionally, there is an open Feature Request in order to connect a repository via the Cloud Build GitHub App that I recommend you to star and follow, as it could further ease your automation needs.

How to make my Github master branch the only branch to be authorised to access Production resources on GCP?

I use Github for version control and Google Cloud Platform to orchestrate my resources and store data. I use Kubernetes (google cloud platform) and Jenkins to execute my scripts. BigQuery, Google Cloud Storage (buckets), Cloud MySQL to store my data.
I have a master branch which has production codes and development branch which has development codes. Is there a way how I can restrict only master branch codes to have write access to Production resources on Google Cloud Platform.

Github webhook is not created when creating a Google Cloud Build trigger

I have many projects which uses Google Cloud Build + Github build pipeline setup. However, there is this one project, which I cannot create a webhook in Github for.
It used to work - but commits to the repository doesn't trigger the build process any more. I deleted the trigger and added it again - but the webhook in Github is not created automatically for this project.
When I run the trigger manually, it picks the wrong, but fixed commit which I did before an year.
Any clue?
Could you try delete a repository on Cloud Source Repositories and setup Google Cloud Build again ?
See:
https://cloud.google.com/cloud-build/docs/running-builds/automate-builds
Note: For external repositories, such as GitHub and Bitbucket, you must have owner-level permissions for the Cloud Platform project with which you're working. When you set up a build trigger with an external repository for the first time, you'll need to set up authorization with that repository.
After you've set up your external repository, Cloud Source Repository creates a mirror of your repository.
https://source.cloud.google.com
https://cloud.google.com/source-repositories/docs/deleting-a-repository
https://cloud.google.com/source-repositories/docs/mirroring-a-github-repository
I am experiencing the same issue. I can create a trigger for a repo, but I cannot connect the repo automatically to cloud build. We also have many projects, and this manual labor is sort of annoying.
Is there any (under the hood) github/gcloud api available in which I can connect a github repo to cloud build? I am aware that this can only be done by someone with admin privileges on a repo or organization in github.
After this, I will be able to run the command gcloud build triggers create github [NAME]

Does Google Cloup Platform have a Github like repository

I am very new to both GCP and github.
For an application running on GCP, I want to push my code from my local machine to GCP. I know that GCP is very well connected with Github.
However I am wondering if GCP has a service similar to github for hosting private repositories? If so, where can I find more info about it.
If not, is there a way to avoid a separate paid monthly subscription?
Yes, here is the documentation : https://cloud.google.com/source-repositories/