Answers: <h1>Code easily recoverable</h1>
  <p>Source code is still in the cloud function's source bucket.
  Check the build log for `Step #0 - &quot;fetch&quot;: Fetching archive ...&quot;</p>
  <p>The archive path will match:</p>
  <ul>
  <li>gs://<code>bucket-id</code>/<code>function-folder</code>/<code>version</code>/function-source.zip</li>
  </ul>
  <p>Navigate to <a href="https://console.cloud.google.com/storage/browser/%60bucket-id%60/%60function-folder%60/" rel="nofollow noreferrer">https://console.cloud.google.com/storage/browser/`bucket-id`/`function-folder`/</a></p>
  <p>There you'll find two versions. One is the most recent build source. The other is the most recent <em>sucessful</em> build.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-09-21T22:03:48.447Z
Answers: 2023-09-21T22:03:48.447Z
Answers: Tony Chiboucas
Answers: <p>You <strong>can</strong> recover lost code when a Cloud Function deployment fails. But it is a bit cumbersome to do so.</p>
  <p>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.</p>
  <ol>
  <li><p>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: <code>gcr.io -&gt; PROJECT -&gt; gcf -&gt; REGION</code>. 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.</p>
  </li>
  <li><p>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.</p>
  </li>
  <li><p>Access the VM. When deployed simply SSH into the VM.</p>
  </li>
  </ol>
  <p>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:</p>
  <ol>
  <li>Create a new image: <code>docker create --name=&quot;my_tmp_image&quot; your_image:tag</code></li>
  <li>Then copy the code to a .tar file: <code>docker export my_tmp_image &gt; img.tar</code></li>
  <li>Download the file using the download button in the open SSH window.</li>
  </ol>
  <p>Once downloaded, you will find your source code in the workspace folder.</p>
  <p>And don't forget to kill the VM so you don't waste money on it.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-06-09T12:22:37.960Z
Answers: 2023-06-09T12:22:37.960Z
Answers: Alex
Answers: <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-07-31T16:01:42.323Z
Answers: 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.
Answers: questionto42
Answers: CC BY-SA 4.0
Answers: 2023-07-28T03:31:40.283Z
Answers: 2023-07-28T03:31:40.283Z
Answers: Jack Heidenreich
Answers: <p><strong>(This later turns out wrong, there <em>is</em> a way to recover the last run code, see the other answers)</strong></p>
  <p>There is no way to recover that failed deployment code.</p>
  <p>Yet, there is an easy workaround to never lose your code of the deployment time again: just deploy with <code>gcloud</code> 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.</p>
  <p>If you really want to roll back to the last running deployment, you can find that online.</p>
  <p>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 <code>Test the function</code> button since you will not use that button anymore.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-03-14T23:49:28.160Z
Answers: 2023-09-22T17:00:27.330Z
Answers: 2023-09-22T17:00:27.330Z
Answers: questionto42
Body: <p>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.</p>
  <p><a href="https://i.sstatic.net/XnHvl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XnHvl.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/oAH4S.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oAH4S.png" alt="enter image description here" /></a></p>
  <p>That means, in a Python Cloud Function, I have lost my most recent code in the &quot;main.py&quot;, any changes in the &quot;requirements.txt&quot; and even any changes in the environment variables.</p>
  <p>Is there a way to go back to the version of my tried deployment and work further from there again?</p>
  
Tags: <deployment><google-cloud-platform>
Title: When a Google Cloud Function fails to deploy, code and settings used for the deployment get lost. Can I revive the last deployment state?
UserDisplayName: questionto42
ThreadText: # When a Google Cloud Function fails to deploy, code and settings used for the deployment get lost. Can I revive the last deployment state?
  
  (Asked by: username_0 on 2022-01-28)
  
  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](https://i.sstatic.net/XnHvl.png)](https://i.sstatic.net/XnHvl.png)
  
  [![enter image description here](https://i.sstatic.net/oAH4S.png)](https://i.sstatic.net/oAH4S.png)
  
  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?
  
  **Tags:** deployment, google-cloud-platform
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2023-09-21. Score: 1 | Accepted answer
  
  # 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`/](https://console.cloud.google.com/storage/browser/%60bucket-id%60/%60function-folder%60/)
  
  There you'll find two versions. One is the most recent build source. The other is the most recent *sucessful* build.
  
  
  ---
  
  ### Answer by username_2 on 2023-06-09. Score: 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.
  
  
  ---
  
  ### Answer by username_3 on 2023-07-28. Score: 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.
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  ### Answer by username_0 on 2022-03-14. Score: 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.
  
  
  ---
  
  