How to add SSH Key in Travis CI?

21,783

Solution 1

This feature is only available for private repositories currently.

For public repositories, you shouldn't need them to be private Git URLs (assuming they're on GitHub), so changing the URLs to use public clone URLs should do the trick.

Solution 2

Using SSH keys is only available for private repositories on travis-ci.com (paid plans).

With the travis command line tool you can generate a new SSH key which will be set up on both Travis CI and your GitHub user account (if you use a dedicated GitHub user for Travis CI).

Here are the necessary console commands:

# Install Travis command line tool
gem install travis

# Login to Travis Pro (private repositories) account
travis login --pro

# Generate and setup SSH key for your GitHub repository
travis sshkey --generate -r organization/repository

Instead of generating a new SSH key with travis, it's also possible to upload an existing SSH key with:

travis sshkey --upload "C:\my_keys\id_rsa" -r organization/repository

Once the SSH key has been created, it is recommended that this key will be refrenced from the config file in the .ssh directory of your Travis user. You can do this by adding these lines to your .travis.yml:

# http://docs.travis-ci.com/user/build-lifecycle/
before_script:  
  - echo -e "Host github.com\n\tHostName github.com\n\tUser git\n\tIdentityFile ~/.ssh/id_rsa\n" >> ~/.ssh/config

For more information, here is a link to the official documentation: Generating a new key.

Share:
21,783

Related videos on Youtube

acfreitas
Author by

acfreitas

Agile QA, Major in Software Engineering and Master's degree in Computer Science. I have worked with QA and quality process improvement for three years, being an enthusiast about Agile Testing and DevOps.

Updated on July 09, 2022

Comments

  • acfreitas
    acfreitas almost 2 years

    In Travis Doc there is tab "SSH Key" in Settings, but not in my account. I need to add SSH Key to clone submodules on GitHub.

    My Account:

    enter image description here

    Travis Doc:

    enter image description here

  • Xiao Hanyu
    Xiao Hanyu over 7 years
    docs for private repo and ssh key: docs.travis-ci.com/user/private-dependencies
  • logankilpatrick
    logankilpatrick over 5 years
    @Benny I did what you suggested! There were no issues with it. However, when Travis CI goes to try to access my private repo, it still just sits there and waits for the username for my GitHub account. How do I fix this?
  • Benny Neugebauer
    Benny Neugebauer over 5 years
    @logankilpatrick Do you have a "git clone" command in your ".travis.yml" file or how do you try to access your private repo from Travis CI? If you try to clone it, then use a SSH url ([email protected]:ORG/PROJECT.git) instead of a HTTPS url (github.com/ORG/PROJECT.git).
  • xeruf
    xeruf about 5 years
    What if I need to clone a private repository in the build of a public repository?
  • pqnet
    pqnet almost 5 years
    @Xerus if in order to build a public repository you need files from a private one, maybe you're not really developing open source software.
  • xeruf
    xeruf almost 5 years
    It is only for the deployment. I am building an open-source-project, but each time we release a version, it is accompanied by a new version of a small closed-source package. It is much more convenient to build and release both with one CI build.
  • Bradley Kreider
    Bradley Kreider about 4 years
    It sounds like you have the dependency backwards. Make the closed source project deployment depend on the opensource project.