License as a env variable?

I’m currently working on a project that uses Kirby CMS, and I’m trying to find a way to inject the Kirby license as an environment variable instead of storing the license file in the repository. I don’t want to expose the license key or have it accidentally uploaded to a public repository and or to expose it to local environments.

To add details, I have a CI setup which is literally a php buildpack, builds everything from scratch and gets kirby through composer alongside all its needed plugin, only mounting the folders I need to mount from the repo.
The root folder gets cleaned out from composer everytime I deploy so the license gets lost on production at every push, if I were to put my license file within the repo to mount it/copy after deployment I then cannot gitignore the license file.

One of the criteria why am I after this is because I want the CMS to be portable across different projects, let’s say I have a kit built myself which provides headless functionality alongside as different tools to quickly build pages. The only thing I would need to do is to create a new droplet, set the license keys and have a headless CMS ready to go in matter of minutes.

I’ve been searching online for a solution but haven’t found anything that works yet. Has anyone done this before or have any suggestions on how to accomplish this? Any help would be greatly appreciated!

Thanks in advance!

I don’t think there’s currently an option to make Kirby directly accept an environment variable as license file.

The “kits” however come by default with a rule that excludes license files from git repos, committing one by mistake is therefore rather unlikely.
Local environments don’t need a license.

Since they normally aren’t present in git repos, the license file is normally created through the panel and is present only on the production server. If for some reason you have a setup where the production environment gets regularly completely erased (deleting the license file), like in some kind of CI setup, I’d suggest to configure the CI job to write the license file into the site/config folder, and that obviously could be done via env variables.

there would be GitHub - beebmx/kirby-env: Enable env variables to Kirby 3 as plugin but you can of course call an env variable in the PHP config file.

But the license file needs to be present in the config folder because it is read via $system->license()(kirby/System.php at 9ecd11cd5b7d4875d4212f767a2b3ad3e52b3528 · getkirby/kirby · GitHub).

Could be done with a workaround, i.e. extend Kirby and System classes and then overwrite $system->license() method, but probably a bit overkill, when the .license file can be ignored in .gitignore.

I should’ve expanded the first post better, yes I have a CI setup which is literally the php heroku buildpack, builds everything from scratch and gets kirby through composer only mounting the folders I need to mount from the repo, so I cannot gitignore the license file.

Hoped there was a quick win to have let’s say a single project which deploys into multiple domains with multiple licenses without changing the CI script to generate a license file.

After reading the posts I’ll rephrase it: I guess there’s no way to provide key and registered email address through the config file like I used to do in kirby2?

I mean you could do something like this in config.php (untested):

<?php

$lroot = kirby()->root('license');
$lenv = getenv('LICENSE_CONTENT');
if($lenv && !F::exists($lroot)) {
    F::write($lroot, $lenv);
}

return [
    // usual stuff
];
2 Likes

Solution was so simple I am ashamed I haven’t thought about it after going in circles for few hours.

Works as is, but I needed to move it on the ready callback into the config as discussed here: Can't access kirby() from the config file · Issue #1724 · getkirby/kirby · GitHub

So in the end I’ve got:

<?php
use Kirby\Filesystem\F;

return [
  ...
  'ready' => function($kirby) {
    $license_file = $kirby->root('license');
    $license = getenv('KIRBY_LICENSE');

    if ($license && !F::exists($license_file)) {
      F::write($license_file, $license);
    }
  }
];

Thanks again :slight_smile:

4 Likes