Skip to content

Commit e7880b1

Browse files
christianklotzbubenshchykov
authored andcommitted
Make the buildpack work with official Heroku multiple buildpacks (debitoor#1)
* Make the buildpack work with official Heroku multiple buildpacks To allow the buildpack to be used with other buildpacks the way Heroku recommends, see https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app, the compile step now creates a `.ssh` directory in the `BUILD_DIR` and only sym-links to it for the duration of the buildpack compilation. Furthermore, this version includes a way to customise the hosts that should be set up for `ssh` by using an environment variable. By default it's connecting to github.com as before. * Update README to include details about usage with heroku-buildpack-multi
1 parent c2225c0 commit e7880b1

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
# ssh-private-key-buildpack
22

3-
A heroku buildpack for setting the ssh private key as part of the application build. It's meant to be used with [heroku-buildpack-multi](https://github.com/heroku/heroku-buildpack-multi), before other buildpacks which require the key to be present, like installing private `npm` modules from `github`.
3+
A Heroku buildpack for setting the ssh private key as part of the application build. It's meant to be used as part of a setup [using multiple buildpacks](https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app), so other buildpacks can authenticate with hosts using ssh keys, for instance to install dependencies from private git repositories.
44

55
# Example usage
66

7-
Upload the private key to heroku (note that the key needs to be base64 encoded).
7+
## Configure Multiple Buildpacks
8+
### _Option 1:_ Heroku CLI or Dashboard
9+
Add the buildpack to your Heroku app either using the CLI or the Heroku dashboard. The `ssh-private-key-buildpack` needs to run before any buildpack trying to get ssh access. In the following example, it runs before the `heroku/go` buildpack.
810

9-
```
10-
heroku config:set SSH_KEY=$(cat ~/.ssh/id_rsa | base64)
11-
```
11+
$ heroku buildpacks:set --index 1 https://github.com/debitoor/ssh-private-key-buildpack.git
12+
$ heroku buildpacks:add heroku/go
1213

13-
Add a `.buildpacks` file (used by `heroku-buildpack-multi`) which contains this and the default node.js buildpack.
14+
### _Option 2:_ Use `heroku-buildpack-multi`
15+
Instead of setting the buildpacks directly with Heroku they can also be configured using a `.buildpacks` in combination with [`heroku-buildpack-multi`]( https://github.com/heroku/heroku-buildpack-multi).
1416

15-
```
16-
https://github.com/debitoor/ssh-private-key-buildpack.git#v1.0.0
17-
https://github.com/heroku/heroku-buildpack-nodejs.git#v75
18-
```
17+
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git
18+
The same example given for the CLI use would have the following `.buildpacks` file.
1919

20-
Now as long as the public key is present on github and the user has the correct permissions, it's possible to install `npm` modules from private `githup` repositories.
20+
$ cat .buildpacks
21+
https://github.com/debitoor/ssh-private-key-buildpack.git
22+
https://github.com/heroku/heroku-buildpack-go
23+
24+
## Configure SSH Key
25+
26+
Set the private key environment variable `SSH_KEY` of your Heroku app (note that the key needs to be base64 encoded).
27+
28+
$ heroku config:set SSH_KEY=$(cat path/to/your/keys/id_rsa | base64)
29+
30+
By default the buildback adds Github to `known_hosts`. However you can configure your app to allow custom hosts, too. All that's needed is the set `SSH_HOSTS` for you app to a comma-separated list of hosts, e.g. `git@github.com,example.com`
31+
32+
$ heroku config:set SSH_HOSTS="git@github.com,example.com"

bin/compile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ function indent() {
88
esac
99
}
1010

11-
env_dir=$3
12-
ssh_key="$(cat $env_dir/SSH_KEY)"
11+
ENV_DIR=${3:-}
12+
ssh_key="$(cat $ENV_DIR/SSH_KEY)"
13+
ssh_hosts=${SSH_HOSTS:-"git@github.com"}
1314

1415
if [ "$ssh_key" != "" ]; then
1516
echo "-----> Running SSH private key setup"
1617

17-
mkdir "$HOME/.ssh"
18+
# The .ssh needs to be located in the home directory which is different to the
19+
# home directory of the built machine. The symlink resolves the issue.
20+
mkdir "$1/.ssh"
21+
ln -s "$1/.ssh" "$HOME/.ssh"
1822
echo "$ssh_key" | base64 --decode > "$HOME/.ssh/id_rsa"
19-
ssh -oStrictHostKeyChecking=no -T git@github.com 2>&1 | indent
23+
24+
IFS=',' read -ra HOST <<< "$ssh_hosts"
25+
for i in "${HOST[@]}"; do
26+
ssh -oStrictHostKeyChecking=no -T $i 2>&1 | indent
27+
done
28+
2029
exit 0
2130
else
2231
echo "-----> No SSH private key"

0 commit comments

Comments
 (0)