Support LicenseKey via environment variable fallback#4634
Conversation
LicenseKey now falls back to the AUTOMAPPER_LICENSE_KEY environment variable if not explicitly set. Added comprehensive unit and integration tests to verify this behavior. Updated test project dependencies to support DI and logging in tests.
|
@jbogard can you run this pipeline to check if anything goes wrong |
|
Hi @jbogard, is this issue related to pipeline, am I missing something? |
|
Hmm that is odd, I tried to re-run it but still getting that error. |
|
yeah @jbogard thats strange does it have something to do with ci pipeline and about read and write permissions? |
|
Hi @jbogard I see you changed the permission into your release.yml and maybe same has to be done from ci.yml from read to write for this to complete as mentioned in my previous comment, and you can try firing the pipeline to verify. Thanks |
|
This would be a great relief if we have this directly fed from the configuration, and introduce less noise ... as we are having more than dozens of services which is using automapper and we just acquired the enterprise license. We are looking for best option weather to go with per service configuration v/s one-place shared library. Both holds its own pros and cons, but if we get this env. var auto-discovery, it would be the real winner for us... with minimal changes. Waiting for this and any other valuable suggestions!!! |
|
Hey all, I want this to land in the next release. I need this to also work with MediatR so I'm going to suggest some changes that allow it to work in environments that use a single Bundle key across both products in a single application. |
Move environment-variable fallback out of the MapperConfigurationExpression property getter and into LicenseAccessor, the single consumer, keeping the config expression a plain POCO. Add a second, shared LUCKYPENNY_LICENSE_KEY environment variable so the same Lucky Penny bundle key can be used across products (e.g. MediatR). Resolution precedence: explicit cfg.LicenseKey > AUTOMAPPER_LICENSE_KEY > LUCKYPENNY_LICENSE_KEY (product-specific wins, matching Duende's two-tier "most-specific config key wins" model). Tests now target LicenseAccessor.ResolveLicenseKey directly for precedence, and the class disables parallelization since it mutates process-global environment variables. Docs updated with the auto-discovery section. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for this, @Nit8 — really appreciate the contribution, and it pushed exactly the right feature forward. 🙏 I've pushed a few commits on top of your branch to get it merge-ready, and wanted to walk through what changed and why. What I kept: your core idea — falling back to an environment variable when no key is set in code, fully backward compatible — is exactly right, and the precedence model (explicit code value wins) is preserved. What I changed:
The env var names follow the flat all-caps convention used by other commercial .NET libraries (Particular's Thanks again — your authorship on the PR stands, this just builds on it. |
dorny/test-reporter needs checks:write to create its report check-run, but GitHub forces GITHUB_TOKEN to read-only on pull_request runs from forks, so the step errored and failed the whole job even though all tests passed. Mark both report steps continue-on-error so report publishing is best-effort; the Build and Test step already reflects actual pass/fail. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The build-windows job authenticated to Azure via OIDC on every run, but the Azure secrets aren't exposed to pull requests (empty on forks), so the login step failed PR builds. Azure login and package signing only matter for the main-branch publish path, so gate both to github.ref == 'refs/heads/main', matching the existing Push to MyGet guard. Windows still builds and tests on PRs; it just skips the publish-only steps. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Thanks so much for taking the time to build on this rather than just closing it really means a lot.
The refactor to LicenseAccessor.ResolveLicenseKey() makes complete sense in hindsight; I hadn't considered the side-effect problem with hitting the environment on every property access.
Happy with everything you've done on top, and glad the core idea was worth building on. Looking forward to seeing it merged this is a library we use in our own project, and having run into this issue ourselves, it felt worth solving for everyone's benefit.
Looking forward to contributing more to this amazing project!
PR: Auto-Detect License Key from Environment Variables Fixes #4631
What Changed
Added support for reading the AutoMapper license key from an environment variable instead of requiring manual configuration in code.
Why
In containerized and cloud environments, it's better to use environment variables for sensitive configuration instead of hardcoding values in code.
How It Works
The LicenseKey property now checks for values in this order:
Usage
NEW WAY - Using Environment Variable:
Set environment variable AUTOMAPPER_LICENSE_KEY before running your app
Then just add AutoMapper without setting the license key:
OLD WAY - Still Works (Backward Compatible):
BOTH SET - Code value wins:
If you set both the environment variable AND code value, the code value is used.
Tests Added
Backward Compatibility
100% backward compatible. All existing code continues to work exactly the same way.