dx-org-devhub-configure

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Enable Dev Hub & View Scratch Org Allocation

启用Dev Hub并查看Scratch Org配额

Enable Dev Hub on a Salesforce org, verify whether it is already on, and see the org's scratch org allocation (Active and Daily limits and how many remain) — for the default org or a named org. The skill can also configure Dev Hub sub-preferences (packaging, org shape export, scratch org snapshots) and list the active scratch orgs created from a Dev Hub.
Dev Hub is the org feature that lets you create and manage scratch orgs and second-generation (2GP) / unlocked packages.
在Salesforce组织中启用Dev Hub,验证其是否已开启,并查看组织的scratch org配额(Active和Daily限制及剩余数量)——针对默认组织或指定名称的组织。该技能还可配置Dev Hub子偏好设置(打包、组织形态导出、scratch org快照),并列出从Dev Hub创建的活跃scratch org。
Dev Hub是一项组织功能,允许您创建和管理scratch org以及第二代(2GP)/解锁包

When to use

适用场景

Trigger on requests like:
  • "Enable Dev Hub" / "Turn on Dev Hub" / "Set up this org for scratch orgs."
  • "Is Dev Hub enabled on my org?" / "Check if Dev Hub is on."
  • "How many scratch orgs can I create?" / "What's my scratch org allocation / limit?" / "How many scratch orgs do I have left?"
  • "Turn on packaging / 2GP / unlocked packages" / "Enable org shape export" / "Enable scratch org snapshots" (Dev Hub sub-preferences).
  • "List the scratch orgs on my Dev Hub."
在以下请求触发此技能:
  • "启用Dev Hub" / "开启Dev Hub" / "配置此组织以创建scratch org"
  • "我的组织是否已启用Dev Hub?" / "检查Dev Hub是否开启"
  • "我可以创建多少个scratch org?" / "我的scratch org配额/限制是多少?" / "我还剩多少个scratch org额度?"
  • "开启打包/2GP/解锁包" / "启用组织形态导出" / "启用scratch org快照"(Dev Hub子偏好设置)
  • "列出我的Dev Hub中的scratch org"

When NOT to use

不适用场景

Do not trigger this skill for:
  • Creating or deleting an individual scratch org — that is
    sf org create scratch
    /
    sf org delete scratch
    , a different workflow. This skill enables the Dev Hub feature and reports allocation; it does not create scratch orgs.
  • Switching the default/active org — use
    dx-org-switch
    .
  • Trial or org expiration dates ("when does my org expire") — use
    dx-org-trial-expiration-check
    .
请勿在以下场景触发此技能:
  • 创建或删除单个scratch org——对应
    sf org create scratch
    /
    sf org delete scratch
    ,属于不同工作流。本技能仅启用Dev Hub功能并报告配额,不负责创建scratch org。
  • 切换默认/活跃组织——请使用
    dx-org-switch
  • 查询试用版或组织到期日期(如“我的组织何时到期”)——请使用
    dx-org-trial-expiration-check

How Dev Hub enablement is determined

Dev Hub启用状态的判定方式

The standard object
ScratchOrgInfo
is provisioned and becomes queryable only when Dev Hub is enabled. So a successful
SELECT COUNT() FROM ScratchOrgInfo
means Dev Hub is ON; an
INVALID_TYPE
error means it is OFF. This is the reliable signal.
Do not infer enablement from
sf org list limits
alone: it reports
ActiveScratchOrgs
/
DailyScratchOrgs
rows even on orgs where Dev Hub is off, so limits show allocation but not enablement.
标准对象**
ScratchOrgInfo
仅在Dev Hub启用后才会被配置并可查询。因此,
SELECT COUNT() FROM ScratchOrgInfo
执行成功意味着Dev Hub已
开启**;若返回
INVALID_TYPE
错误则表示Dev Hub已关闭。这是最可靠的判定信号。
请勿仅通过
sf org list limits
推断启用状态:即使在Dev Hub未开启的组织中,该命令仍会返回
ActiveScratchOrgs
/
DailyScratchOrgs
数据,因此仅能显示配额,无法反映启用状态。

