Skip to content

Commit

Permalink
feat: support env-mapping option (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 authored Jul 29, 2024
1 parent 499b62b commit fdc9259
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 107 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
mount-if-exists: false
workspace-copy: true
native-dev-drive: false
env-mapping: ''

test-formats:
strategy:
Expand Down Expand Up @@ -303,3 +304,42 @@ jobs:
if (-not (Test-Path -Path test.txt -PathType Leaf)) {
exit 1
}
test-env-mapping:
runs-on: windows-2022
steps:
- name: Check out source code
uses: actions/checkout@v4
- name: Setup Dev Drive
uses: ./
with:
workspace-copy: true
env-mapping: |
CARGO_HOME,{{ DEV_DRIVE }}/.cargo
RUSTUP_HOME,{{ DEV_DRIVE }}/.rustup
MY_ENV_VAR , {{ DEV_DRIVE_PATH }}
MY_OTHER_ENV_VAR,{{ DEV_DRIVE_WORKSPACE }}\other\path
SHOULD_WARN,
should_also_warn,about_template
invalid-env-var,???
invalid.env.var,!!!
ENV_VAR_VALUE_INCLUDES_COMMAS,{{ DEV_DRIVE}}/my file name, with commas.txt
a
,
42,{{ DEV_DRIVE }}/42
does_it_substitute_1137,{{ DEV_DRIVE }}/$DEV_DRIVE
- name: Verify Env Mapping
run: |
if ($Env:CARGO_HOME -ne '${{ env.DEV_DRIVE }}/.cargo') { Write-Error 'Failed CARGO_HOME'; exit 1 }
if ($Env:RUSTUP_HOME -ne '${{ env.DEV_DRIVE }}/.rustup') { Write-Error 'Failed RUSTUP_HOME'; exit 1 }
if ($Env:MY_ENV_VAR -ne '${{ env.DEV_DRIVE_PATH }}') { Write-Error 'Failed MY_ENV_VAR'; exit 1 }
if ($Env:MY_OTHER_ENV_VAR -ne '${{ env.DEV_DRIVE_WORKSPACE }}\other\path') { Write-Error 'Failed MY_OTHER_ENV_VAR'; exit 1 }
if ($Env:SHOULD_WARN -ne $null) { Write-Error 'Failed SHOULD_WARN'; exit 1 }
if ($Env:should_also_warn -ne $null) { Write-Error 'Failed should_also_warn'; exit 1 }
if (${Env:invalid-env-var} -ne $null) { Write-Error 'Failed invalid-env-var'; exit 1 }
if (${Env:invalid.env.var} -ne $null) { Write-Error 'Failed invalid.env.var'; exit 1 }
if ($Env:ENV_VAR_VALUE_INCLUDES_COMMAS -ne '${{ env.DEV_DRIVE }}/my file name, with commas.txt') { Write-Error 'Failed ENV_VAR_VALUE_INCLUDES_COMMAS'; exit 1 }
if ($Env:a -ne $null) { Write-Error 'Failed a'; exit 1 }
if (${Env:,} -ne $null) { Write-Error 'Failed ,'; exit 1 }
if (${Env:42} -ne $null) { Write-Error 'Failed 42'; exit 1 }
if ($Env:does_it_substitute_1137 -ne '${{ env.DEV_DRIVE }}/$DEV_DRIVE') { Write-Error 'Failed does_it_substitute_1137'; exit 1 }
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"arrowParens": "avoid",
"bracketSameLine": false,
"bracketSpacing": true,
"editorconfig": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"jsxSingleQuote": false,
Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ You can optionally pass parameters to the action as follows:
workspace-copy: false
# Use native dev drive support when available. Defaults to true.
native-dev-drive: true
# Custom mappings of output environment variables. Defaults to no mapping.
env-mapping: |
MY_PROJECT_BIN,{{ DEV_DRIVE }}/path/to/bin
```
This action is [compatible](#runner-compatibility) with `windows-2022` runners or above.
Expand Down Expand Up @@ -158,6 +161,59 @@ This action will automatically use the built-in [Windows Dev Drive](https://lear
on your behalf when it's available on your Windows runner and `ReFS` is used.
You can use this option to turn this automatic usage off.


### *env-mapping*

By default, this option is not set.

This option provides syntactic sugar to manage the environment variables exposed by this action.

On a particular job, it can be repetitive having to re-declare the environment variables like below.

```yaml
- uses: samypr100/setup-dev-drive@v3
- name: Step A
env:
CARGO_HOME: ${{ env.DEV_DRIVE }}/.cargo
RUSTUP_HOME: ${{ env.DEV_DRIVE }}/.rustup
run: ...
- name: Step B
env:
CARGO_HOME: ${{ env.DEV_DRIVE }}/.cargo
RUSTUP_HOME: ${{ env.DEV_DRIVE }}/.rustup
run: ...
- name: Step C
env:
CARGO_HOME: ${{ env.DEV_DRIVE }}/.cargo
RUSTUP_HOME: ${{ env.DEV_DRIVE }}/.rustup
run: ...
# ...
```

This option allows you to define them once per job as shown in the example below.

It leverages [handlebars](https://handlebarsjs.com/) syntax under the hood to expose the supported
[environment variables](#environment-variables), giving you the ability to create new ones with
their contents after the action runs, so they can be automatically set in subsequent steps.

**Warning**: No canonicalization is performed on the input. The template is substituted as-is with the
typical values of the environment variables and the rest of the input is then appended as-is.

```yaml
- uses: samypr100/setup-dev-drive@v3
with:
env-mapping: |
CARGO_HOME,{{ DEV_DRIVE }}/.cargo
RUSTUP_HOME,{{ DEV_DRIVE }}/.rustup
- name: Step A
run: ...
- name: Step B
run: ...
- name: Step C
run: ...
# ...
```

## Environment Variables

These environment variables are meant to be used along `working-directory` to make sure
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ inputs:
native-dev-drive:
description: "Use native dev drive support when available."
default: "true"
env-mapping:
description: "Allows mapping environment variables generated by this action to new user-defined ones."
required: false
runs:
using: "node20"
main: "dist/setup/index.js"
Expand Down
2 changes: 1 addition & 1 deletion dist/cleanup/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/setup/index.js

Large diffs are not rendered by default.

Loading

0 comments on commit fdc9259

Please sign in to comment.