Skip to content

Commit 0f242c8

Browse files
authored
feat: add environment variable support to cache key templates (#250)
I added support for customizing the key, but forgot the most important piece that we need, being able to reference an environment variable value. Our CI runner provider is now exposing an environment variable that indicates if the OS configuration changes, and we plan to use that one to invalidate the cache, otherwise we get errors because the cached dependencies are linking against an invalid / non-existent `glibc` version. > [!IMPORTANT] > I wrote the code with `claude code` and reviewed it afterwards ## Summary (Claude-generated) - Add support for `{{env.VAR_NAME}}` syntax in cache key templates to allow reading environment variable values - Enables more flexible cache key customization based on CI/CD environment variables like branch names, deployment environments, or custom build identifiers - Maintains backward compatibility with existing cache key templates ## Examples ```yaml # Include branch name from environment cache_key: 'mise-{{env.GITHUB_REF_NAME}}-{{platform}}-{{file_hash}}' # Use custom deployment environment cache_key: 'mise-{{env.DEPLOY_ENV}}-{{platform}}-{{file_hash}}' # Conditional logic with environment variables cache_key: '{{default}}{{#if env.CUSTOM_SUFFIX}}-{{env.CUSTOM_SUFFIX}}{{/if}}' ``` ## Changes - Modified `processCacheKeyTemplate()` in `src/index.ts` to include `process.env` in template data - Updated `action.yml` documentation to include the new `{{env.VAR_NAME}}` syntax - All existing functionality remains unchanged ## Test plan - [x] Build and package successfully with `npm run all` - [x] Linting and formatting pass - [ ] Manual testing with environment variables in cache key templates - [ ] Verify backward compatibility with existing cache key configurations
1 parent ca0c5fc commit 0f242c8

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ inputs:
5050
description: |
5151
Override the complete cache key (ignores all other cache key options).
5252
Supports template variables: {{version}}, {{cache_key_prefix}}, {{platform}}, {{file_hash}},
53-
{{mise_env}}, {{install_args_hash}}, {{default}} and conditional logic like {{#if version}}...{{/if}}
53+
{{mise_env}}, {{install_args_hash}}, {{default}}, {{env.VAR_NAME}} for environment variables,
54+
and conditional logic like {{#if version}}...{{/if}}
5455
experimental:
5556
required: false
5657
default: "false"

dist/index.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,11 @@ async function processCacheKeyTemplate(template: string): Promise<string> {
351351
const defaultTemplate = Handlebars.compile(DEFAULT_CACHE_KEY_TEMPLATE)
352352
const defaultCacheKey = defaultTemplate(baseTemplateData)
353353

354-
// Prepare final template data including the default cache key
354+
// Prepare final template data including the default cache key and env variables
355355
const templateData = {
356356
...baseTemplateData,
357-
default: defaultCacheKey
357+
default: defaultCacheKey,
358+
env: process.env
358359
}
359360

360361
// Compile and execute the user's template

0 commit comments

Comments
 (0)