0

I always have to save code at least in clipboard and sometimes make a screenshot or some other backup of the settings before I deploy a Google Cloud Function, which seems strange to me, perhaps there is a temporary Google backup of it somewhere? If a deployment fails, the Cloud Function is rolled back to the version before deployment (here: version 85) and recent changes that I tried to deploy got lost.

enter image description here

enter image description here

That means, in a Python Cloud Function, I have lost my most recent code in the "main.py", any changes in the "requirements.txt" and even any changes in the environment variables.

Is there a way to go back to the version of my tried deployment and work further from there again?

4 Answers 4

1

Code easily recoverable

Source code is still in the cloud function's source bucket. Check the build log for `Step #0 - "fetch": Fetching archive ..."

The archive path will match:

  • gs://bucket-id/function-folder/version/function-source.zip

Navigate to https://console.cloud.google.com/storage/browser/`bucket-id`/`function-folder`/

There you'll find two versions. One is the most recent build source. The other is the most recent sucessful build.

1

You can recover lost code when a Cloud Function deployment fails. But it is a bit cumbersome to do so.

In case anyone needs it, you can recover it by finding the image that was built, deploy it on a VM on GCE, Then extract the source code and download it using ssh.

  1. Find your image. Container images for GCF are stored in your container registry. Depending on your project and region it can for example be in: gcr.io -> PROJECT -> gcf -> REGION. Then find the folder holding your CF code. I found this path using the build log in case you have trouble finding the correct folder.

  2. Deploy your image. When you find your image you can simple select deploy in the navbar and deploy it to a new VM in GCE.

  3. Access the VM. When deployed simply SSH into the VM.

Since the deployment of the CF function failed, the image will also fail and be in restart mode all the time when you have accessed the VM. But you can still copy the source code and download it:

  1. Create a new image: docker create --name="my_tmp_image" your_image:tag
  2. Then copy the code to a .tar file: docker export my_tmp_image > img.tar
  3. Download the file using the download button in the open SSH window.

Once downloaded, you will find your source code in the workspace folder.

And don't forget to kill the VM so you don't waste money on it.

1

Took my longer than I would have liked to realize, but function source code is stored in a GCS bucket, even for failed deployments, so it can be retrieved from there.

1
  • Could be, good point, sound likely, I cannot test it since I do not work on that platform anymore. I will accept if someone else checks it or if you show some screenshots or the like. Commented Jul 31, 2023 at 16:01
1

(This later turns out wrong, there is a way to recover the last run code, see the other answers)

There is no way to recover that failed deployment code.

Yet, there is an easy workaround to never lose your code of the deployment time again: just deploy with gcloud function from the shell instead of using the online deployment! Then, you can just change the code as you like, save, and deploy, so that you will not lose it even if the deployment fails.

If you really want to roll back to the last running deployment, you can find that online.

This is best practice. Even if you may think setting up gcloud with the local environment variables and code costs some time at first, you will not need to save the code that you deploy before clicking the Test the function button since you will not use that button anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.