CI patterns for organic and external snaps

A snap can be organic to the app that it packs (e.g. a snap/snapcraft.yaml in the same repo as the app itself), or external (e.g. a user/foo-snap repo with just the snap). We need different CI patterns for each case.

CI patterns for organic snaps

Auto release on merge, and use git commit hash instead of version

With this pattern, you do not specify a version in snapcraft.yaml, and you don’t need to bump anything manually. The only thing you need is to have a top level field in snapcraft.yaml: version: git (example).

When released, snap info will show something like this:

latest/edge:      0+git.460c6c9 2024-08-22

From a github workflow perspective, you release to edge every merge to main:

# .github/workflows/release.yml
on:
  workflow_dispatch:
  push:
    branches:
      - main

In the workflow, you use snapcore/action-build and snapcore/action-publish with no special mods (example).

CI patterns for external snaps

Github doesn’t have events for when another repo has a release, so you need to take special care with an external snap to pick up new releases in an upstream project.

Manually edit the hard-coded version and source-tag

This is a manual solution but also the simplest one. In snapcraft.yaml you hard-code a top-level version: field, and then have a matching source-tag: field for the main part. Example.

From a github workflow perspective, you release to edge every merge to main, with basic usage of snapcore/action-build and snapcore/action-publish. Example.

Query periodically for a new version and file a PR

In this approach, the a workflow is running periodically,

on:
  schedule:
    - cron: '0 0,4,8,12,16,20 * * *'

and checking for a new release using pozetroninc/github-action-get-latest-release. If the version doesn’t match what we currently have under version: (and source-tag:), then we update the snapcraft.yaml with the new tag using yq,

yq -i '.version = strenv(version) | .parts.grafana-agent["source-tag"] = strenv(source_tag)'  \
  $GITHUB_WORKSPACE/main/snap/snapcraft.yaml

and open a PR (example). Then, on push to main, the snap is released to edge (example).

Hi @sed-i , thanks for your contribution and initiative in documenting this! :slight_smile: I’ve added it as an activity (#5029). We’ll review and assess how we can incorporate it into the official docs.

1 Like