Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto deploy Google Cloud Functions from Google Cloud Source Control

I can't figure out how to automatically deploy newly pushed commits of Cloud Functions either from Cloud Source Control or from GitHub directly. I have found a similar solution by creating another function and GitHub webhook, but since Functions can't SSH (and have SSH keys installed), it is only working with Public Repos. (https://cloud.google.com/community/tutorials/cloud-functions-github-auto-deployer)

Any ideas how to achieve this? Thanks

like image 991
jirizavadil Avatar asked Dec 04 '17 23:12

jirizavadil


People also ask

Can I run gcloud command on cloud function?

So, bottom-line you can't issue gcloud commands from a Google Cloud Function. To interact with other Google Cloud Platform Services, you have their respective client libraries in many languages, Python being one of them. You might want to take a look at this discussion also.


2 Answers

You could use Google Cloud Builder to achieve this goal. Create a trigger on your repository, and the triggered build deploys the new code to Google Cloud Function.

I made a quick example: https://github.com/Philmod/auto-deploy-gcf

Cheers, Philmod

like image 63
Philmod Avatar answered Sep 30 '22 14:09

Philmod


I managed to find a reasonable workaround for this issue. I hope that it helps others struggling with this same issue.

Required setup

Before you get started with these steps, you'll need to setup SSH keys on your CI/CD system. This is what grants your build system ssh access to your private repo. Here are a couple of articles which discuss how to do that.

  • Gitlab CI 101 - SSH keys
  • An Alternative to npm Private Modules

You'll also need to install the package via git+ssh so that it's included in your package.json (and optionally yarn.lock).

yarn add git+ssh://[email protected]:erichiggins/top_secret.git

At this point, you should see the following entry in your package.json:

...
"dependencies": {
  "top_secret": "git+ssh://[email protected]:erichiggins/top_secret.git"
},
...

Installing the package (twice)

Here are the commands I run inside a shell script on my CI/CD setup, just before the deploy stage, in order to install private repos as packages using git+ssh. I'll use a fake package name of top_secret for my example to make it more clear.

(I'm using yarn and GitLab in this example, but the same applies to npm and GitHub if you prefer.)

yarn install
cd node_modules/top_secret
yarn pack
mv top_secret-v*.tgz ../../
cd ../../
yarn add file:top_secret-v1.0.0.tgz

Note: The yarn pack command will produce a filename that includes a version number, but you can't yarn add with a wildcard (*). I've run into issues using yarn pack --filename with a generic, version-less filename, so you may need to either hardcode this or find a creative solution that uses the filename generated by yarn pack.

Results

If you try running these two commands locally, you'll notice that you end up with just one new entry for top_secret inside the dependencies section of your package.json file, which will look like this:

"top_secret": "file:node_modules/top_secret",

Here's what's happening:

  1. You're installing the private repo as a package via git+ssh on a system that has access.
  2. You're repackaging it from your node_modules/ directory into a tarball file (.tgz).
  3. You're installing the same package from a local tarball file using file:
  4. Because the package name is identical, it replaces the git+ssh entry in package.json with the file: entry.

Your deployment to Cloud Functions should now proceed without any issues, and with your private package included. Good luck and let me know if you have any problems with these instructions -- I'd be happy to correct any mistakes and rewrite anything that is unclear.

Alternative approach

If you don't mind the extra effort or the private repo does not change frequently enough to justify the additional complexity in your CI/CD, you can also use npm pack/yarn pack to create a tarball file, run the same yarn add file:... command listed above to modify your package.json and simply check the tarball file into your repo.

Note: Be mindful that if the repo you're checking the tarball file into is public, the source of your private repo/package will also be made public.

References:

  • Documentation for yarn add
  • Documentation for yarn pack
  • Documentation for npm pack
  • Documentation for npm install
  • Gitlab CI 101 - SSH keys
  • An Alternative to npm Private Modules
like image 24
erichiggins Avatar answered Sep 30 '22 15:09

erichiggins