Snapcraft build environment


My snap pulls a repo as part of build process. I would like to have a way to set the git branch dynamically while building. Is there a way to get a additional parameter passed during snapcraft build step. Or is there a way to get an environment variable inside snapcraft.yaml?


Hm, I’m not entirely sure what you’re asking here. Who or what decides which branch to use? Does the person or tool executing snapcraft decide that? Or do you have another part or a previous step which decides it during the execution of snapcraft.

Both developers and tool executing snapcraft would pass on the branch environment.

if we had another part which gets the environment variable via script, all snaps needs to run this script before any pull. Do you have any startup option like after.

It would be nice to have environment variables accessed in snapcraft.yaml

Environment variable exported in first-part is not seen in second-part. I don’t think passing environment variables trough snapcraft is going to work.

    plugin: nil
    override-pull: |
      export TEST=test
    plugin: nil
    override-pull: |
      env | grep TEST
      - first-part

Only way I found to dynamically build latest version is to override-build and do everything manually in script.

    plugin: nil
    override-build: |
      VERSION=$(curl --silent curl --silent | jq --raw-output .tag_name)
      snapcraftctl set-version "$VERSION"
      # pull and build here
      - curl
      - jq

That’s clear thanks, you basically want the “parameterized builds” feature from Jenkins.

As far as I know, this is not yet supported. It might be useful for you to create a request for this feature here: and link back to this thread for more context.

I can think of a few workarounds:

  • Have a makefile or script which changes snapcraft.yaml to point to the requested branch.
  • Have multiple snapcraft.yaml files, possibly generated from a single template.

Thanks let me check on the both suggestions