How enabling works (important)

启用机制说明(重要)

  • The Dev Hub master switch is the deployable Metadata API field
    DevHubSettings.enableScratchOrgManagementPref
    . There is no
    enableDevHub
    field and no dedicated
    sf
    command to turn it on — deploying this setting is exactly what the Setup toggle does.
  • Enabling is irreversible — once on, Dev Hub cannot be turned off.
  • Requires a user with ModifyAllData or ModifyMetadata (a System Administrator has these). A user without them gets
    INSUFFICIENT_ACCESS
    .
  • Cannot be enabled in a sandbox, or in an org that has a registered namespace.
  • Available in Developer, Enterprise, Performance, Unlimited, and trial editions.
  • Dev Hub的主开关是可部署的Metadata API字段**
    DevHubSettings.enableScratchOrgManagementPref
    **。不存在
    enableDevHub
    字段,也没有专门的
    sf
    命令用于开启Dev Hub——部署此设置与在Setup界面中切换开关的效果完全一致。
  • 启用操作不可逆——一旦开启,Dev Hub无法关闭。
  • 权限要求:用户需拥有ModifyAllDataModifyMetadata权限(系统管理员默认拥有这些权限)。无权限用户会收到
    INSUFFICIENT_ACCESS
    错误。
  • 不可在沙箱中启用,也无法在已注册命名空间的组织中启用。
  • 支持的版本:Developer、Enterprise、Performance、Unlimited试用版

Steps

