3

Is it possible to create a new environment for a repository https://github.com/org/repo/settings/environments using the gh cli?

The only mention of environment I can find in the manual is here https://cli.github.com/manual/gh_secret_set where it says you can assign a secret to an existing environment, but it seems the environment would have to be created manually.

gary69
  • 3,620
  • 6
  • 36
  • 50

2 Answers2

6

From what I'm seeing here in the gh cli repo that's going to be a no at this time. The issue [linked] is an enhancement request for just that.

I too wanted to know if this could be done, as it's part of my workflow to inject environment secrets.

You can still use the API to create it though.

curl -X PUT \
-H 'Authorization: Bearer ghp_...' \
-H 'Accept: application/vnd.github.v3+json' \
https://api.github.com/repos/<org>/<repo>/environments/<env>

Alternatively, the same can be done using gh:

gh api --method PUT -H "Accept: application/vnd.github+json" \
repos/<org>/<repo>/environments/<env>
Paweł Prażak
  • 3,091
  • 1
  • 27
  • 42
Flash1212
  • 76
  • 4
  • Thanks, any idea if there's a way to specify `deployment_branches`? I tried `-d '{"deployment_branches":["branch1", "branch2"]}'` https://docs.github.com/en/rest/reference/deployments#create-or-update-an-environment – gary69 Mar 08 '22 at 19:36
2

Basically you need to create the environment first, then you can set branch policies:

jq -n '{"deployment_branch_policy": {"protected_branches": false, "custom_branch_policies": true}}'|gh api -H "Accept: application/vnd.github+json" -X PUT /repos/:owner/:repo/environments/dev --input -
gh api --method POST -H "Accept: application/vnd.github+json" "/repos/Oceaneering/it_infra_base_application_bootstrapper/environments/dev/deployment-branch-policies" -f name=dev

I wrote a python script for my use case that uses the gh cli to create environments and can include a branch pattern.

https://gist.github.com/walkerk1980/8a6f6879b32260360854a89bb880a48d