1

I want to create a Google Cloud Function using Terraform but want to pull the source code from Github.

I managed to do this zipping up the function and copying it into Cloud Storage using Terraform, but I do not like this workflow as I have to run a script to kick things off. I rather just do a PR on Github and see the new code in GCP.

I already setup Google Cloud Source Repositories to source from my Github.

The Terraform doc to use the "source_repository" argument is not clear to me. What I would like to do is just grab the source from HEAD on the master branch.

This function source code is located under the sub-folder:

cloud-functions/training_data

There are two files inside the function folder:

  • “main.py”
  • “requirements.txt”

I just would like to know how to specify the “source_repository” argument in this case.

4
  • Can you share the Terraform code with what you've tried so far? Commented Jun 21, 2019 at 14:40
  • Unfortunately I can't, working for Big Corp. I just need to understand the format of "source_repository". Commented Jun 21, 2019 at 15:15
  • 2
    Can you produce a minimal reproducible example that doesn't include anything proprietary? Commented Jun 21, 2019 at 15:26
  • fine any solution? Commented Aug 4, 2020 at 5:24

2 Answers 2

1

I recently came across this problem as well.

The documentation says to format the url value to be, https://source.developers.google.com/projects/*/repos/*/moveable-aliases/*/paths/*

To get this to work...hypothetically, say my source code and my repository had the following information...

project: kalefive-project
repo: kalefive-functions-repository
branch: master
directory_in_repo_with_src: src/functions/bin

Then the resulting url that worked for me was...

source_repository = {
  url = https://source.developers.google.com/projects/kalefive-project/repos/kalefive-functions-repository/moveable-aliases/master/paths/src/functions/bin
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

that's exactly what I figure out and got it working! The docs could be better written imho.
This works. Make sure that the service account that terraform uses, has read access to the source repo.
@lilline service account has access to read/write on source repo but still not able to deploy the code. The function is being created perhaps . Error waiting for Creating CloudFunctions Function: Error code 13, message: Failed to retrieve function source code
0
source_repository = {
  url = https://source.developers.google.com/projects/kalefive-project/repos/kalefive-functions-repository/moveable-aliases/master/paths/src/functions/bin
}

incase the above syntax is not working just use it without an equal sign.

resource "google_cloudfunctions_function" "js_function" {
    source_repository  {
      url = https://source.developers.google.com/projects/kalefive-project/repos/kalefive-functions-repository/moveable-aliases/master/paths/src/functions/bin
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.