4

Some times ago I asked this: How to check in both server-side and client-side scripts if we are in preview mode or deployed version

Because I wanted in my code to have different logic whether it is the preview mode or not.

The answer was "preview mode is just another deployment and each deployment has its own Drive table, store some env variables there". That was true and made the trick.

Problem: Drive tables don't exist anymore.

I have not been working with AppMaker the past months so maybe I have missed new features:

  • how can I set environment variables per deployment ? (so I can make a difference between my prod deployment and my pre-prod deployment)

  • is there a way to get the current deployment name from the code ?

Thanks for your answers

Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44
  • I don't think so there is any methods in Appmaker which will return you current deployment name, one possible way out to achieve this will be checking URL as All deployed/preview instances will have different URLs (using `ScriptApp.getService().getUrl()`). However you have a valid question and I will be looking for more proper answer. – Darpan Sanghavi Aug 01 '18 at 08:24
  • 1
    You can try using the [google script properties service](https://developers.google.com/apps-script/reference/properties/) which is also unique per deployment (preview has its own set of properties as well). You could store your preview mode flag there. – Rherma Aug 01 '18 at 14:09
  • @Rherma make a proper response to the post and I'll accept it ;-) – Valentin Coudert Aug 07 '18 at 08:56

3 Answers3

2

This is an undocumented solution. It's not the best and neither recommended for long term usage because it can change in the future, however, to answer your question directly, you can get the deployment name via server scripting. Put the following on a server script:

function getDeployment() {
  var deploymentName = app.a.a.a[13].name;
  return deploymentName;
}

Then insert a button in the UI, add the following to the onClick event handler of the button:

google.script.run.withSuccessHandler(function(result){
  console.log(result);  
}).getDeployment();

Preview the app, test it and you should get Preview. Publish the app to a deployment and test it, you should get the deployment name. I hope it helps!

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
  • any doc on that ? Cause it's seems to be a variable we're not supposed to use – Valentin Coudert Aug 03 '18 at 09:40
  • I would tend to agree with Valentin. It seems that those variables are generated by GWT's compilation process and since they are not documented in the App Maker spec they could change (in turn breaking the app). It would be helpful if the deployment name was available as an official property of the `app` object, though. – Rherma Aug 06 '18 at 20:13
  • @Rherma I also agree, however if you know how to work with those properties, then that won't be a problem. It is only a problem if you don't know how to deal with it. Anyways, OP asked for a solution, here is one. If the OP likes it, then fine if not that is fine too. – Morfinismo Aug 06 '18 at 20:23
  • Hopefully I did not come off as rude. However, your solution is undocumented and the behavior could change without any notice. This fact doesn't have anything to do with knowledge of how those properties work. Google could one day decide to update or otherwise recompile App Maker and that property would change by way of GWT's compilation process. I agree that at the current moment you can access the deployment through those specific properties though. – Rherma Aug 06 '18 at 20:29
  • @Rherma I don't intend to be rude or anything similar. All I'm saying is, OP asked for **Get the deployment name?** and this solution directly answers what he is asking for. Obviously if this changes in the future, I will delete this answer, but for now, it does exactly what he is asking for. I don't see any other solution giving exactly what OP asked for. Nonetheless, I also have to agree with you, it's not the best solution since it is undocumented and most likely, it will change in the future. – Morfinismo Aug 06 '18 at 20:38
2

Deployment unique environment variables can be stored using the Google script properties service.

Rherma
  • 331
  • 2
  • 8
  • I tried and it works (server-side only). `PropertiesService.getScriptProperties()` gives access to a key-value store which is different for each deployment (and so different in preview mode too) – Valentin Coudert Aug 08 '18 at 10:03
  • Just as a side note - you may find it useful to cache those properties client side if you find yourself fetching those properties often. In this case since the deployment type stays the same throughout the entire session you may cache it as a custom page property. I often store this property in the page header as it is present in most pages. You then can write some simple timer function with exponential backoff to keep querying that header for a non-null property (and set the onAttach event to load the property from the server if it is null). – Rherma Aug 14 '18 at 20:20
  • Another choice would be to use a calculated model which has fields for each environment variable you need and then load them into one row of that model. You then can use that datasource for any page which might need them (although this requires that a server side script is called once every time a widget which uses that calculated model is attached). – Rherma Aug 14 '18 at 20:22
  • @ValentinCoudert I stumbled across this while trying to do something similar and I was wondering, how did you populate the property values to set the values you need for each different deployment? – Christina Aug 29 '19 at 12:01
  • @Christina I had to do it manually, from an "admin" screen I made – Valentin Coudert Aug 30 '19 at 09:01
1

Nothing has changed with SQL. The preview and deployment work with separate data sets. It is enough to put one setting record in a table and assing deployment or preview value respectively. Each new preview will work with preview data and each new deployment will work with life data.

  • 1
    I believe this is only true if you use the default cloud SQL service. If you use a custom google cloud instance your tables will share across deployments. I think a better solution (and closer to the idea of environment variables) is to use the script properties service. They are unique per deployment and since the preview is technically just another deployment it also has a unique set of properties. – Rherma Aug 06 '18 at 20:08