如何创建无人机秘密文件?

how to create a drone secret file?

文档展示了如何将文件设置为秘密环境变量 http://readme.drone.io/0.5/secrets/

有没有方便的方法来做相反的事情?例如让这个 ssh 密钥在 .ssh/id_rsa 中可用,并具有所有正确的权限。

而 "convienient" 显然是指无需输入 mkdir>chmod

如果您想在构建过程中使用 ssh 密钥,可以使用以下命令将 ssh 密钥添加到秘密存储区:

drone secrets add --image=<image> <repo> SSH_KEY @/path/to/.ssh/id_rsa

请注意,@ 表示法类似于 curl。此功能存在的原因是因为使用 cat(或某种其他类型的管道)创建秘密似乎会导致上传格式错误的文件。

添加文件后,您可以在 Yaml 中引用:

pipeline:
  image: busybox
  environment:
    - SSH_KEY: ${SSH_KEY}
  commands:
    - mkdir /root/.ssh && echo "$SSH_KEY" > /root/.ssh/id_rsa && chmod 0600 /root/.ssh/id_rsa

请注意,在引号内添加 SSH_KEY 以保留新行很重要。

您可能还需要将主机添加到 known_hosts 以防止主机密钥问题;在下面将 bitbucket.org 更改为您要从中拉出的任何主机,并将其添加到 commands 上面显示的命令之后,以确保 /root/.ssh 目录存在):

ssh-keyscan -H bitbucket.org >> /root/.ssh/known_hosts

(您还需要安装 openssh-client 或等价物,如果它在您的构建映像中尚不可用。)

And by "convienient" I obviously mean without having to type mkdir, > or chmod

没有

在 Drone 0.7+ 中,当使用 Github oAuth2 对 Drone 进行身份验证时,它会自动将 Github 用户名和密码添加到构建 .netrc 中。

密码实际上是一个令牌instead of a password.netrc 看起来像这样:

machine github.com
  login <SOME_SECRET>
  password x-oauth-basic

这意味着您可以通过 HTTPS 克隆私有 Github 存储库,而无需指定 username/password,即 git clone https://github.com/USER/REPO/git.

您也可以通过添加 ~/.netrc 文件并添加如下内容在本地获得相同的效果:

machine github.com
  login <GITHUB_USERNAME>
  password <GITHUB_PERSONAL_TOKEN>

machine api.github.com
  login <GITHUB_USERNAME>
  password <GITHUB_PERSONAL_TOKEN>

你将不得不generate a personal token

例如,如果使用 Ruby 包管理器 bundler,您可以将以下内容添加到 Gemfile:

gem 'documas', git: 'https://github.com/Propheris/documas-core.git'

构建可以 bundle install 成功,因为它将使用 Github 令牌通过 HTTPS 克隆上述存储库。唯一的问题是,当您在本地执行 bundle install 时,它会要求 username/password。为了解决这个问题,请按照上面的示例将 ~/.netrc 文件添加到您的开发机器。

无人机 0.8+

首先,如果是二进制文件,您需要进行 base64 编码。

base64 -i yourfile.bin -o base64file.bin

然后将secret添加到drone中:

drone secret add --repository <repo> --name yourname_keys --value @base64file.bin

管道中的内容如下:

command:
 - echo "$YOURNAME_KEYS" > some/path/afilebase64
 - base64 -D -i some/path/afilebase64 -o some/path/afilebinary