Committing via travis ci failing

tusharmath picture tusharmath · Aug 3, 2013 · Viewed 9.1k times · Source

I am trying to use grunt-gh-pages extension to commit to my gh-branch. It works fine locally but when I use TRAVIS-CI it fails. It gives the following error message -

Warning: fatal: remote error: 
  You can't push to git://github.com/tusharmath/tusharm.com.git
  Use https://github.com/tusharmath/tusharm.com.git
 Use --force to continue.

And when I update the repo option I get the following error -

Warning: remote: Anonymous access to tusharmath/tusharm.com.git denied.
fatal: Authentication failed for 'https://github.com/tusharmath/tusharm.com.git/'
 Use --force to continue.
Aborted due to warnings.

So basically I just want Travis-ci to commit the files in the gh-pages branch of my repo. Is there a way to do that?

Update The final .travis.yml that solved the problem

language: node_js
node_js:
  - '0.11'
before_script:
  - git config --global user.email "[email protected]"
  - git config --global user.name "Travis-CI"
after_script:
  - git config credential.helper "store --file=.git/credentials"
  - echo "https://${GH_TOKEN}:@github.com" > .git/credentials
  - node ./node_modules/grunt-cli/bin/grunt release
env:
  global:
    secure: {"lots-of-seemingly-random-characters"}

Answer

sarahhodne picture sarahhodne · Aug 3, 2013

You certainly can! The first issue, like you discovered, is due to using the git:// URL to push to, but the git protocol can only be used to clone repositories.

As for the "anonymous access denied" error, that's because you need to let Travis log in to your GitHub account in order to push to the repository. Now, you probably don't want to give Travis your GitHub password, and you certainly don't have to. Instead we're going to use OAuth tokens. If you have no idea what that means, don't worry, I'll explain. An OAuth token in most cases works like a password, but it's easier to revoke access to single things.

To generate an OAuth token, go to the GitHub Applications settings page and click "Create new token" under "Personal API Access Token". You probably want to add a note for what this is, that way it's easier to keep track of and easier to revoke if you need to in the future. Note that this token is essentially a password in that it gives access to the same things a password does.

Then, you need to add the token to your .travis.yml file. First, we'll encrypt the token so only Travis can see it. For this, you need the travis Rubygem installed: gem install travis.

travis encrypt GH_TOKEN="the-token-from-github" --add

Your .travis.yml should now look something like this:

…
env:
  global:
    - secure: "lots-of-seemingly-random-characters"
…

Now, in order for Travis to actually use this token, you need to add some more things to your .travis.yml too.

after_script:
  - git config credential.helper "store --file=.git/credentials"
  - echo "https://${GH_TOKEN}:@github.com" > .git/credentials
  - node ./node_modules/grunt-cli/bin/grunt release

This first tells git to look for credentials in the .git/credentials file. This can be any file you want, really, but make sure it's not one you're going to push to GitHub. Then, we add the token to the .git/credentials file. Git now knows that for pushes to https://github.com, it can use your token to authenticate.

You should be all set!

PS: If you only want to push to GitHub if the build passes, you can change after_script to after_success.