Askdesigners
Askdesigners

Reputation: 419

Cloud Function + Cloud Build: How to add a file to function at build time

I have a scenario where I've got a secrets json file in a storage bucket that I would like to cp into the cloud function's dir at build time in Cloud Build.

The cp command works, but the file is neither in the zip that gets deployed or in the code at runtime as the function errors out when called due to missing config values.

Here's the cloudbuild.yaml

steps:
  - name: "gcr.io/cloud-builders/gsutil"
    args: ["cp", "gs://GCP-PROJECT/production.json", "./config/production.json"]
  - name: "gcr.io/cloud-builders/gsutil"
    args: ["cp", "gs://GCP-PROJECT/default.json", "./config/default.json"]
  - name: gcr.io/cloud-builders/gcloud
    args:
      - beta
      - functions
      - deploy
      - --region=europe-west1
      - --memory=128
      - --runtime=nodejs8
      - --trigger-topic=mailsend-sg
      - --stage-bucket=gen-function1-stage
      - --timeout=20s
      - --source=.
      - --entry-point=sendMail
      - send-sendgrid
  - name: gcr.io/cloud-builders/gcloud
    args:
      - beta
      - functions
      - deploy
      - --region=europe-west1
      - --memory=128
      - --runtime=nodejs8
      - --trigger-http
      - --stage-bucket=gen-function2-stage
      - --timeout=20s
      - --source=.
      - --entry-point=makeMail
      - make-fs-mail
timeout: "1600s"

Am I doing something wrong with the localfile path?

Thanks stackoverflow :)

Upvotes: 0

Views: 1239

Answers (1)

Samuel N
Samuel N

Reputation: 623

In your build steps 'args' you put the production.json and default.json in /config folder.

So you need to specify - --source=./config in your build steps instead of - --source=. like so:

steps:
  - name: "gcr.io/cloud-builders/gsutil"
    args: ["cp", "gs://GCP-PROJECT/production.json", "./config/production.json"]
  - name: "gcr.io/cloud-builders/gsutil"
    args: ["cp", "gs://GCP-PROJECT/default.json", "./config/default.json"]
  - name: gcr.io/cloud-builders/gcloud
    args:
      - beta
      - functions
      - deploy
      - --region=europe-west1
      - --memory=128
      - --runtime=nodejs8
      - --trigger-topic=mailsend-sg
      - --stage-bucket=gen-function1-stage
      - --timeout=20s
      - --source=./config
      - --entry-point=sendMail
      - send-sendgrid
  - name: gcr.io/cloud-builders/gcloud
    args:
      - beta
      - functions
      - deploy
      - --region=europe-west1
      - --memory=128
      - --runtime=nodejs8
      - --trigger-http
      - --stage-bucket=gen-function2-stage
      - --timeout=20s
      - --source=./config
      - --entry-point=makeMail
      - make-fs-mail
timeout: "1600s"

That should resolve the Cloud Functions deployment issue. If you still run into errors, post the error/debugging log here

Upvotes: 2

Related Questions