Skip to content

Instantly share code, notes, and snippets.

@dacbarbos
Forked from koter84/travis_secure_private_key.sh
Last active April 18, 2017 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacbarbos/e391b6df600008bbc41759b1a4775972 to your computer and use it in GitHub Desktop.
Save dacbarbos/e391b6df600008bbc41759b1a4775972 to your computer and use it in GitHub Desktop.
Create a private key-pair and encrypt it for use in .travis.yml with working code for decrypting it on both Linux and macOS builders
#!/bin/env bash
ssh-keygen -t rsa -N "" -C travis -f ./travis_key
# tested on Linux only (according the author)
# on macOS you'll need gsplit instead of split
# decryption should work on both Linux and macOS travis-workers
# official doc/help page https://docs.travis-ci.com/user/encryption-keys
# see also http://stackoverflow.com/questions/18027115/committing-via-travis-ci-failing
echo "encrypt private"
base64 --wrap=0 ./travis_key > ./travis_key_base64
ENCRYPTION_FILTER="echo \$(echo \" - secure: \")\$(travis encrypt \"\$FILE='\`cat $FILE\`'\" -r koter84/tg)"
split --bytes=100 --numeric-suffixes --suffix-length=2 --filter="$ENCRYPTION_FILTER" ./travis_key_base64 id_rsa_
echo "encrypt public"
base64 --wrap=0 ./travis_key.pub > ./travis_key_base64.pub
ENCRYPTION_FILTER="echo \$(echo \" - secure: \")\$(travis encrypt \"\$FILE='\`cat $FILE\`'\" -r koter84/tg)"
split --bytes=100 --numeric-suffixes --suffix-length=2 --filter="$ENCRYPTION_FILTER" ./travis_key_base64.pub id_rsa_pub_
## The parts below go in the .travis.yml file, and work on Linux and macOS hosts
- echo "decrypt private"
- for i in {0..30}; do eval $(printf "echo \$id_rsa_%02d\n" $i) >> ~/.ssh/id_rsa_base64; done
- base64 --decode ~/.ssh/id_rsa_base64 > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- echo "decrypt public"
- for i in {0..10}; do eval $(printf "echo \$id_rsa_pub_%02d\n" $i) >> ~/.ssh/id_rsa_base64.pub; done
- base64 --decode ~/.ssh/id_rsa_base64.pub > ~/.ssh/id_rsa.pub
- chmod 600 ~/.ssh/id_rsa.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment