DEV Community

Cover image for Creating a base build of  Snapcraft in Travis CI
Montana Mendy
Montana Mendy

Posted on

Creating a base build of Snapcraft in Travis CI

Sometimes you want to see how things work under the hood, and this is why I put together quickly how I used Snapcraft with Travis CI to get a LXD up and running, and download VLC and then get info on that version of VLC all inside Travis CI.

First let's start with out snapchat.yaml. This is how mine looks:

name: montana
summary: Im just testing out snapd with Travis
description: |
 My name is Montana Mendy and I'm a Software Engineer at Travis CI, I'm trying new things with Travis CI and the Brave Browser.
grade: stable
confinement: strict

  - build-on: amd64

    plugin: dump
    source-type: deb
    # Correct path to icon.
    override-pull: |
      snapcraftctl pull
      rm -rf etc/cron.daily/ 
      rm -rf usr/bin/brave-browser-dev
      chmod 4555 opt/
      unlink opt/
      sed -i 's|Icon=brave-browser|Icon=/opt/\.png|g' usr/share/applications/brave-browser-dev.desktop
      - desktop-gtk3
      - gir1.2-gnomekeyring-1.0
      - libasound2
      - libgconf-2-4
      - libgl1-mesa-glx
      - libglu1-mesa
      - libgnome-keyring0
      - libcap2
      - libgcrypt20
      - libnotify4
      - libnspr4
      - libnss3
      - libpulse0
      - libxtst6
      - libxss1

    command: bin/desktop-launch $SNAP/opt/
    desktop: usr/share/applications/brave-browser-dev.desktop
    # Correct the TMPDIR path for Chromium Framework/Electron to
    # ensure libappindicator has readable resources.
      - alsa
      - avahi-observe
      - browser-sandbox
      - camera
      - cups-control
      - desktop
      - gsettings
      - home
      - mount-observe
      - network
      - opengl
      - password-manager-service
      - pulseaudio
      - remove-media
      - screen-inhibit-control
      - unity7
      - upower-observe
      - x11

    interface: browser-support
    allow-sandbox: true
    interface: content
    target: $SNAP/data-dir/themes
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/icons
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/sounds
    default-provider: gtk-common-themes
Enter fullscreen mode Exit fullscreen mode

This is for the Brave Browser, but I've added things like pulseaudio to the Snap plugs. So let's push that into a directory called snap. Now let's head back to your root directory and make your .travis.yml, and this is what I coded out for my .travis.yml:

language: shell
dist: xenial
os: linux
group: edge 

    - LC_ALL: C.UTF-8
    - LANG: C.UTF-8

    - name: snapcraft
      channel: stable
      confinement: classic
    - name: lxd
      channel: stable

  - sudo usermod --append --groups lxd $USER
  - sudo /snap/bin/lxd.migrate -yes
  - sudo /snap/bin/lxd waitready
  - sudo /snap/bin/lxd init --auto
  - sudo apt install snapd
  - sudo snap install hello-world
  - sudo snap install nethack
  - snap version 
  - snap list
  - snap connections nethack
  - snap services lxd
  - sudo snap install --channel=edge vlc
  - which vlc 
  - snapcraft extensions
  - snap connections vlc
  - sudo systemctl enable --now snapd.socket
  - sudo journalctl -xeb | grep -i snap
Enter fullscreen mode Exit fullscreen mode

So let's trigger a build by using powerline in Vim, and if your build is successful it should look something like this:

Image description

Somethings you'll want to keep in mind with Snapcraft Multipass has 2 CPU's assigned to it, this can be variable depending on your settings:

Enter fullscreen mode Exit fullscreen mode

You can put those in your script: hook in your .travis.yml file.

Migrating between bases

A base snap is a special kind of snap that provides a run-time environment with a minimal set of libraries that are common to most Linux distributions. Think of it as a minimal Gentoo install. At the simplest level, in your snapcraft.yaml file you can do:

- base: core18
+ base: core20
Enter fullscreen mode Exit fullscreen mode

As you can see, only one of the base keywords need to be updated.

Enforcing Snap Policies

One thing you may want to consider when making this, is enforcing some security policies, the way you do that is, yes you guessed it another .yaml file, so let's call this one snap.yaml and it would look a little like this:

name: montana
version: 1.0
    command: mendy
    command: dig
    daemon: simple
    plugs: [network]
Enter fullscreen mode Exit fullscreen mode

If you run this with your Travis CI build and don't like what you see you can always set a snap disconnect conditional so you can disconnect. Don't forget you can add a Cron job directly from Snapcraft by adding this line in your .travis.yml:

sudo snap set system refresh.timer=fri5,23:00-01:00
Enter fullscreen mode Exit fullscreen mode

That's now a cron that will refresh Snap at a time of my choosing.


In some sense setting up Snapcraft for Travis CI was really fun, and got to see hands on how LXD containers are built from the ground up, setting your own policies, enforcing them and even picking your own plugins so you have a great foundation to build on when you're ready.

Here's my repository:

Top comments (0)