Get snap revision inside of a hook

How can I find the revision number of the snap that is being refreshed inside of a hook?

My project writes a parameter file (lets call it /tmp/parameters.yaml) that needs to be backed up before refreshing and restored after refresh depending on the snap revision number.

For example, let’s say I run “snap list” and get:

Name                 Version    Rev   Tracking       Publisher         Notes
...
myawesomesnap  0.1.2      9     latest/edge    me  -
...

and that I have released revision 10. When I execute the refresh I want the pre-refresh hook to back up the parameters of revision 9 (lets call it /tmp/parameters.yaml.rev-9) and I want the post-refresh hook to look for the backed up parameters for the revision being installed ( /tmp/parameters.yaml.rev-10) and restore the parameters file if the backup exists.

I have defined the following hooks:

hooks:
  configure:
    plugs: []
  pre-refresh:
    plugs: []
  post-refresh:
    plugs: []

the corresponding scripts in the folder “snap/hooks” exist and are executed properly. How can I find the information that I need?

Thanks in advance for your help

your environment should have the $SNAP_REVISION variable available …

2 Likes

To build on @ogra’s comment, all of the snap environment variables should be available to you within hooks. It does sound like $SNAP_REVISION is exactly what you’re looking for there, and the hooks you’re using map up to what you’re looking to get out of it (i.e. pre-refresh runs on the revision that will soon become the old revision, post-refresh runs on the new revision).

1 Like