GithubHelp home page GithubHelp logo

romainvialard / programmatically-deploy-a-web-app Goto Github PK

View Code? Open in Web Editor NEW
7.0 0.0 3.0 16 KB

Use the Apps Script API to save a new version of a web app and deploy it

License: Apache License 2.0

JavaScript 100.00%
gsuite apps-script google-apps-script deployment-automation

programmatically-deploy-a-web-app's Introduction

Programmatically deploy an Apps Script web app

History / background

In 2012, the Google Apps Script team released the Script service, which provided the ability to publish scripts as web app programmatically, using ScriptApp.getService().enable(). But soon after this become broken then deprecated due to different updates.

You can follow the issue / feature request here: https://issuetracker.google.com/issues/36756212

Thanks to updates of the Apps Script API it is now possible again to programmatically deploy a web app. Note that if you are developing Apps Script projects locally, it is recommended to use clasp, which provides all required methods.

The present code sample is useful for developers who want to manage this deployment via Apps Script itself.

Deployment process

To be able to programmatically deploy a script as a web app, several steps are required:

  1. The project manifest (appscript.json) needs to be updated with the correct web app configuration (not required if script is already deployed as a web app and you simply want to publish a new version)
  2. A new version of the project needs to be created (or at least one version must have been created and selected as the version to publish)
  3. Then we can deploy as a web app this new version (through the 'deployments' endpoint)

Apps Script deployments and web apps

Note that you should not create a new deployment but instead update an existing one. Creating a new deployment would generate a new web app, with a new url while update the existing one will preserve the web app url (exactly like when you use the "Deploy as web app" menu entry).

Specifically, when you deploy your script as a web app, it will generate a deployment named "web app meta-version" and this is the deployment that you should update.

alt_text

Note that everything is working perfectly if you first do it manually (using the "Deploy as web app" menu entry) and then push updates programmatically. But if you want to do everything programmatically, including the first deployment, you will not be able to ever use the "Deploy as web app" menu entry (well, you can, but this will generate a new deployment, with a new web app url, because the Apps Script editor will not recognize that the script is already deployed as a web app).

Also note that with the release of deployments, it is now possible to have several web app urls, linked to different versions of the same Apps Script project (you can have a deployment for production and another one for development but it is also possible to create many more instances).

Note that there's no need to use Eric's OAuth library, as you can simply add the required scopes in the manifest of your project and do calls using your script oauth token (with ScriptApp.getOAuthToken()).

Specifically, the following scopes are required:

programmatically-deploy-a-web-app's People

Contributors

romainvialard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

programmatically-deploy-a-web-app's Issues

What am I doing wrong here?

I copied the exact same script in the Code.gs.js file into my project and replaced projectId to equal ScriptApp.getScriptId(), so that when the spreadsheet file is copied, the variable is dynamically set to equal to that script's unique id. However, all I get is a 403 error–

When debugging, it looks like when saveAndDeployNewVersion() is executed, the variable deploymentId is always undefined. Neither getWebAppDeploymentId_(projectId) nor createDeploymentAsWebApp_(projectId, newVersionNumber) return a value. Is that what's supposed to happen? Could that be where the problem is? What am I doing wrong here?

Any idea on how to make this work when the user makes a copy of a document bound script?

Thanks for the idea & code, Romain. The problem arises when you try to distribute a script (webapp) inside a (for example) spreadsheet by simply using copies of it. When a copy of the spreadsheet is made, its new apps script project is not attached to a GCP one and as such cannot use the Apps Script REST API to generate deployments in the way you show, so in the end we are at the same point where the user has to publish his own copy of the webapp manually. Do you think there is any feasible way to overcome this?

Thanks and best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.