Skip to content

Commit 4522f64

Browse files
authored
feat: Allow specifying inputs as environment variables (#214)
1 parent a8c5cac commit 4522f64

File tree

5 files changed

+130
-21
lines changed

5 files changed

+130
-21
lines changed

.github/workflows/test.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,54 @@ jobs:
943943
py311 py312
944944
activate-environment: py311
945945

946+
env-variable-input-custom-bin-path:
947+
strategy:
948+
matrix:
949+
os: [ubuntu-latest, macos-latest, windows-latest]
950+
runs-on: ${{ matrix.os }}
951+
steps:
952+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
953+
- name: Move pixi.toml
954+
run: mv test/default/* .
955+
- uses: ./
956+
env:
957+
SETUP_PIXI_PIXI_BIN_PATH: custom-bin/pixi${{ matrix.os == 'windows-latest' && '.exe' || '' }}
958+
with:
959+
cache: false
960+
- run: |
961+
test -f custom-bin/pixi${{ matrix.os == 'windows-latest' && '.exe' || '' }}
962+
pixi --help
963+
which pixi | grep -q custom-bin/pixi
964+
# which pixi should be absolute
965+
which pixi | grep -q "^/"
966+
967+
env-variable-input-custom-pixi-url:
968+
runs-on: ubuntu-latest
969+
steps:
970+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
971+
- name: Move pixi.toml
972+
run: mv test/old-pixi-lockfiles/* .
973+
- uses: ./
974+
env:
975+
SETUP_PIXI_PIXI_URL: https://github.com/prefix-dev/pixi/releases/download/v0.14.0/pixi-x86_64-unknown-linux-musl
976+
with:
977+
cache: false
978+
- run: pixi --version | grep -q "pixi 0.14.0"
979+
980+
env-variable-input-precendence:
981+
runs-on: ubuntu-latest
982+
steps:
983+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
984+
- name: Move pixi.toml
985+
run: mv test/old-pixi-lockfiles/* .
986+
- uses: ./
987+
env:
988+
SETUP_PIXI_PIXI_URL: https://github.com/prefix-dev/pixi/releases/download/v0.14.0/pixi-x86_64-unknown-linux-musl
989+
with:
990+
cache: false
991+
pixi-url: https://github.com/prefix-dev/pixi/releases/download/v0.49.0/pixi-x86_64-unknown-linux-musl
992+
- run: pixi --version | grep -q "pixi 0.49.0"
993+
946994
test-cache-fail-no-lockfile:
947995
runs-on: ${{ matrix.os }}
948996
strategy:

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ Optionally, you can combine this with the `pixi-url-bearer-token` input argument
405405
pixi-url-bearer-token: ${{ secrets.PIXI_MIRROR_BEARER_TOKEN }}
406406
```
407407

408+
### Setting inputs from environment variables
409+
410+
Alternatively to setting the inputs in the `with` section, you can also set each of them using environment variables.
411+
The corresponding environment variable names are derived from the input names by converting them to uppercase, replacing hyphens with underscores, and prefixing them with `SETUP_PIXI_`.
412+
413+
For example, the `pixi-bin-path` input can be set using the `SETUP_PIXI_PIXI_BIN_PATH` environment variable.
414+
415+
This is particularly useful if executing the action on a self-hosted runner.
416+
417+
Inputs always take precedence over environment variables.
418+
408419
## More examples
409420

410421
If you want to see more examples, you can take a look at the [GitHub Workflows of this repository](.github/workflows/test.yml).

dist/index.js

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

dist/post.js

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

src/options.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,30 @@ export const PATHS = {
9494
pixiBin: path.join(os.homedir(), '.pixi', 'bin', `pixi${os.platform() === 'win32' ? '.exe' : ''}`)
9595
}
9696

97+
const getEnvironmentVariableName = (key: string): string => {
98+
return `SETUP_PIXI_${key.toUpperCase().replace(/-/g, '_')}`
99+
}
100+
101+
const inputOrEnvironmentVariable = (key: string): string | undefined => {
102+
const inputValue = core.getInput(key)
103+
// GitHub actions sets empty inputs to the empty string
104+
if (inputValue !== '') {
105+
return inputValue
106+
}
107+
108+
const envVarName = getEnvironmentVariableName(key)
109+
const envVarValue = process.env[envVarName]
110+
// Empty environment variables are treated as undefined
111+
if (envVarValue !== undefined && envVarValue !== '') {
112+
core.debug(`Using environment variable ${envVarName} with value: ${envVarValue}`)
113+
return envVarValue
114+
}
115+
return undefined
116+
}
117+
97118
const parseOrUndefined = <T>(key: string, schema: z.ZodSchema<T>, errorMessage?: string): T | undefined => {
98-
const input = core.getInput(key)
99-
// GitHub actions sets empty inputs to the empty string, but we want undefined
100-
if (input === '') {
119+
const input = inputOrEnvironmentVariable(key)
120+
if (input === undefined) {
101121
return undefined
102122
}
103123
const maybeResult = schema.safeParse(input)
@@ -111,18 +131,16 @@ const parseOrUndefined = <T>(key: string, schema: z.ZodSchema<T>, errorMessage?:
111131
}
112132

113133
const parseOrUndefinedJSON = <T>(key: string, schema: z.ZodSchema<T>): T | undefined => {
114-
const input = core.getInput(key)
115-
// GitHub actions sets empty inputs to the empty string, but we want undefined
116-
if (input === '') {
134+
const input = inputOrEnvironmentVariable(key)
135+
if (input === undefined) {
117136
return undefined
118137
}
119138
return schema.parse(JSON.parse(input))
120139
}
121140

122141
const parseOrUndefinedList = <T>(key: string, schema: z.ZodSchema<T>): T[] | undefined => {
123-
const input = core.getInput(key)
124-
// GitHub actions sets empty inputs to the empty string, but we want undefined
125-
if (input === '') {
142+
const input = inputOrEnvironmentVariable(key)
143+
if (input === undefined) {
126144
return undefined
127145
}
128146
return input

0 commit comments

Comments
 (0)