Skip to content

Instantly share code, notes, and snippets.

@ggggggggg
Last active August 29, 2015 14:14
Show Gist options
  • Save ggggggggg/d5e1e3fa8fa614a29f3c to your computer and use it in GitHub Desktop.
Save ggggggggg/d5e1e3fa8fa614a29f3c to your computer and use it in GitHub Desktop.
Julia: How to use a Secret API Key for automated testing on Travis-CI

If you are writing a package to interface with an API that requires a secret key, it is not a good idea to include a copy of the secret key in your git repository. Wihtout the key however, you can't run automated tests. Travis-CI has a workaround for this, and I'll show you how to use it in Julia. First you need to install the travis gem, if you already have ruby installed this is simply gem install travis. Next we're going to use the travis encrypt feature, go into the git reposity you want to access the key from and do

travis encrypt TEST_SECRET=secretvalue --add

This will add some information to your .travis.yml file that looks like

env:
  global:
  - secure: A8GPJHJzHv71/WLYrsqzSlWRnVKEr/wyoQyjsYyRePRmjWiOFuZax9KC7obZez424F8UgUZCDUMG7VNYoEZphLVMVXU48Rq2IFLueJqDhAi2IZexyU2FaPnSBurU/3P228A92b8sxYXrkM+/w/uKbCuppKUVmRFtjbmA38PrT34=

The effect will be that when you run travis and environment variable named TEST_SECRET will have the value secretvalue. To use this conveniently from within julia I recomend making a file called yourpackage/test/TEST_SECRET that contains secretvalue. Add test/TEST_SECRET to .gitignore, then add this function to runtests.jl. It will check for an enviorment variable named TEST_SECRET and load it if available, otherwise it loads from the file of the same name in yourpackage/test.

function loadsecret(name="TEST_SECRET")
  if name in keys(ENV)
    println("Loading $name from environment variable")
    return ENV[name]
  else
    println("Loading $name from file")
    return open(name) do f
      strip(readline(f))
    end
  end
end
apikey=loadsecret()

ThingSpeak.jl uses this method succesfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment