Skip to content

Instantly share code, notes, and snippets.

@kaizhu256
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaizhu256/f3db4d203675bbd304e0 to your computer and use it in GitHub Desktop.
Save kaizhu256/f3db4d203675bbd304e0 to your computer and use it in GitHub Desktop.
travis encrypt secrets using shell commands
#!/bin/sh
function shTravisEncrypt () {
## this shell function travis encrypts the secret $2 for github repo $1
## tested to work on linux and osx (mountain lion)
local GITHUB_REPO="$1"
local SECRET="$2"
printf "fetching public rsa key from https://api.travis-ci.org/repos/$GITHUB_REPO/key ...\n"
curl -3Ls https://api.travis-ci.org/repos/$GITHUB_REPO/key\
| perl -ne 's/[^-]*//; s/"[^"]*$//; s/\\n/\n/g; s/ RSA / /g; print'\
> /tmp/id_rsa.pub
printf "encrypting value with fetched public rsa key ...\n"
printf "$SECRET"\
| openssl rsautl -encrypt -pubin -inkey /tmp/id_rsa.pub\
| base64\
| tr -d "\n"
printf "\n"
}
# usage example
# $ source travis-encrypt.sh
# $ shTravisEncrypt kaizhu256/utility2 "SECRET=open-sesame"
# fetching public rsa key from https://api.travis-ci.org/repos/kaizhu256/utility2/key ...
# encrypting value with fetched public rsa key ...
# AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment