Google Cloud build always deploys my cloud-function with previous commit
Asked Answered
E

2

7

I have a problem with my automated cloud function deployment

I have a cloud function stored in a Google Cloud repository

Git code includes a cloudbuild.yaml file with this content :

steps:
- name: "gcr.io/cloud-builders/gcloud"
  args: ["functions", "deploy", "myfunction", "--region=europe-west1"]
timeout: "1600s"

I only have a branch Master.

When i push my commit, cloudbuild triggers and deploys the cloud function

The problem is that it always deploys the previous commit, not the last

For example : 2:23 : I push my source code to Google Source repository

Here is the result :

Commit from local source

At 2:23:33, cloudbuild triggers and deploys successfully the cloud function

enter image description here

Here is the log of Cloudbuild :

starting build "e3a0e735-50fc-4315-bafd-03128156d69f"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/myproject/r/myrepo
 * branch            1b67729b8498c35fc19a45b14b8d674635300594 -> FETCH_HEAD
HEAD is now at 1b67729 PrayingforCommit
BUILD
Already have image (with digest): gcr.io/cloud-builders/gcloud
Deploying function (may take a while - up to 2 minutes)...
...............................................done.
availableMemoryMb: 256
entryPoint: process_gcs
eventTrigger:
  eventType: google.storage.object.finalize
  failurePolicy: {}
  resource: projects/_/buckets/mybucket
  service: storage.googleapis.com
ingressSettings: ALLOW_ALL
labels:
  deployment-tool: cli-gcloud
name: projects/myproject/locations/europe-west1/functions/myfunction
runtime: python37
serviceAccountEmail: [email protected]
sourceRepository:
  deployedUrl: https://source.developers.google.com/projects/myproject/repos/myrepo/revisions/2ed14c3225e7fcc089f2bc6a0ae29c7564ec12b9/paths/
  url: https://source.developers.google.com/projects/myproject/repos/myrepo/moveable-aliases/master/paths/
status: ACTIVE
timeout: 60s
updateTime: '2020-04-15T00:24:55.184Z'
versionId: '2'
PUSH
DONE

As you can see, the commit that triggers is the 1b67729, but the DeployedUrl line says 2ed14c3 which is the previous commit

Operation ended at 2:24:55, i see the same time in my cloud function source tab

enter image description here

If i just click the edit button, then deploy button, to manually force the cloud function rebuild, it deploys the correct commit (1b67729)

enter image description here

Here are the parameters of the cloud-function :

enter image description here

Where is my mistake with cloudbuild, and how to always deploy the last commit ???

Thanks for your help

Eastnortheast answered 15/4, 2020 at 1:18 Comment(5)
Are you facing this issue recently? Could you please share when you first start encountering this issue? Before is it working correctly?Shakiashaking
Hi Nibrass, thanks for your interest I have several projects using cloudbuild Regarding to the build history, the last correct deployment was February, 16th From ... * branch 35ac17fa2360c0c409f83447842054b33763f49c -> FETCH_HEAD HEAD is now at 35ac17f typo BUILD [...] deployedUrl: .../revisions/35ac17fa2360c0c409f83447842054b33763f49c/paths/ [...] PUSH DONEEastnortheast
Found a workaround - Drop both trigger and repository - Modify the cloudbuild (setting the new repository for cloudfunction with --source parameter) - Recreate the trigger - Push my local git The question remains open, i have no idea of why cloudbuild had this strange behavior with the old repositoryEastnortheast
Thanks for your answer. Were you able to create a new repository with the old cloudbuild file and deploy. Does it also deploy the last commit? Or just it's happening with your old repository?Shakiashaking
I have the same issue, no work-around found yet. I enlisted a 2nd pair of eyes for hours, and we found nothing. I don't know how to trace the cause. In my case though, I have the issue with 2 repos that I deploy cloud functions from, whereas another 4 (similar) repos that I deploy cloud functions from in the same way are just fine. All done from Terraform.Incertitude
P
3

Cloud Functions does not check for updates to source repos when the --source flag is omitted

The function was previously deployed directly from a source repository and you are not passing a --source flag to gcloud. Under these circumstances gcloud ignores the code in the local directory.

The easiest fix is to explicitly specify the source in your cloudbuild.yaml:

steps:
- name: "gcr.io/cloud-builders/gcloud"
  args: ["functions", "deploy", "myfunction", "--region=europe-west1", "--source=."]
timeout: "1600s"

(You would not hit this if the function had never been configured to fetch its source directly from the repository.)

Explanation

1. The Cloud Function has previously been deployed from a source repository

When configuring a Cloud Function, there is an option to fetch source code from a Cloud Source Repository. You are prompted with this as an option when creating a function through the console, but could also have used gcloud:

gcloud functions deploy NAME \
    --source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/master/paths/SOURCE \
    [... other gcloud options ...]

This is the 'sourceRepository' setting that you can see in the build output.

However, as you can see in the console (resolved to 1b67729b) Cloud Functions is not updating its code from there.

2. Omitting the --source flag to gcloud in these circumstances leads to potentially confusing behaviour

When you leave out the --source flag to gcloud, if the function was previously deployed directly from a repository, specific behaviour applies. From the documentation for gcloud functions deploy:

If you do not specify the --source flag:

  • The current directory will be used for new function deployments.
  • If the function was previously deployed using a local filesystem path, then the function's source code will be updated using the current directory.
  • If the function was previously deployed using a Google Cloud Storage location or a source repository, then the function's source code will not be updated.

You are hitting the third option.

Conclusion: Avoid linking Cloud Functions directly to the source repository when deploying through Cloud Build

Cloud Build is configured to run gcloud in a directory containing a copy of your source code. You can therefore deploy directly from the local filesystem - this packages up the function in a zip file, uploads it to Cloud Storage, and tells Cloud Functions to fetch it.

If you tell Cloud Functions to fetch its code from a repository, then a second checkout of the repo is made and a zip file created from there, which will be slightly slower (and potentially prone to a race condition if the branch has updated in the background).

Peppergrass answered 6/6, 2021 at 17:38 Comment(0)
C
0

The repository wasn't synced in my case - in the build log it kept showing the HEAD was from a build weeks ago

went to: https://source.cloud.google.com/ re-synced and my branches updated

Carola answered 15/1 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.