Add activate-environment option (#108)

This commit is contained in:
Oliver Borchert
2024-05-11 15:11:52 +02:00
committed by GitHub
parent 2fcf8775bc
commit d72307a91c
11 changed files with 642 additions and 314 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ jobs:
run: |
set -euo pipefail
latest_version="$(jq -r '.version' package.json)"
count_expected=13
count_expected=15
count_actual="$(grep -c "setup-pixi@v$latest_version" README.md || true)"
if [ "$count_actual" -ne "$count_expected" ]; then
echo "::error file=README.md::Expected $count_expected mentions of \`setup-pixi@v$latest_version\` in README.md, but found $count_actual."
+108 -1
View File
@@ -693,7 +693,7 @@ jobs:
cache-write: false
cache-key: test-cache-${{ github.sha }}-${{ github.run_attempt }}-
# check the action logs to see if the cache write was skipped
pyproject:
timeout-minutes: 10
runs-on: ubuntu-latest
@@ -717,6 +717,113 @@ jobs:
cache: true
manifest-path: test/pyproject-manifest/pyproject.toml
environment-activation-unix:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/default/* .
- uses: ./
with:
activate-environment: true
- run: which python | grep '.pixi/envs/default/bin/python'
- run: env | grep CONDA_PREFIX
- run: '[[ $PIXI_ENVIRONMENT_NAME = "default" ]]'
environment-activation-windows-bash:
runs-on: windows-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/default/* .
- uses: ./
with:
activate-environment: true
- run: which python | grep '.pixi/envs/default/python'
- run: env | grep CONDA_PREFIX
- run: '[[ $PIXI_ENVIRONMENT_NAME = "default" ]]'
environment-activation-windows-pwsh:
runs-on: windows-latest
defaults:
run:
shell: pwsh
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/default/* .
- uses: ./
with:
activate-environment: true
- run: if (-Not (Get-Command python).Path -like '*\.pixi\envs\default\python') { exit 1 }
- run: if (!$env:CONDA_PREFIX) { exit 1 }
- run: if ($env:PIXI_ENVIRONMENT_NAME -ne "default") { exit 1 }
environment-activation-windows-cmd:
runs-on: windows-latest
defaults:
run:
shell: cmd
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/default/* .
- uses: ./
with:
activate-environment: true
- run: where python | findstr "\.pixi\\envs\\default\\python.exe"
- run: set | findstr CONDA_PREFIX
- run: |
if defined PIXI_ENVIRONMENT_NAME (
if "%PIXI_ENVIRONMENT_NAME%" NEQ "default" (
exit 1
)
)
environment-activation-false:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/default/* .
- uses: ./
with:
activate-environment: false
- run: '[[ $PIXI_ENVIRONMENT_NAME = "" ]]'
environment-activation-explicit-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/multiple-environments/* .
- uses: ./
with:
environments: py311
activate-environment: true
- run: which python | grep '.pixi/envs/py311/bin/python'
- run: '[[ $PIXI_ENVIRONMENT_NAME = "py311" ]]'
environment-activation-multiple-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Move pixi.toml
run: mv test/multiple-environments/* .
- uses: ./
with:
environments: >-
py311 py312
activate-environment: py311
- run: which python | grep '.pixi/envs/py311/bin/python'
- run: '[[ $PIXI_ENVIRONMENT_NAME = "py311" ]]'
# not properly testable
# https://github.com/actions/runner/issues/2347
# https://github.com/orgs/community/discussions/15452
+39 -13
View File
@@ -23,7 +23,7 @@ GitHub Action to set up the [pixi](https://github.com/prefix-dev/pixi) package m
## Usage
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
pixi-version: v0.21.1
cache: true
@@ -34,7 +34,7 @@ GitHub Action to set up the [pixi](https://github.com/prefix-dev/pixi) package m
> [!WARNING]
> Since pixi is not yet stable, the API of this action may change between minor versions.
> Please pin the versions of this action to a specific version (i.e., `prefix-dev/setup-pixi@v0.6.0`) to avoid breaking changes.
> Please pin the versions of this action to a specific version (i.e., `prefix-dev/setup-pixi@v0.7.0`) to avoid breaking changes.
> You can automatically update the version of this action by using [Dependabot](https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot).
>
> Put the following in your `.github/dependabot.yml` file to enable Dependabot for your GitHub Actions:
@@ -73,7 +73,7 @@ In order to not exceed the [10 GB cache size limit](https://docs.github.com/en/a
This can be done by setting the `cache-write` argument.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
cache: true
cache-write: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
@@ -118,7 +118,7 @@ test:
environment: [py311, py312]
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
environments: ${{ matrix.environment }}
```
@@ -128,7 +128,7 @@ test:
The following example will install both the `py311` and the `py312` environment on the runner.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
# separated by spaces
environments: >-
@@ -163,7 +163,7 @@ Specify the token using the `auth-token` input argument.
This form of authentication (bearer token in the request headers) is mainly used at [prefix.dev](https://prefix.dev).
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
auth-host: prefix.dev
auth-token: ${{ secrets.PREFIX_DEV_TOKEN }}
@@ -175,7 +175,7 @@ Specify the username and password using the `auth-username` and `auth-password`
This form of authentication (HTTP Basic Auth) is used in some enterprise environments with [artifactory](https://jfrog.com/artifactory) for example.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
auth-host: custom-artifactory.com
auth-username: ${{ secrets.PIXI_USERNAME }}
@@ -188,7 +188,7 @@ Specify the conda-token using the `conda-token` input argument.
This form of authentication (token is encoded in URL: `https://my-quetz-instance.com/t/<token>/get/custom-channel`) is used at [anaconda.org](https://anaconda.org) or with [quetz instances](https://github.com/mamba-org/quetz).
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
auth-host: anaconda.org # or my-quetz-instance.com
conda-token: ${{ secrets.CONDA_TOKEN }}
@@ -228,13 +228,39 @@ If you want to use PowerShell, you need to specify `-Command` as well.
> There are some custom shells provided by GitHub that have slightly different behavior, see [`jobs.<job_id>.steps[*].shell`](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell) in the documentation.
> See the [official documentation](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#custom-shell) and [ADR 0277](https://github.com/actions/runner/blob/main/docs/adrs/0277-run-action-shell-options.md) for more information about how the `shell:` input works in GitHub Actions.
### Environment activation
Instead of using a custom shell wrapper, you can also make all pixi-installed binaries available to subsequent steps by "activating" the installed environment in the currently running job.
To this end, `setup-pixi` adds all environment variables set when executing `pixi run` to `$GITHUB_ENV` and, similarly, adds all path modifications to `$GITHUB_PATH`.
As a result, all installed binaries can be accessed without having to call `pixi run`.
```yml
- uses: prefix-dev/setup-pixi@v0.7.0
with:
activate-environment: true
```
If you are installing multiple environments, you will need to specify the name of the environment that you want to be activated.
```yml
- uses: prefix-dev/setup-pixi@v0.7.0
with:
environments: >-
py311
py312
activate-environment: py311
```
Activating an environment may be more useful than using a custom shell wrapper as it allows non-shell based steps to access binaries on the path.
However, be aware that this option augments the environment of your job.
### `--frozen` and `--locked`
You can specify whether `setup-pixi` should run `pixi install --frozen` or `pixi install --locked` depending on the `frozen` or the `locked` input argument.
See the [official documentation](https://prefix.dev/docs/pixi/cli#install) for more information about the `--frozen` and `--locked` flags.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
locked: true
# or
@@ -253,7 +279,7 @@ The first one is the debug logging of the action itself.
This can be enabled by running the action with the `RUNNER_DEBUG` environment variable set to `true`.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
env:
RUNNER_DEBUG: true
```
@@ -271,7 +297,7 @@ The second type is the debug logging of the pixi executable.
This can be specified by setting the `log-level` input.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
# one of `q`, `default`, `v`, `vv`, or `vvv`.
log-level: vvv
@@ -297,7 +323,7 @@ If nothing is specified, `post-cleanup` will default to `true`.
On self-hosted runners, you also might want to alter the default pixi install location to a temporary location. You can use `pixi-bin-path: ${{ runner.temp }}/bin/pixi` to do this.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
post-cleanup: true
# ${{ runner.temp }}\Scripts\pixi.exe on Windows
@@ -313,7 +339,7 @@ You can also use a preinstalled local version of pixi on the runner by not setti
This can be overwritten by setting the `manifest-path` input argument.
```yml
- uses: prefix-dev/setup-pixi@v0.6.0
- uses: prefix-dev/setup-pixi@v0.7.0
with:
manifest-path: pyproject.toml
```
+7 -1
View File
@@ -21,8 +21,14 @@ inputs:
environments:
description: |
A space-separated list of environments to install. If not specified, only the default environment is installed.
activate-environment:
description: |
If the installed environment should be "activated" for the current job, modifying `$GITHUB_ENV` and
`$GITHUB_PATH`. If more than one environment is specified in `environments`, this must be the name of the
environment. Defaults to `false`. Requires at least pixi v0.21.0.
locked:
description: Whether to use `pixi install --locked`. Defaults to `true` when the lockfile is present, otherwise `false`.
description:
Whether to use `pixi install --locked`. Defaults to `true` when the lockfile is present, otherwise `false`.
frozen:
description: Whether to use `pixi install --frozen`. Defaults to `false`.
cache:
Generated Vendored
+371 -295
View File
File diff suppressed because it is too large Load Diff
Generated Vendored
+15
View File
@@ -9921,6 +9921,9 @@ var validateInputs = (inputs) => {
if (inputs.runInstall === false && inputs.environments) {
throw new Error("Cannot specify environments without running install");
}
if (inputs.activateEnvironment === "true" && inputs.environments && inputs.environments.length > 1) {
throw new Error("When installing multiple environments, `activate-environment` must specify the environment name");
}
};
var determinePixiInstallation = (pixiUrlOrVersionSet, pixiBinPath) => {
const preinstalledPixi = import_which.default.sync("pixi", { nothrow: true });
@@ -9972,6 +9975,16 @@ var inferOptions = (inputs) => {
if (!lockFileAvailable && inputs.cacheWrite === true) {
throw new Error("You cannot specify cache-write = true without a lock file present");
}
let activatedEnvironment;
if (inputs.activateEnvironment === "true") {
if (inputs.environments) {
activatedEnvironment = inputs.environments[0];
} else {
activatedEnvironment = "default";
}
} else if (inputs.activateEnvironment && inputs.activateEnvironment !== "false") {
activatedEnvironment = inputs.activateEnvironment;
}
const cache = inputs.cacheKey ? { cacheKeyPrefix: inputs.cacheKey, cacheWrite: inputs.cacheWrite ?? true } : inputs.cache === true || lockFileAvailable && inputs.cache !== false ? { cacheKeyPrefix: "pixi-", cacheWrite: inputs.cacheWrite ?? true } : void 0;
const frozen = inputs.frozen ?? false;
const locked = inputs.locked ?? (lockFileAvailable && !frozen);
@@ -9995,6 +10008,7 @@ var inferOptions = (inputs) => {
pixiLockFile,
runInstall,
environments: inputs.environments,
activatedEnvironment,
frozen,
locked,
cache,
@@ -10021,6 +10035,7 @@ var getOptions = () => {
manifestPath: parseOrUndefined("manifest-path", stringType()),
runInstall: parseOrUndefinedJSON("run-install", booleanType()),
environments: parseOrUndefinedList("environments", stringType()),
activateEnvironment: parseOrUndefined("activate-environment", stringType()),
locked: parseOrUndefinedJSON("locked", booleanType()),
frozen: parseOrUndefinedJSON("frozen", booleanType()),
cache: parseOrUndefinedJSON("cache", booleanType()),
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "setup-pixi",
"version": "0.6.0",
"version": "0.7.0",
"private": true,
"description": "Action to set up the pixi package manager.",
"scripts": {
+69
View File
@@ -0,0 +1,69 @@
import * as os from 'os'
import * as osPath from 'path'
import * as core from '@actions/core'
import { executeGetOutput, pixiCmd } from './util'
type ShellHook = {
environment_variables: Record<string, string>
}
const splitEnvironment = (shellHook: ShellHook): [Record<string, string>, string?] => {
if (os.platform() === 'win32') {
// On Windows, environment variables are case-insensitive but JSON isn't...
const pathEnvs = Object.keys(shellHook.environment_variables).filter((k) => k.toUpperCase() === 'PATH')
if (pathEnvs) {
const caseSensitivePathName = pathEnvs[0]
const path = shellHook.environment_variables[caseSensitivePathName]
delete shellHook.environment_variables[caseSensitivePathName]
return [shellHook.environment_variables, path]
}
} else {
if ('PATH' in shellHook.environment_variables) {
const path = shellHook.environment_variables.PATH
delete shellHook.environment_variables.PATH
return [shellHook.environment_variables, path]
}
}
// If the path cannot be found, return all other environment variables
return [shellHook.environment_variables]
}
const getNewPathComponents = (path: string): string[] => {
const currentPath = process.env.PATH
if (!currentPath) {
throw new Error('Unable to obtain current PATH from environment')
}
if (!path.endsWith(currentPath)) {
throw new Error('Unable to handle environment activation which does not only append to PATH')
}
core.debug(`Found current path '${currentPath}'`)
core.debug(`Got new path '${path}'`)
const newPath = path.slice(0, path.length - currentPath.length)
return newPath.split(osPath.delimiter).filter((p) => p.length > 0)
}
export const activateEnvironment = async (environment: string): Promise<void> => {
// First, obtain the environment variables that would be set by environment activation
const envOption = environment === 'default' ? '' : `-e ${environment}`
const shellHookOutput = await executeGetOutput(pixiCmd(`shell-hook ${envOption} --json`), { silent: true })
const shellHook: ShellHook = JSON.parse(shellHookOutput.stdout)
// Then, we split the environment variables into the special 'PATH' and all others
const [envVars, path] = splitEnvironment(shellHook)
// Finally, new path components are added...
if (path) {
const newComponents = getNewPathComponents(path)
for (const component of newComponents) {
core.info(`Adding path component '${component}'`)
core.addPath(component)
}
}
// ... as well as all remaining environment variables
for (const key of Object.keys(envVars)) {
core.info(`Exporting environment variable '${key}=${envVars[key]}'`)
core.exportVariable(key, envVars[key])
}
}
+6
View File
@@ -8,6 +8,7 @@ import type { PixiSource } from './options'
import { options } from './options'
import { execute, getPixiUrlFromVersion, pixiCmd } from './util'
import { saveCache, tryRestoreCache } from './cache'
import { activateEnvironment } from './activate'
const downloadPixi = (source: PixiSource) => {
const url = 'version' in source ? getPixiUrlFromVersion(source.version) : source.url
@@ -110,6 +111,8 @@ const generateList = async () => {
const generateInfo = () => core.group('pixi info', () => execute(pixiCmd('info')))
const activateEnv = (environment: string) => core.group('Activate environment', () => activateEnvironment(environment))
const run = async () => {
core.debug(`process.env.HOME: ${process.env.HOME}`)
core.debug(`os.homedir(): ${os.homedir()}`)
@@ -121,6 +124,9 @@ const run = async () => {
await pixiInstall()
await generateInfo()
await generateList()
if (options.activatedEnvironment) {
await activateEnv(options.activatedEnvironment)
}
}
run().catch((error) => {
+17
View File
@@ -15,6 +15,7 @@ type Inputs = Readonly<{
manifestPath?: string
runInstall?: boolean
environments?: string[]
activateEnvironment?: boolean | string
frozen?: boolean
locked?: boolean
cache?: boolean
@@ -71,6 +72,7 @@ export type Options = Readonly<{
pixiBinPath: string
auth?: Auth
postCleanup: boolean
activatedEnvironment?: string
}>
const logLevelSchema = z.enum(['q', 'default', 'v', 'vv', 'vvv'])
@@ -159,6 +161,9 @@ const validateInputs = (inputs: Inputs): void => {
if (inputs.runInstall === false && inputs.environments) {
throw new Error('Cannot specify environments without running install')
}
if (inputs.activateEnvironment === 'true' && inputs.environments && inputs.environments.length > 1) {
throw new Error('When installing multiple environments, `activate-environment` must specify the environment name')
}
}
const determinePixiInstallation = (pixiUrlOrVersionSet: boolean, pixiBinPath: string | undefined) => {
@@ -222,6 +227,16 @@ const inferOptions = (inputs: Inputs): Options => {
if (!lockFileAvailable && inputs.cacheWrite === true) {
throw new Error('You cannot specify cache-write = true without a lock file present')
}
let activatedEnvironment // default is undefined
if (inputs.activateEnvironment === 'true') {
if (inputs.environments) {
activatedEnvironment = inputs.environments[0]
} else {
activatedEnvironment = 'default'
}
} else if (inputs.activateEnvironment && inputs.activateEnvironment !== 'false') {
activatedEnvironment = inputs.activateEnvironment
}
const cache = inputs.cacheKey
? { cacheKeyPrefix: inputs.cacheKey, cacheWrite: inputs.cacheWrite ?? true }
: inputs.cache === true || (lockFileAvailable && inputs.cache !== false)
@@ -255,6 +270,7 @@ const inferOptions = (inputs: Inputs): Options => {
pixiLockFile,
runInstall,
environments: inputs.environments,
activatedEnvironment,
frozen,
locked,
cache,
@@ -289,6 +305,7 @@ const getOptions = () => {
manifestPath: parseOrUndefined('manifest-path', z.string()),
runInstall: parseOrUndefinedJSON('run-install', z.boolean()),
environments: parseOrUndefinedList('environments', z.string()),
activateEnvironment: parseOrUndefined('activate-environment', z.string()),
locked: parseOrUndefinedJSON('locked', z.boolean()),
frozen: parseOrUndefinedJSON('frozen', z.boolean()),
cache: parseOrUndefinedJSON('cache', z.boolean()),
+8 -2
View File
@@ -2,7 +2,8 @@ import type { BinaryLike } from 'crypto'
import { createHash } from 'crypto'
import * as os from 'os'
import * as core from '@actions/core'
import { exec } from '@actions/exec'
import type { ExecOptions } from '@actions/exec'
import { exec, getExecOutput } from '@actions/exec'
import { options } from './options'
export const getCondaArch = () => {
@@ -73,8 +74,13 @@ export const execute = (cmd: string[]) => {
return exec(cmd[0], cmd.slice(1))
}
export const executeGetOutput = (cmd: string[], options?: ExecOptions) => {
core.debug(`Executing: ${cmd.join(' ')}`)
return getExecOutput(cmd[0], cmd.slice(1), options)
}
export const pixiCmd = (command: string, withManifestPath = true) => {
let commandArray = [options.pixiBinPath].concat(command.split(' '))
let commandArray = [options.pixiBinPath].concat(command.split(' ').filter((x) => x !== ''))
if (withManifestPath) {
commandArray = commandArray.concat(['--manifest-path', options.manifestPath])
}