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:

get-version:
    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 ""

download-deb:
    after: 
      - 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 :

download-deb:
    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:

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

  download-deb:
    after: [get-version]
    plugin: nil
    build-packages:
      - wget
    override-pull: |
      set -eux
      . /tmp/snapd-project-version
      wget "http://ftp.de.debian.org/debian/pool/main/s/snapd/snapd_${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"
2 Likes

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

source: 
  - 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.

2 Likes

I know that it is a sample, but it still shows bad behavour. Please use apt download which will verify GPG signatures on hashes of the deb, or please use https links to download things.

Alternatively it is much better to use pull-pkg or its variants (pull-lp-debs pull-debian-debs etc) which attempt to do all of the above for you.

1 Like