操作步骤

  1. CRITICAL: Run the bundled helper script, which handles Dev Hub detection, the enablement deploy (with a safe dry-run default), allocation math, and structured output, and works on macOS and Linux. Always invoke it by absolute path from the skill directory — never
    ./scripts/
    , which resolves against the user's current directory and will either run the wrong script or fail. The script self-checks this: if
    $0
    is not absolute it exits with a usage error, so pass the full path to
    <skill_dir>
    (the directory containing this SKILL.md).
    bash
    bash "<skill_dir>/scripts/devhub.sh" <alias-or-username>            # status: on? + allocation
    bash "<skill_dir>/scripts/devhub.sh"                               # default org (target-org)
    bash "<skill_dir>/scripts/devhub.sh" <org> --allocation           # allocation only
    bash "<skill_dir>/scripts/devhub.sh" <org> --enable               # validate enabling (dry run)
    bash "<skill_dir>/scripts/devhub.sh" <org> --enable --apply       # actually enable (irreversible)
    bash "<skill_dir>/scripts/devhub.sh" <org> --list-scratch         # list active scratch orgs
    bash "<skill_dir>/scripts/devhub.sh" <alias> --instance-url <url> --enable --apply  # log in first, then enable
    bash "<skill_dir>/scripts/devhub.sh" <url>                        # bare instance URL: log in to it
    When the user supplies an instance URL (their My Domain, sandbox, or pre-release/scratch login URL — e.g.
    https://my-domain.my.salesforce.com
    ), pass that exact URL with
    --instance-url <url>
    — the script accepts Salesforce-owned hostnames and logs in with
    sf org login web --instance-url <url>
    and then continues. A bare URL positional is accepted as shorthand for the same thing.
    <skill_dir>
    is the absolute path to the directory containing this SKILL.md.
  2. For enablement, default to a dry run first. Run
    --enable
    (no
    --apply
    ) to validate the deploy, then relay the result. Because enabling is irreversible, only run
    --enable --apply
    when the user has clearly asked to actually enable Dev Hub. Applied operations require an explicit org alias or username; the script refuses to mutate an implicit default org. If the deploy is rejected for permissions, surface the Setup-UI fallback it prints.
  3. Relay the script output. When an org can't be queried, surface the
    sf org login web
    command the script prints so the user can authenticate. Pick optional flags based on the request (see below):
    --allocation
    for limits only,
    --list-scratch
    to enumerate scratch orgs,
    --packaging
    /
    --snapshots
    /
    --shape
    to turn on sub-preferences,
    --json
    for automation.
  1. 关键操作:运行附带的辅助脚本,该脚本可处理Dev Hub检测、启用部署(默认安全试运行)、配额计算和结构化输出,支持macOS和Linux系统。请始终通过绝对路径从技能目录调用该脚本——切勿使用
    ./scripts/
    ,因为该路径会基于用户当前目录解析,可能导致运行错误脚本或执行失败。脚本会自行检查路径:若
    $0
    不是绝对路径则会退出并提示使用错误,因此请传入
    <skill_dir>
    的完整路径(即包含此SKILL.md文件的目录)。
    bash
    bash "<skill_dir>/scripts/devhub.sh" <alias-or-username>            # 状态查询:是否开启+配额
    bash "<skill_dir>/scripts/devhub.sh"                               # 默认组织(target-org)
    bash "<skill_dir>/scripts/devhub.sh" <org> --allocation           # 仅查询配额
    bash "<skill_dir>/scripts/devhub.sh" <org> --enable               # 验证启用操作(试运行)
    bash "<skill_dir>/scripts/devhub.sh" <org> --enable --apply       # 实际启用(不可逆)
    bash "<skill_dir>/scripts/devhub.sh" <org> --list-scratch         # 列出活跃scratch org
    bash "<skill_dir>/scripts/devhub.sh" <alias> --instance-url <url> --enable --apply  # 先登录,再启用
    bash "<skill_dir>/scripts/devhub.sh" <url>                        # 直接传入实例URL:登录该组织
    当用户提供实例URL(如My Domain、沙箱或预发布/scratch登录URL——例如
    https://my-domain.my.salesforce.com
    )时,请将该URL与
    --instance-url <url>
    一起传入——脚本接受Salesforce官方主机名,并通过
    sf org login web --instance-url <url>
    登录后继续执行。直接传入URL作为位置参数是简化写法。
    <skill_dir>
    是包含此SKILL.md文件的目录的绝对路径。
  2. 启用操作默认先执行试运行。运行
    --enable
    (不带
    --apply
    )验证部署操作,然后反馈结果。由于启用操作不可逆,仅当用户明确要求实际启用Dev Hub时,才运行
    --enable --apply
    。执行修改操作需要指定明确的组织别名或用户名;脚本拒绝修改隐式默认组织。若因权限不足导致部署被拒绝,请反馈脚本输出的Setup界面备选方案。
  3. 反馈脚本输出结果。若无法查询组织,请提供脚本输出的
    sf org login web
    命令,以便用户进行身份验证。根据请求选择可选参数(如下所示):
    --allocation
    仅查看限制、
    --list-scratch
    枚举scratch org、
    --packaging
    /
    --snapshots
    /
    --shape
    开启子偏好设置、
    --json
    用于自动化场景。

Before you finish

完成前验证

Verify these before returning your answer:
  • CRITICAL: Invoked the helper via its absolute path (
    bash "<skill_dir>/scripts/devhub.sh" …
    ), never
    ./scripts/
    (the script rejects a non-absolute
    $0
    ).
  • Did not run
    --apply
    unless the user clearly asked to actually enable Dev Hub or deploy a preference — enabling is irreversible.
  • Relayed the script's own output (status/allocation or the login/Setup-UI guidance it printed), rather than substituting a hand-written answer.
返回结果前请验证以下内容:
  • 关键检查:通过绝对路径调用辅助脚本(
    bash "<skill_dir>/scripts/devhub.sh" …
    ),切勿使用
    ./scripts/
    (脚本会拒绝非绝对路径的
    $0
    )。
  • 除非用户明确要求实际启用Dev Hub或部署偏好设置,否则请勿运行
    --apply
    ——启用操作不可逆
  • 反馈脚本自身的输出结果(状态/配额或登录/Setup界面指引),切勿手动编写替代内容。

Options

可选参数

