Set snap version dynamically according to Gemfile version

Hi, I noticed that the Travis snap package is out of date, likely because they need to bump the version manually.

They already bump the version manually on their Gemfile (it’s a Ruby app). I would like to write a patch so that snapcraft can read the version number from such Gemfile instead of having a fixed value.

So I was thinking to edit the snapcraft file like this:

name: travis
version: nil
summary: Travis CI client
description: CLI and Ruby client library for Travis CI

grade: stable

confinement: strict

    command: travis
    plugs: [network, home, removable-media]

    plugin: ruby
    build-packages: [wget]
    override-build: |
      snapcraftctl build
      GEM_VERSION=`ruby -e 'require "rubygems"' -e 'puts Gem::Specification::load("travis.gemspec").version'`
      snapcraftctl set-version $GEM_VERSION
      wget  "$GEM_VERSION.tar.gz"
      gem build travis.gemspec
      gem install travis-*.gem --env-shebang
    prime: ['-lib/ruby/gems/*/gems/ffi-*/ext/ffi_c/libffi-*/include/ffitarget.h']
    stage-packages: [git]


  1. Instead of override pull can I set the source dynamically? Like


I managed to make it work, however, the version remains nil. Do you know why? I’m thinking it’s because I’m setting it in the build stage, but I’m not sure. Also, I cannot set it in the pull stage as ruby is not yet installed at that time. Some help?


I needed to remove version and set adopt-info: travis, where travis is the name of the part that sets such info

Pull request pending

There is currently no support for variables like that in the source field.

1 Like

Too bad, it would make thigs much more easier, but I imagine it would also make the parsing much more complicated :confused:

A possible idea, to eventually consider if such variable replacing mechanism was also useful in other scenarios, is to use a template mechanism. So you parse the template first, which replaces the variables, and you produce the yaml, to be parsed from snapcraft. Actually, I think it could work even today, without modifications, using something like Jinja to parse the template and then using running snapcraft on the produced yaml :slight_smile: Just thinking aloud :wink:

Nowadays, it’s possible!

You just have to use:

craftctl set version=$(run some command to get the version)

Like this, for example!

It’s also important to use the adopt-info high level key (see using external metadata docs), mentioning the part that you are fetching the version, or just create a part called version like it’s done in the previous example.