Understand Google Apps Script (GAS) Versions and Deployments

  • fennng 

It confused me for a whole between Versions and Deployments on Google Apps
Script. Every time I make a change, I use the following command to deploy my
new version.

clasp push && clasp deploy

I have to push the code to the cloud, then deploy. Without any of these two
steps, it won’t work. The clasp push doesn’t create a version. It only sync
your local code to Google Cloud. The deploy will create a new version.

But the problem with this deploy command is that you can only create 20
deployments. Once you reach this number, you have to delete all your
deployments before you can run the deploy command.

When I deploy my app from the web portal of Google Apps Script, it creates
a new version, but will not create a deployment. This is what I want.

I dig into these and now fully understand the difference between versions
and deployments.

Basically, a version is a snapshot of code. A deployment is a end point
(URL), You can treat it this way.

The clasp deploy actually does three things.

1. Create a new version
2. Create a new deployment (This will generate a new URL)
3. Deploy the new version to the new URL.

Although it creates a new URL, your app may still work. The reason is that
your are using the Head Deployment, the head Deployment always point to the
latest Deployment. But it’s not recommended to be use for production. (I
believe most people use it as production)

developers.google.com/apps-script/concepts/deployments#head_deployments

Note that the Head Deployment is still using the exec endpoint, it’s
running the latest versioned code. Not the latest code (saved but
unpublished) in the web editor.

Whereas, when you deploy a new version using the Google Apps Script web
portal, it does one thing:

1. Create a new version

This will work if you use head deployment in production, as the head
deployment always point to the latest code. (But this is not always true,
when you deploy from the web portal, if you choose a old version rather
than the “new” version, the head deployment will now point to the old
version, this make you easy to rollback to your old version if you are not
happy with the latest version).

As I mentioned, as each deployment has an URL, you can create certain
version for production, then use the head version for development. You can
also manage all your deployments from the web portal as below:

On this interface, you can create new deployments, edit deployments. You
can choose which version to deploy on the new URL.

As the following screenshot, I just created a new deployment call Live and
the deployment is using version 52 of the code. I now can use this URL in
my App to consume the service.

So I will only use the Head Deployment URL in my test environment.

As clasp deploy command will always create a new deployment. It’s not
suggested to be used.

The right way to do this is using the version command with the redeploy
command.

First, I use clasp deploy to create a new deployment and edit it on web
portable.

When I created a new deployment, a deployment id will be returned.

Now I will create a new version everytime I want to test my code using

clasp version

It will return the version number.

Now I will use the deployment id and version number to do a redeployment.

clasp redeploy
AKfycbyHpy7In1NHTbDQLg4jkG4PaijeXjQcXxLyzs0oUnD8GupKI9AGWdljpVdqPxs_Ax8WbA
55 "Added a new feature"

Now my “Test” Deployment end point will be updated to use the latest
version of code. I will not create a lot deployments any more. My test Env
will use the Test URL. My production env will use the Live URL.

I will use the same command to deploy to my Live URL when a version is well
tested.

 

Update:

Once I updated my clasp to 1.7.0, there is a easier way to do the deployment. This single command will create an new version and also do a redeployment. The redeploy option has been removed from clasp

clasp deploy --deploymentId AKfycbw2wUrlpt9qZbLRpO6v8cdGT3BbAB9in3zmIcyfvwJSYxKYhoAWhms82LyP2gFKWvVU

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注