|
| 1 | +--- |
| 2 | +name: Create or update an environment secret |
| 3 | +example: octokit.actions.createOrUpdateEnvironmentSecret({ repository_id, environment_name, secret_name }) |
| 4 | +route: PUT /repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name} |
| 5 | +scope: actions |
| 6 | +type: API method |
| 7 | +--- |
| 8 | + |
| 9 | +# Create or update an environment secret |
| 10 | + |
| 11 | +Creates or updates an environment secret with an encrypted value. Encrypt your secret using |
| 12 | +[LibSodium](https://libsodium.gitbook.io/doc/bindings_for_other_languages). You must authenticate using an access |
| 13 | +token with the `repo` scope to use this endpoint. GitHub Apps must have the `secrets` repository permission to use |
| 14 | +this endpoint. |
| 15 | + |
| 16 | +#### Example encrypting a secret using Node.js |
| 17 | + |
| 18 | +Encrypt your secret using the [tweetsodium](https://github.com/github/tweetsodium) library. |
| 19 | + |
| 20 | +``` |
| 21 | +const sodium = require('tweetsodium'); |
| 22 | +
|
| 23 | +const key = "base64-encoded-public-key"; |
| 24 | +const value = "plain-text-secret"; |
| 25 | +
|
| 26 | +// Convert the message and key to Uint8Array's (Buffer implements that interface) |
| 27 | +const messageBytes = Buffer.from(value); |
| 28 | +const keyBytes = Buffer.from(key, 'base64'); |
| 29 | +
|
| 30 | +// Encrypt using LibSodium. |
| 31 | +const encryptedBytes = sodium.seal(messageBytes, keyBytes); |
| 32 | +
|
| 33 | +// Base64 the encrypted secret |
| 34 | +const encrypted = Buffer.from(encryptedBytes).toString('base64'); |
| 35 | +
|
| 36 | +console.log(encrypted); |
| 37 | +``` |
| 38 | + |
| 39 | +#### Example encrypting a secret using Python |
| 40 | + |
| 41 | +Encrypt your secret using [pynacl](https://pynacl.readthedocs.io/en/stable/public/#nacl-public-sealedbox) with Python 3. |
| 42 | + |
| 43 | +``` |
| 44 | +from base64 import b64encode |
| 45 | +from nacl import encoding, public |
| 46 | +
|
| 47 | +def encrypt(public_key: str, secret_value: str) -> str: |
| 48 | + """Encrypt a Unicode string using the public key.""" |
| 49 | + public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder()) |
| 50 | + sealed_box = public.SealedBox(public_key) |
| 51 | + encrypted = sealed_box.encrypt(secret_value.encode("utf-8")) |
| 52 | + return b64encode(encrypted).decode("utf-8") |
| 53 | +``` |
| 54 | + |
| 55 | +#### Example encrypting a secret using C# |
| 56 | + |
| 57 | +Encrypt your secret using the [Sodium.Core](https://www.nuget.org/packages/Sodium.Core/) package. |
| 58 | + |
| 59 | +``` |
| 60 | +var secretValue = System.Text.Encoding.UTF8.GetBytes("mySecret"); |
| 61 | +var publicKey = Convert.FromBase64String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU="); |
| 62 | +
|
| 63 | +var sealedPublicKeyBox = Sodium.SealedPublicKeyBox.Create(secretValue, publicKey); |
| 64 | +
|
| 65 | +Console.WriteLine(Convert.ToBase64String(sealedPublicKeyBox)); |
| 66 | +``` |
| 67 | + |
| 68 | +#### Example encrypting a secret using Ruby |
| 69 | + |
| 70 | +Encrypt your secret using the [rbnacl](https://github.com/RubyCrypto/rbnacl) gem. |
| 71 | + |
| 72 | +```ruby |
| 73 | +require "rbnacl" |
| 74 | +require "base64" |
| 75 | + |
| 76 | +key = Base64.decode64("+ZYvJDZMHUfBkJdyq5Zm9SKqeuBQ4sj+6sfjlH4CgG0=") |
| 77 | +public_key = RbNaCl::PublicKey.new(key) |
| 78 | + |
| 79 | +box = RbNaCl::Boxes::Sealed.from_public_key(public_key) |
| 80 | +encrypted_secret = box.encrypt("my_secret") |
| 81 | + |
| 82 | +# Print the base64 encoded secret |
| 83 | +puts Base64.strict_encode64(encrypted_secret) |
| 84 | +``` |
| 85 | + |
| 86 | +```js |
| 87 | +octokit.actions.createOrUpdateEnvironmentSecret({ |
| 88 | + repository_id, |
| 89 | + environment_name, |
| 90 | + secret_name, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Parameters |
| 95 | + |
| 96 | +<table> |
| 97 | + <thead> |
| 98 | + <tr> |
| 99 | + <th>name</th> |
| 100 | + <th>required</th> |
| 101 | + <th>description</th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + <tr><td>repository_id</td><td>yes</td><td> |
| 106 | + |
| 107 | +</td></tr> |
| 108 | +<tr><td>environment_name</td><td>yes</td><td> |
| 109 | + |
| 110 | +The name of the environment |
| 111 | + |
| 112 | +</td></tr> |
| 113 | +<tr><td>secret_name</td><td>yes</td><td> |
| 114 | + |
| 115 | +secret_name parameter |
| 116 | + |
| 117 | +</td></tr> |
| 118 | +<tr><td>encrypted_value</td><td>no</td><td> |
| 119 | + |
| 120 | +Value for your secret, encrypted with [LibSodium](https://libsodium.gitbook.io/doc/bindings_for_other_languages) using the public key retrieved from the [Get an environment public key](https://docs.github.com/rest/reference/actions#get-an-environment-public-key) endpoint. |
| 121 | + |
| 122 | +</td></tr> |
| 123 | +<tr><td>key_id</td><td>no</td><td> |
| 124 | + |
| 125 | +ID of the key you used to encrypt the secret. |
| 126 | + |
| 127 | +</td></tr> |
| 128 | + </tbody> |
| 129 | +</table> |
| 130 | + |
| 131 | +See also: [GitHub Developer Guide documentation](https://docs.github.com/rest/reference/actions#create-or-update-an-environment-secret). |
0 commit comments