I\'m experimenting with more cost effective ways to deploy my Rails apps, and went through the Ruby Starter Projects to get a feel for Google Cloud Platform.
It\'s al
The command below may work:
gcloud app deploy app.yaml worker.yaml --promote --stop-previous-version
see: https://cloud.google.com/sdk/gcloud/reference/app/deploy
I cannot find any documents about the default values of promote
and stop-previous-version
. As far as I observed, promote
is True
and stop-previous-version
is False
though.
I wrote both options for safety.
We created a little Python script to delete old versions automagically. We run it before every deploy so that our deploys always succeed (and never fail due to having too many versions deployed already).
https://pypi.org/project/appengine-clean/
To turn off the instances associated with older versions you could try to use the teardown scripts (eventually customised) mentioned in the delete tutorial resources doc:
If you tried the tutorial about how to run Ruby on Compute Engine, see the Deployment and teardown scripts section of that tutorial
Careful with deleting the app, though, you can't re-use the app id!
Since the older versions are no longer the default ones at least no new instances will be automatically starting for them. You may still need to delete them manually, unless Google automatically deletes the older automatically assigned versions exceeding the max number of versions allowed for an app.
We deploy an App Engine instance from Cloud Build, which is a common deployment pattern. At the end of the cloudbuild.yaml, we specify the following commands, which deletes every version except the last 5 versions:
versions=$(gcloud app versions list \
--service default \
--sort-by '~version' \
--format 'value(VERSION.ID)' | sed 1,5d)
for version in $versions; do
gcloud app versions delete "$version" \
--service default \
--quiet
done
To stop all instances of all non-default versions of all modules (independently of what languages those modules are in), you could add a small control
module, written in Python, using the modules
API:
from google.appengine.api.modules import modules
# core logic (inside a cron or other handler)
for m in modules.get_modules():
dv = modules.get_default_version(m)
for v in modules.get_versions(m):
if v != dv: modules.stop_version(m, v)
This doesn't delete the non-default versions (the modules
API does not appear to currently support deletion), but does ensure that none of their instances are running (so no charges would be incurred for them).
This core logic is meant for you to wrap it inside a handler, so you can trigger it as required, for example in a cron job, or in a "regular" handler that you trigger from the outside (with appropriate auth) e.g. via wget or curl within your bash scripts.
I don't believe there's a Ruby version of Python's google.appengine.api.modules.modules
, but, I could be wrong... I just couldn't find one. However, a simple Python-coded module should let you control modules coded in whatever other App Engine language (since App Engine lets you mix and match modules coded in different languages).
The following removes all version except 5 most recent:
Uses ghead
(on macOS, brew install coreutils
), replace with head
for linux.
gcloud app versions delete `gcloud app versions list | sed 's/ */:/g' | cut -f 2 -d : | tail -n +2 | ghead -n -5 | tr "\n" " "`