FlagPurpose
--allocation
,
-A
Show only the scratch org allocation (Active/Daily).
--list-scratch
,
-l
List active scratch orgs created from this Dev Hub.
--enable
,
-e
Enable Dev Hub (deploys
enableScratchOrgManagementPref=true
). Dry run unless
--apply
.
--configure
,
-c
Configure sub-preferences without (re)enabling the master switch. Pair with a pref flag.
--packaging
enablePackaging2=true
(Unlocked + 2GP packages).
--snapshots
enableScratchOrgSnapshotPref=true
.
--shape
enableShapeExportPref=true
.
--scratch-management
enableScratchOrgManagementPref=true
(the Dev Hub switch).
--pref KEY=VALUE
Any
DevHubSettings
sub-pref (KEY starts with
enable
, VALUE
true
/
false
). Repeatable.
--apply
Actually deploy to the explicit org argument. Default for
--enable
/
--configure
is a validate-only dry run.
--login
Authenticate an org first via
sf org login web
(opens a browser), then run the action.
--instance-url <url>
Log in to a specific instance (My Domain, sandbox, or pre-release/scratch instance) with
sf org login web --instance-url <url>
. Implies
--login
.
--json
Emit machine-readable JSON.
--fail-if-disabled
Exit
3
if Dev Hub is not enabled (CI/cron gate).
--help
,
-h
Show usage.
--enable
may be combined with sub-pref flags to enable and configure at once (e.g.
--enable --packaging --apply
). If omitted, the org defaults to
target-org
, then
target-dev-hub
.
参数用途
--allocation
,
-A
仅显示scratch org配额(Active/Daily)。
--list-scratch
,
-l
列出从此Dev Hub创建的活跃scratch org。
--enable
,
-e
启用Dev Hub(部署
enableScratchOrgManagementPref=true
)。除非添加
--apply
,否则默认仅执行试运行。
--configure
,
-c
配置子偏好设置,无需(重新)启用主开关。需搭配偏好参数使用。
--packaging
设置
enablePackaging2=true
(解锁包+2GP包)。
--snapshots
设置
enableScratchOrgSnapshotPref=true
--shape
设置
enableShapeExportPref=true
--scratch-management
设置
enableScratchOrgManagementPref=true
(Dev Hub主开关)。
--pref KEY=VALUE
设置任意
DevHubSettings
子偏好(KEY以
enable
开头,VALUE为
true
/
false
)。可重复使用。
--apply
实际部署到指定的组织参数。
--enable
/
--configure
的默认行为仅为验证性试运行。
--login
先通过
sf org login web
进行组织身份验证(打开浏览器),再执行后续操作。
--instance-url <url>
通过
sf org login web --instance-url <url>
登录到指定实例(My Domain、沙箱或预发布/scratch实例)。隐含
--login
--json
输出机器可读的JSON格式内容。
--fail-if-disabled
若Dev Hub未启用则退出并返回状态码
3
(用于CI/定时任务校验)。
--help
,
-h
显示使用说明。
--enable
可与子偏好参数组合使用,实现启用并配置的一次性操作(例如
--enable --packaging --apply
)。若未指定组织,默认使用
target-org
,其次是
target-dev-hub

Output examples

输出示例

The script prints the authoritative output at runtime. If you need to calibrate what an enabled-vs-disabled status transcript looks like (including the allocation table and the enable/Setup-UI guidance), read
examples/status-output.md
— otherwise skip it to keep this workflow lean.
脚本会在运行时输出权威结果。若需要了解启用/未启用状态的输出示例(包括配额表和启用/Setup界面指引),请阅读
examples/status-output.md
——否则可跳过此部分以简化工作流。

Structured output for automation (
--json
)

