The first two steps of the build file will authenticate your private dependency, and install all dependencies on the project. The third step will call the custom helper function we created above to prepare your .npmrc file. This function takes two arguments,
pathToAuthToken is the left-hand side of the authToken assignment in your
.npmrc file. It should look something like this, replacing
projectId with your own project:
pathToNpmrc would be wherever you’ve saved your
.npmrc file. In this case, the value would look like so:
This build step removes the token value on the file and saves it to a variable, and replaces the
.npmrc file with the environment variable
TOKEN. So, the Cloud Function never stores the actual token in the source code, and the
.npmrc file that is saved locally looks like this:
The last step in the build file redeploys the function, replacing the environment variable in the
.npmrc file with the token value we just created. To run the build steps, you can set up a trigger, or run the following command manually, replacing the variables as we’ve described above:
gcloud builds submit --config=cloudbuild.yaml \ --substitutions=_PATHTOTOKEN="<PATHTOTOKEN>",_PATHTONPMRC="
Before running, make sure you’ve set the appropriate permissions for your Cloud Build function.
That’s all there is to it! Once set up this way, your Google Cloud Function can pull in your private dependency from Artifact Registry without hosting on any external package managers, and without any manual deployment steps.
Automate publishing your private dependency
To speed up the deployment of your local package to Artifact Registry, you can also add a Cloud Build file to your Artifact Registry package that will trigger a publishing event when changes are saved to your package. You can follow the setup steps here, but here is a snippet of a sample
cloudbuild.yaml file that would live in your private dependency: