15. Set up charm compatibility version

Prerequisites

The charm must use Poetry to manage Python dependencies.

charmcraft.yaml must contain a poetry-deps part that installs Poetry and makes the poetry command available.

Does the charm not use Poetry? Follow these instructions

Use reusable workflow

Add charm-refresh-build-version

Add charm-refresh-build-version to the build-refresh-version Poetry dependency group:

$ poetry add --group build-refresh-version charm-refresh-build-version

Mark the dependency group as optional

pyproject.toml
[tool.poetry.group.build-refresh-version]
optional = true

Update charmcraft.yaml

Add the following configuration to the files part. If the files part does not exist, create it.

Example charmcraft.yaml
parts:
  # "files" part name is arbitrary; use for consistency
  files:
    plugin: dump
    source: .
    after:
      - poetry-deps  # Ensure poetry is installed
    build-packages:
      - git
    override-build: |
      # Set `charm_version` in refresh_versions.toml from git tag
      # Create venv in `..` so that git working tree is not dirty
      python3 -m venv ../refresh-version-venv
      source ../refresh-version-venv/bin/activate
      poetry install --only build-refresh-version
      write-charm-version

      craftctl default
    stage:
      - LICENSE
      - refresh_versions.toml