Using custom variables throughout snapcraft.yaml

I’m trying to use variables in my snapcraft.yaml to have less repetition with changes. However I can’t find any way to get variables to work. e.g.

name: mysnap
version: '1.1'
environment:
  MY_VERSION: '1.1'
  MY_ARCHIVE: "mytool_1.1_20240613.zip"
summary: My Tool
description: 'A tool'

base: core22
confinement: strict
grade: stable
license: Apache-2.0

architectures:
  - build-on: [amd64]
    build-for: [amd64]
  - build-on: [arm64]
    build-for: [arm64]

apps:
  mytool:
    command: mytool_${MY_VERSION}_PUBLIC/mytoolRun
    plugs:
    - desktop
    - network
    - network-bind
    - x11

parts:
  mytool:
    plugin: dump
    source: https://github.com/me/mytool/releases/download/mytool_${MY_VERSION}_build/${MY_ARCHIVE}
    after:
    - patches
    override-build: |
      craftctl default
      cp $CRAFT_STAGE/patches/mytool.properties mytool*/support/mytool.properties

  patches:
    plugin: dump
    source: patches
    organize:
      '*' : patches/
    override-prime: ""

This fails at the first step of downloading the archive:

Failed to pull source: unable to determine source type of 'https://github.com/me/mytool/releases/download/mytool_${MY_VERSION}_build/${MY_ARCHIVE}'.

How can I use variable substitution in all things in the yaml?

Cheers.

You can’t use variables from top level into the parts directly. For version, you can use this

$SNAPCRAFT_PROJECT_VERSION as you’ve already set it.

1 Like

Thank you. I’ll accept that as a partial answer, but is there no way to create an arbitrary variable? There’s the filename which contains a datestamp and I would like to be able to track that as a variable just as for the version string.

Cheers.

1 Like

Absoulutely possible,

override-pull: |
    craftctl default #pulls the source code
    craftctl set version=$(cat path/to/file) #use craftctl tool to set the version

For ref

https://snapcraft.io/docs/using-craftctl

I don’t understand. In my example…

parts:
  mytool:
    plugin: dump
    source: https://github.com/me/mytool/releases/download/mytool_1.1_build/mytool_1.1_20240613.zip
    after:
    - patches
    override-build: |
      craftctl default
      cp $CRAFT_STAGE/patches/mytool.properties mytool*/support/mytool.properties

[/quote]

…I understand now that I can replace 1.1 with ${SNAPCRAFT_PROJECT_VERSION}, but unfortunately I also want to have mytool_1.1_20240613.zip as a variable.

You’ll have to override the pull to achieve such thing. Can you share us the repo and what you want to achieve?

I can’t share the project unfortunately, it’s closed for now. But essentially I want to be able to have a parameter that can be used throughout the snapcraft.yaml for the URL source:

name: mysnap
version: '1.1'
environment:
  MY_ARCHIVE: "https://github.com/me/mytool/releases/download/mytool_1.1_build/mytool_1.1_20240613.zip"

...

parts:
  mytool:
    plugin: dump
    source: ${MY_ARCHIVE}

As you’ve clarified already, I cannot achieve this, but you said that I can do this by overriding the pull. I’m just not clear how yet. You sent over the following snippet:

override-pull: |
    craftctl default #pulls the source code
    craftctl set version=$(cat path/to/file) #use craftctl tool to set the version

I don’t see how - even with the override - I can use a variable as the source URL to pull from.