bash - 如何在 Ubuntu 上使用 bash 创建 GitHub 存储库机密/加密它?
问题描述
我正在尝试在 Ubuntu 上使用 bash 创建/更新 GitHub 机密。
他们的 api 文档说我应该
- 从 repo 中获取公钥
- 用它加密秘密
- 创建/更新 GitHub 密码
但是示例仅在 NodeJS 和 Python 中,我不确定如何使用 Ubuntu 上的 bash 中提到的 libsodium 来实现我需要的。
https://docs.github.com/en/rest/reference/actions#create-or-update-a-repository-secret
我能够让公众key
和key_id
使用
$ curl -s \
-H "authorization: Bearer $MY_ACCESS_TOKEN" \
https://api.github.com/repos/MYORG/MYREPO/actions/secrets/public-key
{
"key_id": "123456789012345678",
"key": "abcdHXZ2BrPAFPrZHy1AAct3B12k7BPgxXgdtxcABCo="
}
我能够使用 Python 3 示例生成似乎是我的秘密的有效加密值:
#!/usr/bin/python3
from base64 import b64encode
from nacl import encoding, public
def encrypt(public_key: str, secret_value: str) -> str:
public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
sealed_box = public.SealedBox(public_key)
encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
return b64encode(encrypted).decode("utf-8")
print(encrypt("abcdHXZ2BrPAFPrZHy1AAct3B12k7BPgxXgdtxcABCo=", "ABCDEF1234"))
$ ./encrypt-secret.py
ST5Blke5GXO2FyMLUbYAhkmzLKJ3cljd1lI97q028gcrq3XC9aTqPlNzbMQAI5iHoj/70ao0/GOrhg==
但我正在寻找一个 bash 实现。
解决方案
看起来可以使用 github cli 实用程序 (gh) 来创建秘密,而无需运行 python/nodejs 代码,这看起来像是我一直在寻找的解决方案。这是您的操作方法:
export DEBIAN_FRONTEND=noninteractive
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
sudo apt update && \
sudo apt-get install -y gh
export GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" && \
export SECRET_VALUE_FILE_PATH="your_file_with_secret_value" && \
gh secret set KUBECONFIG < "$SECRET_VALUE_FILE_PATH"