We will examine a complete snapcraft.yaml file for an application called wethr, a command line weather tool.
Below, we will look at blocks of code that were not previously covered in the Basic snapcraft.yaml example, or those that differ significantly from what we already discussed.
The full snapcraft.yaml file is available on the project GitHub page. The contents may change and slightly differ from the example shown below.
name: wethr
summary: Command line weather tool.
description: |
Get current weather
adopt-info: wethr
base: core22
grade: stable
confinement: strict
architectures:
- build-on: amd64
- build-on: armhf
- build-on: arm64
apps:
wethr:
command: bin/wethr
plugs:
- network
parts:
wethr:
plugin: npm
npm-include-node: true
npm-node-version: "10.14.1"
source: https://github.com/twobucks/wethr.git
source-type: git
override-pull: |
snapcraftctl pull
last_committed_tag="$(git describe --tags --abbrev=0)"
last_committed_tag_ver="$(echo ${last_committed_tag} | sed 's/v//')"
last_released_tag="$(snap info wethr | awk '$1 == "latest/beta:" { print $2 }')"
# If the latest tag from the upstream project has not been released to
# beta, build that tag instead of master.
if [ "${last_committed_tag_ver}" != "${last_released_tag}" ]; then
git fetch
git checkout "${last_committed_tag}"
fi
snapcraftctl set-version "$(git describe --tags | sed 's/v//')"
build-packages:
- git
- sed
The metadata, base, and confinement declarations are rather similar to our basic example, but with some notable differences:
This keyword instructs Snapcraft to “adopt” metadata information using external metadata sources. Such use can be useful for CI systems, where the declarations in the snapcraft.yaml file can be obtained from scripts rather than manually.
In this example, the wethr snap application version is obtained from the Git repository release tag. This is done two stages:
adopt-info: wethr
Alternatively, in this particular example, the version field could also be manually defined, e.g.: version: ‘1.5’.
The optional grade keyword defines the quality level of the snap. Two levels are available: devel and stable. Snaps with the devel grade level cannot be uploaded to either of the stable or candidate channels in the Snap Store.
grade: stable
This section defines the target architectures for which the snap should be built. It requires the build system that is running the Snapcraft tool to be able to compile and build the snap for the listed platforms.
architectures:
- build-on: amd64
- build-on: armhf
- build-on: arm64
While largely similar to the yt-dlp example, the wethr application does introduce some notable differences in the build definition section. We will discuss the parts section first.
The parts definition consists of the following lines of code:
parts:
wethr:
plugin: npm
npm-include-node: true
npm-node-version: "10.14.1"
source: https://github.com/twobucks/wethr.git
source-type: git
override-pull: |
snapcraftctl pull
last_committed_tag="$(git describe --tags --abbrev=0)"
last_committed_tag_ver="$(echo ${last_committed_tag} | sed 's/v//')"
last_released_tag="$(snap info wethr | awk '$1 == "latest/beta:" { print $2 }')"
# If the latest tag from the upstream project has not been released to
# beta, build that tag instead of master.
if [ "${last_committed_tag_ver}" != "${last_released_tag}" ]; then
git fetch
git checkout "${last_committed_tag}"
fi
snapcraftctl set-version "$(git describe --tags | sed 's/v//')"
build-packages:
- git
- sed
The wethr snap also only has one part. However, here, it is built using the npm plugin, which is a Snapcraft plugin designed to simplify the building of Node.js and JavaScript-based applications.
npm-node-version
is downloaded and included in the package.The apps build definition consists of the following lines of code:
apps:
wethr:
command: bin/wethr
plugs:
- network
The wethr example has a single application - wethr. Other snaps may have multiple sub-applications or executables.
Last updated 1 year, 2 months ago.