hen*_*ald 50 heroku node.js npm
我正在尝试将我的应用程序部署到Heroku,但我依赖于使用一些私有git repos作为模块.我这样做是为了在项目之间重用代码,例如我有一个我在多个应用程序中使用的自定义记录器.
"logger":"git+ssh://git@bitbucket.org..............#master"
Run Code Online (Sandbox Code Playgroud)
问题是Heroku显然没有ssh访问此代码.我在这个问题上找不到任何东西.理想情况下Heroku有一个我可以添加到模块的公钥.
Koe*_*en. 67
GitHub支持基本身份验证:
"dependencies" : {
"my-module" : "git+https://my_username:my_password@github.com/my_github_account/my_repo.git"
}
Run Code Online (Sandbox Code Playgroud)
和BitBucket一样:
"dependencies" : {
"my-module": "git+https://my_username:my_password@bitbucket.org/my_bitbucket_account/my_repo.git"
}
Run Code Online (Sandbox Code Playgroud)
但是在你的密码中package.json可能不需要.
为了使这个答案更新,我现在建议在GitHub上使用个人访问令牌而不是用户名/密码组合.
你现在应该使用:
"dependencies" : {
"my-module" : "git+https://<username>:<token>@github.com/my_github_account/my_repo.git"
}
Run Code Online (Sandbox Code Playgroud)
对于Github,您可以在此处生成新令牌:
https://github.com/settings/tokens
应用程序密码主要用于提供与不支持双因素身份验证的应用程序的兼容性,您也可以将它们用于此目的.首先,创建一个应用程序密码,然后像这样指定您的依赖关系:
"dependencies" : {
"my-module": "git+https://<username>:<app-password>@bitbucket.org/my_bitbucket_account/my_repo.git"
}
Run Code Online (Sandbox Code Playgroud)
对于BitBucket,您可以在Manage Team页面上生成API Key,然后使用以下URL:
"dependencies" : {
"my-module" : "git+https://<teamname>:<api-key>@bitbucket.org/team_name/repo_name.git"
}
Run Code Online (Sandbox Code Playgroud)
Tom*_*cer 38
更新2016-03-26
如果使用npm3,则描述的方法不再有效,因为npm3 package.json在运行preinstall脚本之前获取所描述的所有模块.这已被确认为一个错误.
官方node.js Heroku buildpack现在包括heroku-prebuild和heroku-postbuild,npm install分别在之前和之后运行.你应该使用这些脚本,而不是preinstall和postinstall在所有情况下,同时支持NPM2和npm3.
换句话说,你package.json应该像:
"scripts": {
"heroku-prebuild": "bash preinstall.sh",
"heroku-postbuild": "bash postinstall.sh"
}
Run Code Online (Sandbox Code Playgroud)
我想出了迈克尔答案的替代方案,保留了(IMO)有利的要求,即保持您的凭据不受源代码控制,同时不需要自定义构建包.这是因为迈克尔链接的buildpack已经过时而感到沮丧.
解决方案是在npm preinstall和postinstall脚本中设置和拆除SSH环境,而不是在buildpack中.
请遵循以下说明:
preinstall.sh和postinstall.sh.chmod +x *.sh).preinstall.sh: #!/bin/bash
# Generates an SSH config file for connections if a config var exists.
if [ "$GIT_SSH_KEY" != "" ]; then
echo "Detected SSH key for git. Adding SSH config" >&1
echo "" >&1
# Ensure we have an ssh folder
if [ ! -d ~/.ssh ]; then
mkdir -p ~/.ssh
chmod 700 ~/.ssh
fi
# Load the private key into a file.
echo $GIT_SSH_KEY | base64 --decode > ~/.ssh/deploy_key
# Change the permissions on the file to
# be read-only for this user.
chmod 400 ~/.ssh/deploy_key
# Setup the ssh config file.
echo -e "Host github.com\n"\
" IdentityFile ~/.ssh/deploy_key\n"\
" IdentitiesOnly yes\n"\
" UserKnownHostsFile=/dev/null\n"\
" StrictHostKeyChecking no"\
> ~/.ssh/config
fi
Run Code Online (Sandbox Code Playgroud)
postinstall.sh: #!/bin/bash
if [ "$GIT_SSH_KEY" != "" ]; then
echo "Cleaning up SSH config" >&1
echo "" >&1
# Now that npm has finished running, we shouldn't need the ssh key/config anymore.
# Remove the files that we created.
rm -f ~/.ssh/config
rm -f ~/.ssh/deploy_key
# Clear that sensitive key data from the environment
export GIT_SSH_KEY=0
fi
Run Code Online (Sandbox Code Playgroud)
将以下内容添加到您的package.json:
"scripts": {
"preinstall": "bash preinstall.sh",
"postinstall": "bash postinstall.sh"
}
Run Code Online (Sandbox Code Playgroud)使用生成私钥/公钥对ssh-agent.
GIT_SSH_KEY.当Heroku构建您的应用程序时,在npm安装依赖项之前,preinstall.sh脚本会运行.这将从GIT_SSH_KEY环境变量的已解码内容创建私钥文件,并创建SSH配置文件以告知SSH在连接时使用此文件github.com.(如果您要连接到Bitbucket,请将Host条目更新preinstall.sh到bitbucket.org).然后npm使用此SSH配置安装模块.安装后,将删除私钥并擦除配置.
这允许Heroku通过SSH下载您的私有模块,同时保持私钥不在代码库中.如果您的私钥被泄露,因为它只是部署密钥的一半,您可以撤销GitHub中的公钥并重新生成密钥对.
顺便说一句,由于GitHub部署密钥具有读/写权限,如果您在GitHub组织中托管该模块,您可以创建一个只读团队并为其分配一个"部署"用户.然后,可以使用密钥对的公共一半配置部署用户.这为您的模块增加了额外的安全层.
Dom*_*ker 13
在你的git repo中使用纯文本密码是一个非常糟糕的主意,使用访问令牌会更好,但你仍然需要非常小心.
"my_module": "git+https://ACCESS_TOKEN:x-oauth-basic@github.com/me/my_module.git"
Run Code Online (Sandbox Code Playgroud)
Mic*_*ang 13
我创建了一个自定义nodeJS buildpack,它允许您指定一个使用ssh-agent注册的SSH密钥,并在首次设置dynos时由npm使用.它无缝地允许您将模块指定为您所显示的ssh url package.json:
"private_module": "git+ssh://git@github.com:me/my_module.git"
Run Code Online (Sandbox Code Playgroud)
要设置您的应用以使用您的私钥:
ssh-keygen -t rsa -C "your_email@example.com"输入无密码.build包不支持带密码的密钥)pbcopy < ~/.ssh/id_rsa.pub在OS X中)并将结果粘贴到github admin中cat id_rsa | base64 | pbcopy,然后heroku config:set GIT_SSH_KEY=<paste_here> --app your-app-name我的自定义buildpack可以在这里找到:https://github.com/thirdiron/heroku-buildpack-nodejs,它适用于我的系统.评论和拉取请求非常受欢迎.
基于@fiznool的答案,我使用存储为环境变量的自定义ssh密钥创建了一个buildpack来解决这个问题.由于buildpack与技术无关,因此可以使用任何工具来下载依赖项,例如用于php的composer,用于ruby的bundler,用于javascript的npm等等:https://github.com/simon0191/custom-ssh-key-buildpack
将buildpack添加到您的应用程序:
$ heroku buildpacks:add --index 1 https://github.com/simon0191/custom-ssh-key-buildpack
Run Code Online (Sandbox Code Playgroud)生成一个没有密码短语的新SSH密钥(假设您将其命名为deploy_key)
将公钥添加到您的私有存储库帐户.例如:
将私钥编码为base64字符串,并将其添加为CUSTOM_SSH_KEYheroku应用程序的环境变量.
制作以逗号分隔的主机列表,其中应使用ssh密钥,并将其添加为CUSTOM_SSH_KEY_HOSTSheroku应用程序的环境变量.
# MacOS
$ heroku config:set CUSTOM_SSH_KEY=$(base64 --input ~/.ssh/deploy_key) CUSTOM_SSH_KEY_HOSTS=bitbucket.org,github.com
# Ubuntu
$ heroku config:set CUSTOM_SSH_KEY=$(base64 ~/.ssh/deploy_key) CUSTOM_SSH_KEY_HOSTS=bitbucket.org,github.com
Run Code Online (Sandbox Code Playgroud)hen*_*ald -4
简而言之,这是不可能的。我想出的这个问题的最佳解决方案是使用新的git 子树。在撰写本文时,它们不在官方 git 源中,因此需要手动安装,但它们将包含在 v1.7.11 中。目前它可以在 homebrew 和 apt-get 上使用。那么这是一个做的情况
git subtree add -P /node_modules/someprivatemodue git@github.......someprivatemodule {master|tag|commit}
Run Code Online (Sandbox Code Playgroud)
这会增加存储库的大小,但是通过使用 gitsubtree pull 执行上面的命令可以轻松更新。
| 归档时间: |
|
| 查看次数: |
15086 次 |
| 最近记录: |