When creating a trigger programmatically, i.e. through ScriptApp.newTrigger()
, does it always point to the "head" deployment, or the most recent published (versioned) deployment? (See documentation on deployment types here.)
For context, I'm distributing a script in a multi-user environment which relies on triggers to run every few minutes. I'm trying to ensure that changes made in development (i.e. before publishing those changes) are not picked up immediately by all the clients of the script, as this effectively means any code updates in dev are instantly live for all users.
If triggers can only point to head deployments, what are best practices for pointing them at code which can be safely updated in dev (head version)?
- You want to know that when a trigger is installed by
ScriptApp.newTrigger()
, which version of script is used. - You want to know the method for installing a trigger by selecting version of script.
I understand your question like above. If my understanding is correct, how about this answer? Please think of this as one of several answers.
Answer for Q1:
When a trigger is installed by
ScriptApp.newTrigger()
, although the value of "Deployment" is not shown in "Current project's triggers", the latest script is run by the installed trigger. This situation is independent of the version of script. The current latest script is used.If the trigger is manually installed, you can select the deployment version at "Choose which deployment should run".
- Unfortunately, in the current stage, this cannot be achieved by the script yet. I think that this might be achieved by the future update.
Answer for Q2:
In order to install a trigger by selecting version of script, as a workaround, I would like to propose to use a library. The flow of this workaround is as follows. This is a sample flow.
- Create 2 standalone projects.
- One is used as a library.
- Another is used as the project to use the library.
- In the project to use the library, for example,
function myFunction() {lib.myFunction()}
is put. This script runs the function of the library.- Install the library to this project.
- You can select the script, that you want to make users run, by selecting the version of library.
- Trigger is installed to this project.
- The project of the library is the project for developing. It manages the version of developing script by "Manage version" of the script editor.
- When the trigger is installed, it runs
ScriptApp.newTrigger("myFunction")
in the project to use the library. At this time, when the trigger is run, the selected version of library is used. By this, the latest script of library is not used. Users can use the stable script of the version you selected.
- You can also see the version of library at Manifests file (appsscript.json). This file can be modified by the script. By this, you can also modify the version of script using by the trigger by the script.
References:
If I misunderstand your question, I'm sorry.
Yes, it always point to the "head" as versioned deployments are only available for Gmail Add-ons.
The above is based on that the only the guide to publish a Gmail add-on includes as step the creation of a versioned deployment -> https://developers.google.com/gmail/add-ons/how-tos/publish#step_1_create_a_versioned_deployment
来源:https://stackoverflow.com/questions/54210044/how-to-use-versioned-deployments-when-programmatically-creating-triggers