使用 REST API 与 GitHub Actions 中的秘密进行交互。
你可以使用 REST API 来创建、更新、删除和检索有关可在 GitHub Actions 中的工作流中使用的秘密的信息。秘密允许你将敏感信息(例如访问令牌)存储在你的存储库、存储库环境或组织中。有关详细信息,请参阅“在 GitHub Actions 中使用秘密”。
列出组织中可用的所有秘密,而不显示其加密值。
经过身份验证的用户必须具有对存储库的协作者访问权限才能创建、更新或读取秘密。
OAuth 应用程序令牌和个人访问令牌(经典)需要 admin:org 范围才能使用此端点。如果存储库是私有的,则还需要 repo 范围。
admin:org
repo
此端点适用于以下细粒度令牌类型
细粒度令牌必须具有以下权限集
accept
建议设置为 application/vnd.github+json。
application/vnd.github+json
org
组织名称。名称不区分大小写。
per_page
每页的结果数(最多 100)。有关详细信息,请参阅“在 REST API 中使用分页”。
默认: 30
30
page
要获取的结果的页码。有关详细信息,请参阅“在 REST API 中使用分页”。
默认: 1
1
200
OK
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets
响应
状态: 200
{ "total_count": 3, "secrets": [ { "name": "GIST_ID", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z", "visibility": "private" }, { "name": "DEPLOY_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z", "visibility": "all" }, { "name": "GH_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z", "visibility": "selected", "selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories" } ] }
获取公钥,你需要使用它来加密秘密。在创建或更新秘密之前,你需要加密一个秘密。
OAuth 令牌和个人访问令牌(经典)需要admin:org范围才能使用此端点。如果存储库是私有的,则 OAuth 令牌和个人访问令牌(经典)需要repo范围才能使用此端点。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/public-key
{ "key_id": "012345678912345678", "key": "2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234" }
获取单个组织机密,而不显示其加密值。
经过身份验证的用户必须具有对存储库的协作者访问权限,才能创建、更新或读取机密
secret_name
机密的名称。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME
{ "name": "GH_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z", "visibility": "selected", "selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories" }
使用加密值创建或更新组织机密。使用 LibSodium 加密机密。有关更多信息,请参阅“为 REST API 加密机密”。
encrypted_value
您的秘密的值,使用 LibSodium 使用从 获取组织公钥 端点检索到的公钥进行加密。
key_id
用于加密秘密的密钥的 ID。
visibility
哪种类型的组织存储库有权访问组织秘密。selected 意味着只有 selected_repository_ids 指定的存储库才能访问秘密。
selected
selected_repository_ids
可以是以下之一: all, private, selected
all
private
可以访问组织秘密的存储库 ID 数组。只有在将 visibility 设置为 selected 时,您才能提供存储库 ID 列表。您可以使用 列出组织秘密的选定存储库、设置组织秘密的选定存储库 和 从组织秘密中移除选定的存储库 端点管理选定存储库的列表。
201
创建秘密时的响应
204
更新秘密时的响应
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME \ -d '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678","visibility":"selected","selected_repository_ids":[1296269,1296280]}'
状态:201
使用秘密名称删除组织中的秘密。
无内容
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME
状态:204
当存储库访问的visibility设置为selected时,列出所有已选定的存储库。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME/repositories
{ "total_count": 1, "repositories": [ { "id": 1296269, "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", "name": "Hello-World", "full_name": "octocat/Hello-World", "owner": { "login": "octocat", "id": 1, "node_id": "MDQ6VXNlcjE=", "avatar_url": "https://github.com/images/error/octocat_happy.gif", "gravatar_id": "", "url": "https://api.github.com/users/octocat", "html_url": "https://github.com/octocat", "followers_url": "https://api.github.com/users/octocat/followers", "following_url": "https://api.github.com/users/octocat/following{/other_user}", "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", "organizations_url": "https://api.github.com/users/octocat/orgs", "repos_url": "https://api.github.com/users/octocat/repos", "events_url": "https://api.github.com/users/octocat/events{/privacy}", "received_events_url": "https://api.github.com/users/octocat/received_events", "type": "User", "site_admin": false }, "private": false, "html_url": "https://github.com/octocat/Hello-World", "description": "This your first repo!", "fork": false, "url": "https://api.github.com/repos/octocat/Hello-World", "archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", "assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}", "blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", "branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}", "collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", "comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}", "commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}", "compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}", "contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors", "deployments_url": "https://api.github.com/repos/octocat/Hello-World/deployments", "downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads", "events_url": "https://api.github.com/repos/octocat/Hello-World/events", "forks_url": "https://api.github.com/repos/octocat/Hello-World/forks", "git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", "git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", "git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", "git_url": "git:github.com/octocat/Hello-World.git", "issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", "issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}", "issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}", "keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}", "labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}", "languages_url": "https://api.github.com/repos/octocat/Hello-World/languages", "merges_url": "https://api.github.com/repos/octocat/Hello-World/merges", "milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}", "notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", "pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}", "releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}", "ssh_url": "[email protected]:octocat/Hello-World.git", "stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers", "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}", "subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers", "subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription", "tags_url": "https://api.github.com/repos/octocat/Hello-World/tags", "teams_url": "https://api.github.com/repos/octocat/Hello-World/teams", "trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", "hooks_url": "http://api.github.com/repos/octocat/Hello-World/hooks" } ] }
当存储库访问的visibility设置为selected时,替换组织秘密的所有存储库。当您创建或更新组织秘密时,将设置可见性。
可以访问组织机密的存储库 ID 数组。仅当将 visibility 设置为 selected 时,才能提供存储库 ID 列表。可以使用 将选定存储库添加到组织机密 和 从组织机密中移除选定存储库 端点添加和移除各个存储库。
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME/repositories \ -d '{"selected_repository_ids":[64780797]}'
当存储库访问的 visibility 设置为 selected 时,将存储库添加到组织机密。有关设置可见性的详细信息,请参阅 创建或更新组织机密。
OAuth 令牌和个人访问令牌(经典)需要 admin:org 范围才能使用此端点。如果存储库是私有的,则 OAuth 令牌和个人访问令牌(经典)需要 repo 范围才能使用此端点。
repository_id
将存储库添加到选定列表时无内容
409
可见性类型未设置为选定时冲突
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME/repositories/REPOSITORY_ID
当存储库访问的visibility设置为selected时,从组织机密中移除存储库。当您创建或更新组织机密时,会设置可见性。
从选定列表中移除存储库时的响应
可见性类型未设置为选定的冲突
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/actions/secrets/SECRET_NAME/repositories/REPOSITORY_ID
列出与存储库共享的所有组织机密,但不会显示其加密值。
OAuth 应用令牌和个人访问令牌(经典版)需要repo范围才能使用此端点。
owner
存储库的帐户所有者。名称不区分大小写。
不带.git扩展名的存储库名称。名称不区分大小写。
.git
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/organization-secrets
{ "total_count": 2, "secrets": [ { "name": "GH_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z" }, { "name": "GIST_ID", "created_at": "2020-01-10T10:59:22Z", "updated_at": "2020-01-11T11:59:22Z" } ] }
列出存储库中可用的所有机密,而不会泄露其加密值。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/secrets
任何具有存储库读取权限的人都可以使用此端点。
如果存储库是私有的,OAuth 令牌和个人访问令牌(经典版)需要 repo 范围才能使用此端点。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/secrets/public-key
获取单个存储库机密,而不会泄露其加密值。
经过身份验证的用户必须具有对存储库的协作者访问权限才能使用此端点。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/secrets/SECRET_NAME
{ "name": "GH_TOKEN", "created_at": "2019-08-10T14:59:22Z", "updated_at": "2020-01-10T14:59:22Z" }
使用加密值创建或更新存储库机密。使用 LibSodium 加密您的机密。有关更多信息,请参阅“为 REST API 加密机密”。
OAuth 令牌和个人访问令牌(经典)需要 repo 范围才能使用此端点。
使用从 获取存储库公钥 端点检索的公钥,用 LibSodium 加密的密钥值。
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/secrets/SECRET_NAME \ -d '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}'
使用密钥名称删除存储库中的密钥。
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/actions/secrets/SECRET_NAME
列出环境中可用的所有密钥,而不显示其加密值。
environment_name
环境的名称。名称必须经过 URL 编码。例如,名称中的任何斜杠都必须替换为 %2F。
%2F
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME/secrets
获取环境的公钥,你需要使用该公钥加密环境机密。在创建或更新机密之前,你需要加密机密。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME/secrets/public-key
获取单个环境机密,而不显示其加密值。
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME/secrets/SECRET_NAME
使用加密值创建或更新环境机密。使用 LibSodium 加密你的机密。有关详细信息,请参阅“为 REST API 加密机密”。
你的机密的值,使用 LibSodium 和从 获取环境公钥 端点检索的公钥进行加密。
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME/secrets/SECRET_NAME \ -d '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}'
使用密钥名称删除环境中的密钥。
默认响应
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME/secrets/SECRET_NAME