11

I accidentally lost my up to date source file of a cloud function and then deployed again the cloud function with an old version of it.

Each time I deploy a cloud function I see on the console the version id of the deploy.

Is there something I can do to roll back to the previous version of the source file?

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Ariel
  • 821
  • 2
  • 9
  • 19
  • You might be able to retrieve it with the approach shown here: https://stackoverflow.com/questions/43916490/get-code-from-firebase-console-which-i-deployed-earlier – Frank van Puffelen Oct 18 '17 at 03:40
  • Possible duplicate of [Get code from firebase console which I deployed earlier](https://stackoverflow.com/questions/43916490/get-code-from-firebase-console-which-i-deployed-earlier) – Frank van Puffelen Oct 18 '17 at 03:40
  • I know the code exists on the web console, but because i accidently deployed an old version of the function i first must roll back to the previous version of the deploy i have done, is such thing possible to do? if not then why do they keep the version count? – Ariel Oct 18 '17 at 07:13

4 Answers4

17

I finally figured this out:

Each time you make a deploy to a cloud function you get an output line like this:

sourceArchiveUrl: gs://my-store-bucket/us-central1-function_name-xxoxtdxvxaxx.zip

I entered my Google Cloud Platform Developer Console -> Cloud Functions -> function_name -> Source tab

and there almost at the bottom it says: Source location

my-store-bucket/us-central1-function_name-xxoxtdxvxaxx.zip

the same as it was shown in the CLI, but without gs:// that link lead me to the following: https://storage.cloud.google.com/my-store-bucket/us-central1-function_name-........

I removed from the link everything that came after

https://storage.cloud.google.com/my-store-bucket

and that lead me to a huge list of files that each one of them represented a an image of all my cloud functions at the time point of each time i have made a deploy, exactly what i needed!

The only thing left to do was to locate the file with the last date before my mistaken deploy

Ariel
  • 821
  • 2
  • 9
  • 19
  • I don;t see it now – Roshan Fernando Mar 01 '19 at 17:55
  • 8
    It changed to a Download the source: "Download zip" button, and if you right-click on the button and modify the URL, you can edit the `functionVersion` parameter. – Daniel Sparing Mar 19 '19 at 13:16
  • As of June 2021 Google seems to have changed how they do this. I am able to go back one version via the `functionVersion` param, but no versions prior to that. – Brian Moeskau Jun 23 '21 at 20:01
  • I think I found my way to the page described by @Ariel, but the files available for download are image files, and I don't know what to do with them after I've downloaded them! – Philip Stratford Mar 16 '22 at 18:28
3

When you deploy a new version of a CF the code is store in a GCS bucket.This bucket only stores the last 2 deployed versions. You can set a retention policy to keep older versions of the CF.

The name of the bucket it's similar to something like this:

gcf-source-[ID]-[region of the function]

A workaround to restore old version of a CF would be to download the code from the bucket and redeploy the function with it.

drauedo
  • 641
  • 4
  • 17
1

The following shows where to locate and restore a previous deployment version of your Google Cloud Function.

You can select each of your previous versions on the scrolldown menu at the top left of your GCF screen once you enter a function.

On this overview page, while NOT entering EDIT mode, you can select some previous versions (although not all) from the menu - the selected version is then deployed (here: Version 5).

enter image description here

Agile Bean
  • 6,437
  • 1
  • 45
  • 53
1

I just landed here in 2022 with very much the same problem and whilst the answers here pointed me in the right direction, this is how I retrieved the code I'd written in the web UI's Cloud Functions editor and subsequently lost:

  1. Go to https://console.cloud.google.com/home/dashboard and select the relevant project from the dropdown list in the title bar in the top-left corner of the page.
  2. In the "Resources" card, click the "Cloud Functions" link.Screenshot of Cloud Platform Dashboard
  3. The Cloud Functions page loads, listing the last few versions of the deployed (in my case only to Test) code. Click on the entry with the most recent "Last Deployed" date (or an earlier one, if you need).
  4. The Function Details page loads, initially showing charts relating to the function's activities. Click the "Source" tab near the top of the page.Screenshot of the Function details page
  5. You should now see your code files listed down the left-hand side of the page, with the code contained in each shown in the main panel when a file is selected. From here you can copy the code you've lost, or just click the "Download ZIP" button in the top-right corner to download it all.

Incidentally, the way I lost my code is hilarious (now that I've got it back). I made a small typo and instinctively hit Ctrl + Z to undo it. Bizarrely, this key combination, known the world over as the shortcut for "Undo", in Google's Cloud Functions editor instead has the undocumented meaning "Delete everything". Try it. It literally removes all of the code in the active document. And since there's no way to "Close without saving", and navigating away from the editor triggers an automatic save, I'd quickly saved over my afternoon's work (which I assumed - rightly, in a way, was being version-controlled) with a blank version of the file.

Philip Stratford
  • 4,513
  • 4
  • 45
  • 71