LinuxCommandLibrary

gh-variable

Manage GitHub repository variables

TLDR

List variables for the current repository

$ gh variable [[ls|list]]
copy

List variables for a specific organization
$ gh variable [[ls|list]] [[-o|--org]] [organization]
copy

Get a variable for the current repository
$ gh variable get [name]
copy

Set a variable for the current repository (user will be prompted for the value)
$ gh variable set [name]
copy

Set a variable for a deployment environment in the current repository
$ gh variable set [name] [[-e|--env]] [environment_name]
copy

Set an organization variable visible to all repositories
$ gh variable set [name] [[-o|--org]] [organization] [[-v|--visibility]] all
copy

Set multiple variables from a dotenv file
$ gh variable set [[-f|--env-file]] [path/to/file.env]
copy

Delete a variable for the current repository
$ gh variable delete [name]
copy

SYNOPSIS

gh variable <list|set|delete> [NAME] [VALUE] [<flags>]

PARAMETERS

--env <string>
    Target environment name (production, staging, development)

--org <string>
    Operate on organization variables

--repo [<OWNER/>]<NAME>
    Operate on specific repository

--limit <int>
    Maximum variables to list (default 30, for list)

--show-vals
    Display variable values (for list)

--json [<fields>]
    Output as JSON (for list)

--body-path <file>
    Read variable value from file (for set)

DESCRIPTION

gh variable is a subcommand of the GitHub CLI (gh) for managing environment variables in GitHub repositories, organizations, and environments. It enables listing, creating, updating, and deleting variables used in GitHub Actions workflows via command line, avoiding the web UI.

Variables store non-sensitive data like API keys or config values, visible to repository collaborators (unlike encrypted secrets). Target specific contexts with --repo, --org, or --env (e.g., production, staging). List variables with optional JSON output or value display; set from direct input, stdin, or files; delete precisely.

Ideal for CI/CD automation, scripting deployments, and team workflows. Requires authentication via gh auth login. Supports pagination for large sets.

CAVEATS

Requires GitHub CLI installed and user authenticated. Variables are unencrypted and visible to repo/org collaborators. No bulk operations; use scripts for multiples. Environment support limited to named ones.

EXAMPLES

Set variable: gh variable set MY_API_KEY "value" --repo owner/repo --env production
List: gh variable list --repo owner/repo --show-vals
Delete: gh variable delete MY_VAR --org myorg

VALUE INPUT

For set, omit VALUE to read from stdin: echo "secret" | gh variable set KEY --body-path - or use file with --body-path file.txt.

HISTORY

Part of GitHub CLI v1.0.0 (Dec 2020). Environment targeting added in v2.0+ (2022). JSON output and limits improved in recent updates for better scripting.

SEE ALSO

gh(1), gh-secret(1), git(1)

Copied to clipboard