@initx-plugin/env
v0.4.4
Published
initx plugin to manage env vars and secrets from private GitHub or GitLab repositories
Maintainers
Readme
@initx-plugin/env
initx plugin to manage env vars and secrets from a private GitHub or GitLab repository.
One private repository holds one directory per store (stores/<name>/env.json). Pull a store into a
local .env, inject it into a child process or CI job, or write it into GitHub Actions secrets.
pnpm add -D @initx-plugin/envThe vault is the source of truth. Storage and execution platforms are independent: GitLab CI can
read a GitHub vault, and a GitLab vault can supply GitHub Actions secrets via apply. GitHub
Actions secrets are write-only; this plugin projects values from the vault into that target.
GitLab CI/CD Variables synchronization is a separate future target, not part of this release.
Setup
npm i -g initx # optional, otherwise use npx
pnpm add -D @initx-plugin/env
initx env-config set repo <owner/repo> # the private repo that holds your stores
initx env-config set token <pat> # classic PAT with the `repo` scope is enough
initx env-config set path stores # optional, default: stores
initx env-config status # prints the config (token masked) + checks connectivityThe config lives in ~/.initx/stores/@initx-plugin/env/store.json. The token is stored in
plaintext there (same trade-off as @initx-plugin/cp) — set INITX_ENV_TOKEN instead if you would
rather keep it out of a file.
Project configuration (GitHub or GitLab)
Install this package in the business project. initx discovers installed @initx-plugin/* and
initx-plugin-* packages directly, so no initx plugin add env step is needed:
pnpm add -D @initx-plugin/envCommit a non-sensitive initx-env.config.ts in the business project's working directory:
import { defineConfig } from '@initx-plugin/env'
export default defineConfig({
schema: 1,
storage: {
type: 'gitlab',
apiBaseUrl: 'https://gitlab.example.com/api/v4',
repo: 'example-group/subgroup/env-vault',
branch: 'main',
path: 'stores'
}
})type and repo are required. GitLab accepts a numeric project ID (a string such as '12345')
or the full namespace, including multiple subgroups. apiBaseUrl means the API base, including
/api/v4 for GitLab or /api/v3 for GitHub Enterprise. Defaults are
https://gitlab.com/api/v4 and https://api.github.com. Branch defaults to main, directory to
stores; "path": "" explicitly selects repository root. For a GitHub vault, use "type": "github"
and "repo": "owner/vault". Examples are in examples.
The vault project/repository must already exist, but it can be empty. The first env push <store>
or env set <store> K=V creates the configured branch (default main) and the store file in one
commit, on both GitHub and GitLab. No README or manual branch creation is required. list and
env-config status report zero stores for a confirmed empty vault without writing anything.
An initialized vault with a missing configured branch still fails. The token must have permission
to create the first commit; GitLab instance rules for the initial/default branch may require a
Maintainer even when a Developer can write to other branches.
All storage commands and env-config status accept --config <file>. Relative paths resolve
against the caller's cwd. Without the flag, only cwd's initx-env.config.ts is checked; parents
are never searched. An explicitly missing file, invalid module, unknown schema or unknown field
fails instead of falling back. Project files cannot contain tokens, credentials or vars; business
values belong to the remote store (whose schema remains 1). Config files are only read.
initx env-config status --config initx-env.config.ts --offline
initx env run wukong-content-production --config initx-env.config.ts -- npm run pipeline
# When running from an isolated HOME/cwd, pass the business config's absolute path.
initx env run demo --config /workspace/business/initx-env.config.ts -- node /workspace/business/task.mjsThe config's location never changes the child cwd. npm run pipeline above is an example business
entry point, implemented by the consuming project. Set only INITX_ENV_TOKEN in CI; tokens and
business values do not belong in this file. Existing Slides invocations using INITX_ENV_REPO,
INITX_ENV_BRANCH, INITX_ENV_PATH and a GitHub token continue to work without new flags.
For local persisted GitLab settings (independent of a project file):
initx env-config set type gitlab
initx env-config set apiBaseUrl https://gitlab.example.com/api/v4
initx env-config set repo example-group/subgroup/env-vault
# Prefer INITX_ENV_TOKEN; if saving a token locally, select type and API address first.
initx env-config set token <gitlab-access-token>Changing local type/API clears the old repo/branch/path to prevent accidental reuse. New persisted tokens are keyed by provider and normalized full API base (including its path), not just hostname. Legacy flat config remains GitHub config; its token is accepted only on the default GitHub API.
Commands
| Command | What it does |
| --- | --- |
| initx env list | List the stores in the vault repo |
| initx env pull [store] | Pull a store into a local .env (asks which store / which file when ambiguous) |
| initx env set <store> K=V ... | Write variables into a store (the store name is the file name) |
| initx env push <store> | Update a store from a local .env |
| initx env setup [store] | Inject a store into this environment (CI job or shell) |
| initx env run <store> -- <command> [...args] | Inject a store directly into one child process and return its exit status |
| initx env apply <store> --repo o/r | Write a store into a repo's GitHub Actions secrets |
| initx env file add <store> KEY --file <path> | Upload a file and replace the value with ${file-<id>} |
| initx env file ls/cat/pull/rm/check | Inspect, materialize, delete and verify file objects |
Command failures print only the error message through logger.error and exit with code 1, without a stack
trace or minified source dump. A failed connectivity check in env-config status also exits
with code 1. env run continues to forward the child's own exit status, signals and output.
Terminal messages use the existing utils color builders: errors are red, successful changes
green, warnings/modified keys yellow, and configuration values/store names blue. Message-body
colors are disabled when redirected or when NO_COLOR/FORCE_COLOR=0 is set; FORCE_COLOR=1
enables them explicitly. JSON, exported variable values and child output are not colorized.
# write / update a store
initx env set mail-peon-publish EXTENSION_ID=abc CHROME_REFRESH_TOKEN=1//0g...
# pull it into a local .env (keeps your other keys, comments and order)
initx env pull mail-peon-publish
initx env pull mail-peon-publish --to .env.local --dry-run
# push local changes back
initx env push mail-peon-publish --from .env.local
initx env push mail-peon-publish --prune # also drop remote keys missing locally
# run a script with the store's original values (including real newlines)
initx env run mail-peon-publish -- node scripts/publish.mjsRun a command locally or in CI
initx env run demo -- node scripts/check.mjs
initx env run demo -- node --version
initx env run acewill-slidev-production -- pnpm deploy
initx env run demo -- bash scripts/deploy.shThe store and the first -- are required. Put optional --token <token> and --config <file> before --;
every argument after it belongs to the child, including --help, --version, --token, another
--, empty strings and arguments with spaces. Extra positional arguments and other plugin flags
(including --repo, --json, --stdout, --only, --shell, --cwd) are rejected before reading
the vault. Help before -- is handled by initx; help after it is passed to the child.
The child inherits the caller's working directory, stdin, stdout, stderr and environment. Store
variables override inherited values without changing the calling shell. Empty values, CRLF, real
newlines, Unicode and punctuation are preserved; values are never expanded. Empty stores are valid.
Variable names must match ^[A-Za-z_][A-Za-z0-9_]*$, and values cannot contain NUL. All values are
validated before starting the child. Windows names are case insensitive: a store's PATH replaces
an inherited Path, and conflicting store names such as FOO / foo abort. Other commands retain
their existing name validation rules.
Arguments are passed as an array. The plugin does not interpret shell expressions, pipelines or
redirection. Explicitly run a shell or, preferably, write a script for multiple operations. One
run affects only that child and its descendants; the next independent CI command does not inherit
the fetched store. cross-spawn is the runtime dependency used to resolve executables and escape
Windows npm/pnpm .cmd launchers without enabling shell: true for every command
(upstream behavior).
Normal exits preserve the child's code, including nonzero codes. Missing commands return 127,
permission failures return 126, and parameter/configuration/read/validation/other startup failures
return 1. On POSIX, signal termination is reproduced after initx has finished its store cleanup,
with a nonzero 128 + signal number fallback. Parent SIGINT/SIGTERM events are forwarded once to
the direct child, with a 5-second grace period followed by best-effort SIGKILL. Listeners are removed
when execution finishes. Background processes detached from the direct child are outside this
contract; deployment scripts should stay in the foreground.
On Windows, Node's process termination APIs do not deliver catchable POSIX signals: forwarding
SIGINT/SIGTERM can terminate the direct child immediately, and the wrapper returns 130/143. A
console Ctrl+C event received by Node uses the same forwarding handler. .cmd launchers can have
descendant processes; this version does not guarantee termination of that whole tree. Windows
tests exercise the real direct-child termination and listener cleanup by triggering the parent's
signal event over IPC; interactive console Ctrl+C still needs a manual console check.
run writes no .env, $GITHUB_ENV, dotenv artifact or secret cache, and emits no GitHub masking
protocol. Its own diagnostics use the utils logger without values, tokens, complete response bodies or
command argument lists. Child output is inherited without masking: a script can still print
secrets. The parent environment, including inherited GitHub tokens, is retained. This is process
injection for trusted scripts and stores, not an isolation sandbox or encrypted secret manager.
The stdout form of setup remains intended for inspection/legacy use; use run for exact values.
File-backed variables
Some values are files: SSH keys, certificates, .p12 keystores, service-account JSON. Storing
them as one long base64 string makes a store unreadable and unreviewable. A store can keep the
file next to itself and put a short reference in vars:
{
"schema": 1,
"vars": { "DEPLOY_KEY": "${file-deploy-key}" },
"files": {
"deploy-key": {
"sha256": "…", // content hash, used for verification and push comparison
"bytes": 3243,
"encoding": "utf8", // utf8 (inject text) or base64 (inject base64 text)
"mode": "600" // only used by `file pull`
}
}
}The file lives at <path>/<store>/_files/<id>; the path is derived, not stored. encoding
controls the injected form only — the file is always stored as raw bytes.
Rules:
- A reference must be the whole value.
prefix${file-x}is not expanded, and${...}is never otherwise expanded. - The id must be declared in
files. An undeclared reference is a hard error; silently injecting the literal${file-x}as a secret is worse than failing. pull/setup/run/apply/--jsonresolve references.pullwrites real values;setupmasks the resolved values;runinjects them directly without a shell.pushcompares local content withbytes+sha256without downloading. If it differs, the command refuses by default: use--update-filesto re-upload and keep the reference, or--inlineto turn the key into a literal and drop the file entry.file pullwrites raw bytes and appliesmodeon POSIX. It does not overwrite existing files unless--forceis given.file checkverifies four directions: missing/size/sha drift, undeclared references, orphan files, and metadata not referenced by any key.
initx env file add acewill-slidev DEPLOY_KEY --file ~/.ssh/deploy_key
initx env file add acewill-ios IOS_P12 --file ./dist.p12 --encoding base64 --mode 600
initx env file ls acewill-slidev
initx env file cat acewill-slidev DEPLOY_KEY --show
initx env file pull acewill-slidev --to-dir ./.secrets
initx env file check acewill-slidev
initx env file rm acewill-slidev DEPLOY_KEY--file - reads the content from stdin. file check exits nonzero when it reports a problem, so it
can be used as a CI gate.
Limits: 10 MB per file (INITX_ENV_MAX_FILE_BYTES), 1 MiB per value when pull writes .env
(INITX_ENV_MAX_INLINE_BYTES), 32 KB per value for setup (hard failure), and 48 KB per GitHub
Actions secret. Large files should be materialized with file pull and passed as paths; do not
put them into environment variables. run maps E2BIG to the same advice.
There is no extract and no gc. Existing base64 values are migrated by hand: decode one to a
temporary file, run file add ... --encoding base64, then file check. Orphans left by a failed
rm or an --inline are reported by file check.
Configuration precedence
| Setting | Highest to lowest priority |
| --- | --- |
| Provider | Explicit override → INITX_ENV_TYPE → project storage.type → local storage type → github |
| API base | Explicit override → INITX_ENV_API_BASE_URL → project API → same-provider local API → provider default |
| Token | Explicit override / --token → INITX_ENV_TOKEN → GH_TOKEN → GITHUB_TOKEN (last two only for default GitHub API) → scoped local token |
| Vault repo | Explicit override → INITX_ENV_REPO → project storage.repo → same-provider/API local repo |
| Branch | Explicit override → INITX_ENV_BRANCH → project branch → same-provider/API local branch → main |
| Store path | Explicit override → INITX_ENV_PATH → project path → same-provider/API local path → stores |
Empty strings fall back to the next source. Environment settings affect the current invocation
only and are never written back to the plugin store. env-config status --offline shows the
effective settings with a masked token; get / list show the persisted settings. env apply
--repo o/r still selects the destination Actions repository, not the vault. run has no --repo.
The provider is resolved first; values from another provider/API are never inherited. GH_TOKEN
and GITHUB_TOKEN are never sent to GitLab or custom API bases. --token and INITX_ENV_TOKEN
explicitly authorize the selected current target. Authenticated requests do not follow redirects,
have a 30-second timeout, and are not automatically retried (a failed write may have committed).
Status shows per-field sources, provider, API base, repo/ref/path, config file and fully masked
token source. get/list/set/rm operate only on persisted local settings, never project files.
Use in GitLab CI
Preinstall fixed versions of initx and this plugin in the CI image, or prepare them with an explicit
noninteractive installation step. Do not rely on a first-run plugin installation prompt. This
implementation is smoke-tested with initx 0.4.3; compatible versions must expose the untouched
tail as ctx.cliOptions['--']: string[], keep child flags out of prefix help/version handling, and
preserve process.exitCode after handlers finish. Older versions have not been verified.
Commit initx-env.config.ts as above and set a protected, masked INITX_ENV_TOKEN in GitLab. For a
GitLab vault, use a Personal/Project Access Token with read_api and access to the vault project;
local writes require api plus branch permissions. Check both Files and Tree access on your
instance. For a GitHub vault, use Contents: read access. Runner platform does not select storage.
pipeline:
script:
- initx env run wukong-content-production --config "$CI_PROJECT_DIR/initx-env.config.ts" -- npm run pipelineThis illustrates the integration point; prepare pinned initx/plugin versions noninteractively in
your CI image first. This feature is not published by this change: use a tested local build until
a separate release exists, then pin that released version. Let the business script orchestrate its
own workflow; the plugin does not hard-code business variable names or perform deployments. Scripts may
materialize multiline SSH keys as needed, set file permissions and clean them up; run does not
create key files or pass them through artifacts/cache.
Fetched variables are not automatically registered as GitLab masked variables. Disable command tracing that could print them. Vault read permissions apply to the entire repository, not to a store filename: use separate repositories for separate trust boundaries. The private JSON store and its Git history remain plaintext.
Use in GitHub Actions
The built-in GITHUB_TOKEN can only read the repository the workflow runs in, so a separate vault
repo needs its own token (one secret):
- run: npm install --no-save initx @initx-plugin/env
# inject into this job: every later step sees $EXTENSION_ID / $CHROME_REFRESH_TOKEN
- run: npx --yes initx env setup mail-peon-publish --token "${{ secrets.ENV_VAULT_TOKEN }}"
# or write them into the repo's Actions secrets once (needs secrets write permission)
- run: npx --yes initx env apply mail-peon-publish --repo "${{ github.repository }}"
env:
INITX_ENV_TOKEN: ${{ secrets.ENV_VAULT_TOKEN }}env setup writes to $GITHUB_ENV and emits ::add-mask:: for every value before it does, so the
values do not show up in logs. Values with newlines use the heredoc (KEY<<EOF) form.
Apply to GitHub Actions
apply --repo owner/target always selects the destination GitHub repository. Its vault is resolved
independently via --config/environment/local settings. For a GitLab or custom-host source, set
INITX_ENV_TARGET_TOKEN (recommended for CI) or --target-token <github-token> with Secrets write
access to the target. The source token never crosses providers/hosts. GH_TOKEN/GITHUB_TOKEN
are not implicit target credentials for these cross-platform calls. --dry-run also validates
that independent target credentials are configured, but makes no target API call.
initx env apply demo --config initx-env.config.ts --repo owner/target --dry-run
# Same invocation without --dry-run writes GitHub Actions secrets.
# Supply INITX_ENV_TOKEN for the vault and INITX_ENV_TARGET_TOKEN for the target in the environment.Existing default GitHub API vault → default GitHub Actions calls may continue reusing the source
token. Explicit target credentials take precedence. targets.secrets retains its schema-1 shape.
GitLab CI Variables writing and CI_JOB_TOKEN support are deferred. Deploy Tokens used for git clone
are not Files/Tree API tokens.
Storage guarantees and GitLab compatibility
Commands use an explicit adapter factory; the store service deals only in UTF-8 content and opaque
version values. GitHub uses the file SHA; GitLab uses the file's last_commit_id, never Tree blob
IDs. Creates are explicit, updates/deletes carry the expected version. Conflict responses are
verified by a safe reread; GitHub 422 and GitLab 400 are not automatically conflicts. GitLab's
unchanged-content 400 is accepted only if the file still has the expected version and exact content.
push --force rereads and retries once; permission/validation/network errors do not trigger it.
The existing force semantics overwrite using the locally prepared store, including its routing
hints; concurrent remote-only changes can be replaced. Pull/review before force if those matter.
GitLab uses Files GET/POST/PUT, a conditional Commits delete action, and Tree offset pagination
(per_page=100). It supports full subgroups and numeric IDs, and does not require keyset pagination, newer Tree metadata or job-token
features. Missing paths are empty only after validating repository/ref access or confirming an
empty repository (GitLab project empty_repo, GitHub's explicit empty-repository response). Tree accepts both
legacy 200 empty arrays and newer 404 responses. Directory listing is not recursive. Errors expose
only a category, safe hint and HTTP status, never upstream response text or request URLs.
The implementation targets GitLab CE 15.9.4, with mock coverage for its API behavior. This is not a claim of real-instance acceptance. Use a temporary vault with non-sensitive dummy values to verify read_api Files/Tree/commit access, pagination, encoded refs/paths, protected-branch errors, create races and stale update/delete rejection on your actual instance. Record token type, scope, role and instance version. See implementation and acceptance record. API references: Files, Tree, Deploy Token limitations.
One deliberate change from the initial plan: 15.9.4's Files DELETE does not declare
last_commit_id, so it discards that condition. Deletion therefore uses the existing Commits API
with one delete action containing last_commit_id; it does not rely on a client-side precheck.
The versioned Files route,
Commits route and
MultiService check
provide the compatibility basis; real-instance testing remains required.
The known legacy protected-branch 400 message is recognized as permission failure using an exact literal from v15.9.4 CreateService. Arbitrary response messages are never forwarded to the caller or printed.
Store format
// stores/mail-peon-publish/env.json
{
"schema": 1,
"vars": {
"EXTENSION_ID": "abcdefghijklmnopabcdefghijklmnop",
"CHROME_REFRESH_TOKEN": "1//0g...",
"DEPLOY_KEY": "${file-deploy-key}"
},
"files": { // optional: file-backed variables
"deploy-key": { "sha256": "…", "bytes": 3243, "encoding": "utf8", "mode": "600" }
},
"targets": { // optional routing hints
"envFiles": [".env.local"], // pull: skip the file picker
"secrets": [{ "repo": "mail-peon/mail-peon", "env": null }] // apply: default destination
}
}Each store is a directory: stores/<store>/env.json plus stores/<store>/_files/<id>.
Good to know
- Values are stored as plaintext JSON in v1. The repo is private, but anything committed stays in git history forever — rotate the values if that repo ever becomes public. Encryption is planned and will be orthogonal to the command surface (dotenvx / sops + age).
- File contents are plaintext too.
filesonly moves a large value out of the JSON; it does not encrypt it.file checkdetects size/sha drift, orphan files and metadata not used by any key. - No local cache. Values are fetched on every invocation.
pullwrites its target.envfile and backup;setupmay write$GITHUB_ENV;rundoes not persist fetched values. applyconstraints (GitHub's, not ours): secret values are write-only; a single secret is capped at 48 KB; names may only contain[A-Za-z0-9_], cannot start with a digit orGITHUB_, and are stored uppercased. Illegal names abort the command unless you pass--skip-invalid.- Token permissions: GitHub vaults need Contents read/write; GitLab vaults use
read_apifor reads orapifor writes, plus project/branch access.applyneeds Secrets: write on the GitHub target. Default GitHub API calls can share a classic PAT withrepo; other sources use independent target credentials. Organization secrets are not supported. - Output is masked: tokens print as
***, values are never printed bylist/get/status.
Development
Do not install the initx CLI as a dependency or test tool in this project. Use the existing
core/utils SDKs, mocks and command-handler fixtures. Historical CLI checks and business CI
installation examples do not change this rule; see AGENTS.md.
pnpm install
pnpm stub # unbuild --stub, then: initx plugin add .
pnpm test # vitest
pnpm build
pnpm lint
pnpm typecheckTo debug a GitLab request with an existing local initx installation, run pnpm stub and
ix plugin add . in this plugin directory. This links the plugin, without installing the CLI
in the project. Leave stub mode enabled while diagnosing; source changes then take effect directly.
The switch comes from initx's context (ctx.cliOptions.debug or ctx.cliOptions.d). In the
business project, repeat your original command with initx's existing global debug option:
ix env push my-store --token <gitlab-token> --debugPlugin debug messages use logger.debug and retain only empty-vault initialization, write results,
failed requests, and permission summaries. Routine read requests, expected missing-file/ref responses,
and successful project lookups are silent. No URLs, tokens, request/response
bodies, commit messages or variable values are logged. For a branch-write permission failure,
debug mode makes additional read-only project/branch/protection requests; failed diagnostics
preserve the original error and never retry the write. accessLevel: 30 means Developer and
40 means Maintainer. allowedPushLevels lists numeric levels from the exact-name protection
rule, when readable. A missing/unreadable rule does not rule out wildcard or initial default-branch
policies. For an empty project, creating the first branch is still subject to GitLab permissions.
In GitLab CE 15.9.4, default-branch protection can apply before the first branch exists:
ProtectedBranch.protected? treats an empty project with default-branch protection as protected.
If a Developer token is denied on the first commit, use a Maintainer token with api, or have
a project maintainer configure the intended initial-branch policy. A failed protection lookup
(403) means the token cannot inspect the rule; it does not mean there is no protection.
See the versioned ProtectedBranch model.
All human-readable messages use the existing utils logger (which currently writes to stdout).
Raw JSON, shell exports and GitHub Actions protocol lines remain data output without logger prefixes;
the child's own stdout/stderr and exit status remain unchanged.
When env run cannot read a store, --debug (or -d) additionally reports the resolved backend,
whether configuration came from a project file, and the token source name (never its value).
Normal output keeps the error reason and HTTP status without configuration metadata.
Malformed store JSON and schema failures have separate hints. Schema hints
include only fixed field labels such as vars or targets.secrets, never variable names,
store values, raw response bodies or arbitrary exception text. An explicit missing --config
file fails during configuration and does not silently fall back to GitHub.
ix env run acewill-slidev-production --config initx-env.config.ts --debug -- node -e "console.log('ENV_RUN_OK')"Put the debug option before --; arguments after -- belong to the child process.
pnpm test runs unit tests and isolated child-process fixtures with fake GitHub/GitLab responses and
temporary directories. Tests use the existing core matcher and direct command handlers, without
installing or importing the initx CLI. Child-process tests check execution markers and nonzero
exit codes. No real vault, DNS, SSH or
GitHub/GitLab write endpoint is used. The Linux/Windows CI matrix runs the same checks. Platform-specific
coverage and the local verification record are in env run implementation.
License
MIT
