Auto deploy Google Cloud Functions from Google Cloud Source Control

亡梦爱人 提交于 2019-11-30 16:14:37

问题


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


回答1:


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




回答2:


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://git@gitlab.com:erichiggins/top_secret.git

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

...
"dependencies": {
  "top_secret": "git+ssh://git@gitlab.com: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


来源:https://stackoverflow.com/questions/47643386/auto-deploy-google-cloud-functions-from-google-cloud-source-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!