Using $SNAPCRAFT_PROJECT_VERSION with 'snapcraftctl set-version'

I want to get the last release tag of a github repository and then use it to define download links for .deb files.

Something like:

    plugin: nil
    source: https://repo.git
    source-type: git 
    override-pull: |
        snapcraftctl pull
        snapcraftctl set-version "$(git describe --tags --abbrev=0)"
        echo "$SNAPCRAFT_PROJECT_VERSION"  # <--- prints ""

      - get-version
    plugin: dump
    source: https://link/name_$SNAPCRAFT_PROJECT_VERSION_amd64.deb

It works if I use version: but not with adopt-info: and snapcraftctl set-version. The link looks like https://link/name__amd64.deb with adopt-info.

One possibility for env variable propagation:

In get-version

echo "export PROJECT_VERSION=123" > /tmp/snapcraft-project-version

in download-deb

. /tmp/snapcraft-project-version

Hmm snapcraftctl pull didn’t respect the env variable :

    plugin: dump
    source: https://link/name_$PROJECT_VERSION_amd64.deb
    source-type: deb
    override-pull: |
        export PROJECT_VERSION=123
        printenv               # <---- shows PROJECT_VERSION=123
        snapcraftctl pull      # <---- variable in link does not become 123 but stays "$PROJECT_VERSION"
        printenv               # <---- shows PROJECT_VERSION=123

edit: It looks like snapcraftctl doesn’t respect changes

I somehow missed you wanting to use the variable outside the scriptlet.

How about you download & extract the deb yourself, like so:

    plugin: nil
    override-pull: |
      set -eux
      echo "export PROJECT_VERSION=2.44.1-2" > /tmp/snapd-project-version

    after: [get-version]
    plugin: nil
      - wget
    override-pull: |
      set -eux
      . /tmp/snapd-project-version
      wget "${PROJECT_VERSION}_amd64.deb" \
       -O snapd.deb
    override-build: |
      set -eux
      dpkg-deb -xv $SNAPCRAFT_PART_SRC/snapd.deb $SNAPCRAFT_PART_INSTALL

You could simplify this by not using two separate parts, but I don’t know your requirements.

you could do this as a one-liner and actually “stream the deb to disk” :wink:

wget -q -O- $URL | dpkg -x - "$SNAPCRAFT_PART_SRC/snapd"

Thank you both. If I had two sources in the normal case, i.e.:

  - on amd64: URL
  - on i386: URL

What is the correct syntax to differentiate them in override-pull?

You have access to SNAPCRAFT_ARCH_TRIPLET. That should do it.

1 Like