自动化场景的结构化输出(
--json

Use
--json
in any mode for machine-readable output (no prose). Status returns
devHubEnabled
,
status
, and an
allocation
object; enablement returns
success
,
applied
, and
devHubVerifiedState
. Deterministic, no LLM needed.
bash
bash "<skill_dir>/scripts/devhub.sh" my-devhub --json
在任意模式下使用
--json
参数可获取机器可读的输出结果(无描述性文本)。状态查询会返回
devHubEnabled
status
allocation
对象;启用操作会返回
success
applied
devHubVerifiedState
。结果确定,无需依赖大语言模型。
bash
bash "<skill_dir>/scripts/devhub.sh" my-devhub --json

{"org":"my-devhub","devHubEnabled":true,"status":"enabled", ... }

{"org":"my-devhub","devHubEnabled":true,"status":"enabled", ... }


`--fail-if-disabled` exits `3` (and prints an `ALERT:` line to stderr) if Dev
Hub is not enabled, so a scheduled job can gate on it.

`--fail-if-disabled`参数会在Dev Hub未启用时退出并返回状态码`3`(同时向stderr输出`ALERT:`行),因此定时任务可基于此进行校验。

Exit codes

退出状态码

  • 0
    success
  • 1
    an org could not be queried, or a deploy failed (auth/connection/deploy)
  • 2
    bad usage or a missing dependency (
    sf
    or
    jq
    )
  • 3
    Dev Hub is not enabled (only when
    --fail-if-disabled
    is set)
  • 0
    执行成功
  • 1
    无法查询组织,或部署失败(身份验证/连接/部署问题)
  • 2
    使用方式错误或缺少依赖(
    sf
    jq
  • 3
    Dev Hub未启用(仅当设置
    --fail-if-disabled
    时返回)

Authentication

身份验证

If the org isn't authenticated yet, the skill can log in for you with
--login
(or
--instance-url <url>
), then continue with the requested action in the same run — this is the preferred path when the user provides an instance URL:
Substitute the user's actual instance URL for
<url>
(shown here as the generic
https://my-domain.my.salesforce.com
placeholder):
bash
undefined
若组织尚未完成身份验证,技能可通过
--login
(或
--instance-url <url>
)为您登录,然后在同一次运行中继续执行请求的操作——当用户提供实例URL时,这是首选方式:
将用户提供的实际实例URL替换为
<url>
(以下示例中使用通用占位符
https://my-domain.my.salesforce.com
):
bash
undefined

Log in to a specific instance (My Domain / pre-release / sandbox), then enable:

登录到指定实例(My Domain/预发布/沙箱),然后启用:

bash "<skill_dir>/scripts/devhub.sh" my-alias
--instance-url https://my-domain.my.salesforce.com --enable --apply
bash "<skill_dir>/scripts/devhub.sh" my-alias
--instance-url https://my-domain.my.salesforce.com --enable --apply

Bare instance URL as shorthand — logs in to it, then reports status:

直接传入实例URL作为简化写法——登录该组织,然后报告状态:

bash "<skill_dir>/scripts/devhub.sh" https://my-domain.my.salesforce.com
bash "<skill_dir>/scripts/devhub.sh" https://my-domain.my.salesforce.com

Just authenticate an org (no other action):

仅进行组织身份验证(无其他操作):

bash "<skill_dir>/scripts/devhub.sh" my-alias --login

`sf org login web` opens a browser for the OAuth flow; complete it promptly (the
session times out). A supplied positional (`my-alias`) becomes the CLI alias; a
positional that looks like a URL (contains `://`) is taken as the
`--instance-url` to log in to. The script accepts only HTTPS Salesforce-owned
hostnames for this option. Omit `--instance-url` for standard
production/DE/trial logins (`login.salesforce.com`). If the browser flow keeps
timing out, the script prints the `sf org login device` fallback for a
code-based login.

If an org isn't authenticated and no login flag was passed, the script instead
prints the exact command to log in and re-run — using the instance URL you
supplied when there is one:

```text
  my-org                         could not query org (...)

  To authenticate against https://my-domain.my.salesforce.com, run:
    sf org login web --instance-url "https://my-domain.my.salesforce.com" --alias "my-org"
  Or let this script log you in and continue in one step:
    devhub.sh "my-org" --instance-url "https://my-domain.my.salesforce.com" --login
  Then re-run. List existing logins with:  sf org list
bash "<skill_dir>/scripts/devhub.sh" my-alias --login

`sf org login web`会打开浏览器进行OAuth流程;请及时完成验证(会话会超时)。传入的位置参数(`my-alias`)会成为CLI别名;若位置参数看起来是URL(包含`://`),则会被视为`--instance-url`用于登录。该选项仅接受HTTPS协议的Salesforce官方主机名。对于标准生产/DE/试用版登录(`login.salesforce.com`),可省略`--instance-url`。若浏览器流程持续超时,脚本会输出`sf org login device`备选方案,用于基于验证码的登录。

若组织未完成身份验证且未传入登录参数,脚本会输出准确的登录命令并提示重新运行——若提供了实例URL,则会使用该URL:

```text
  my-org                         无法查询组织(...)

  要在https://my-domain.my.salesforce.com进行身份验证,请运行:
    sf org login web --instance-url "https://my-domain.my.salesforce.com" --alias "my-org"
  或让此脚本一次性完成登录并继续执行:
    devhub.sh "my-org" --instance-url "https://my-domain.my.salesforce.com" --login
  然后重新运行。查看已登录组织列表:  sf org list

Manual fallback (if the script is unavailable)

手动备选方案(若脚本不可用)

bash
undefined
bash
undefined

Detect Dev Hub (INVALID_TYPE error means OFF; a count means ON):

检测Dev Hub状态(返回INVALID_TYPE错误表示未开启;返回计数表示已开启):

sf data query --query "SELECT COUNT() FROM ScratchOrgInfo" --target-org <org> --json
sf data query --query "SELECT COUNT() FROM ScratchOrgInfo" --target-org <org> --json

Enable Dev Hub via the Tooling API (what the Setup toggle does; irreversible):

通过Tooling API启用Dev Hub(与Setup界面开关效果一致;不可逆):

sf api request rest "/services/data/v47.0/tooling/sobjects/DevHubSettings/DevHub"
--method PATCH
--body '{"FullName":"DevHub","Metadata":{"enableScratchOrgManagementPref":true}}'
--target-org <org>
sf api request rest "/services/data/v47.0/tooling/sobjects/DevHubSettings/DevHub"
--method PATCH
--body '{"FullName":"DevHub","Metadata":{"enableScratchOrgManagementPref":true}}'
--target-org <org>

Or enable in the UI: Setup > Quick Find > "Dev Hub" > turn Enable Dev Hub On.

或在UI中启用:Setup > 快速查找 > "Dev Hub" > 开启Enable Dev Hub。

View scratch org allocation:

查看scratch org配额:

sf org list limits --target-org <org> --json | jq '.result[] | select(.name|test("ScratchOrg"))'
undefined
sf org list limits --target-org <org> --json | jq '.result[] | select(.name|test("ScratchOrg"))'
undefined

Notes

注意事项

  • Requires the Salesforce CLI (
    sf
    ) and
    jq
    on the PATH, plus at least one authenticated org (
    sf org login web
    ).
  • After enabling, set the org as your default Dev Hub:
    sf config set target-dev-hub <org>
    .
  • Enabling packaging (
    enablePackaging2
    ) requires Dev Hub to be on first —
    --enable --packaging
    handles the ordering in a single deploy.
  • Non-admin users still need object access (Read/Create on
    ScratchOrgInfo
    and
    ActiveScratchOrg
    ) to create/view scratch orgs, and the "Create and Update Second-Generation Packages" permission to build 2GP/unlocked packages.
  • To switch the default org use
    dx-org-switch
    ; to check expiration use
    dx-org-trial-expiration-check
    .
  • 需要将Salesforce CLI(
    sf
    )和
    jq
    添加到PATH中,且至少有一个已完成身份验证的组织(通过
    sf org login web
    登录)。
  • 启用Dev Hub后,可将该组织设置为默认Dev Hub:
    sf config set target-dev-hub <org>
  • 启用打包
    enablePackaging2
    )需要先开启Dev Hub——
    --enable --packaging
    会在单次部署中处理顺序问题。
  • 非管理员用户仍需拥有对象访问权限(
    ScratchOrgInfo
    ActiveScratchOrg
    的读/创建权限)才能创建/查看scratch org,且需要“创建和更新第二代包”权限才能构建2GP/解锁包。
  • 如需切换默认组织,请使用
    dx-org-switch
    ;如需查询到期日期,请使用
    dx-org-trial-expiration-check