Skip to content

Instantly share code, notes, and snippets.

@brunodb3
Last active June 21, 2021 11:28
Show Gist options
  • Save brunodb3/0577c35d38d00ba04a05c31fa7bcadac to your computer and use it in GitHub Desktop.
Save brunodb3/0577c35d38d00ba04a05c31fa7bcadac to your computer and use it in GitHub Desktop.
Set up GitHub deploy keys for Travis

Set up GitHub deploy keys for Travis

Based on qoomon's Gist.


See the Travis documentation for more information.

These commands should be run on the root of your project/repository.

Install Travis CLI locally:

gem install travis or brew install travis if on OSX

Generate a new SSH Key for GitHub access:

ssh-keygen -t rsa -b 4096 -f 'github_deploy_key' -N ''

Copy the contents of the public key and set as a deploy key on GitHub (example: https://github.com/<USER>/<REPOSITORY>/settings/keys)

cat github_deploy_key.pub

Login to Travis CLI

  • if you want to use travis-ci.com instead of .org, add --pro to the command. See the travis cli docs for more details.

travis login --pro --auto

Encrypt the GitHub deploy key

  • again, if you want to use travis-ci.com, add --pro to the command.
  • this will add two keys on your Travis dashboard, which you will use below
    • $encrypted_xxxxxxxxxxxx_key
    • $encrypted_xxxxxxxxxxxx_iv

travis encrypt-file --pro 'github_deploy_key'

Add the encoded deploy key to the GitHub repository

git add 'github_deploy_key.enc'

Add openssl line on your .travis.yml

  • replace $encrypted_xxxxxxxxxxxx_key and $encrypted_xxxxxxxxxxxx_iv with the output of travis encrypt-file
  • you can also do this automatically by running travis encrypt-file --add
before_install:
  - >-
    openssl aes-256-cbc 
    -K $encrypted_xxxxxxxxxxxx_key 
    -iv $encrypted_xxxxxxxxxxxx_iv 
    -in github_deploy_key.enc 
    -out github_deploy_key 
    -d
  - chmod 600 github_deploy_key
  - eval $(ssh-agent -s)
  - ssh-add github_deploy_key

Add the changes to .travis.yml to Git and push a new commit

git add '.travis.yml'

git commit -m 'chore: add github deploy key'

git push

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