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 solu
I managed to find a reasonable workaround for this issue. I hope that it helps others struggling with this same issue.
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.
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"
},
...
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
.
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:
git+ssh
on a system that has access.node_modules/
directory into a tarball file (.tgz).file:
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.
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.
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