Building your Charms with Github Actions

We do hope to introduce our own official Github Actions in the near future to simplify the user experience! In the meantime, there are a couple of options to build your charms with Github Actions (or similar CI systems):

(1) Use Charmcraft’s --destructive-mode on an OS that matches a build-on base configuration in your project’s charmcraft.yaml (for more information, check out Juju | Charmcraft Configuration). Today, this would mean configuring either ubuntu-18.04 or ubuntu-20.04 as the operating system, with a corresponding bases config.

(2) Use Charmcraft’s default, with LXD used to build Charms for all applicable bases. On Github Actions, this means setting up LXD for use by Charmcraft and running charmcraft with the correct permissions. For example:

      - name: Configure LXD
        run: |
          sudo groupadd --force --system lxd
          sudo usermod --append --groups lxd $USER
          sudo snap start lxd
          sudo lxd waitready --timeout=30
          sudo lxd init --auto

      - name: Build my Charm
        run: |
          sg lxd -c "charmcraft -v pack"
          ...
1 Like

That’s great! Thanks for sharing!

Could we go one step further and also publish to latest/edge with a CI?

For anyone interested in a concrete, working example for GitLab CI, using approach (1) from above:

# FILE: .gitlab-ci.yml
charm:
  image: registry.gitlab.com/en-ser-public/gitlab-cicd-docker/build-charms-docker
  stage: build
  script:
  - charmcraft pack --destructive-mode
  artifacts:
    paths:
    - '*.charm'

The Dockerfile for the build image used above is very simple, installing charmcraft as described in the README:

FROM ubuntu:22.04

ARG CHARMCRAFT_VERSION=2.2.0
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
 && apt-get upgrade -y \
 && apt-get install -y \
    curl \
    git \
    libapt-pkg-dev \
    libffi-dev \
    libssl-dev \
    python3 \
    python3-pip \
    python3-venv \
 && apt-get clean \
 && pip3 install https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/python-apt/2.3.0ubuntu2/python-apt_2.3.0ubuntu2.tar.xz \
 && pip3 install charmcraft==$CHARMCRAFT_VERSION

I found helpful background details on --destructive-mode in Charmcraft bases & provider support.