Committing via travis ci failing

你说的曾经没有我的故事 提交于 2019-12-28 09:13:14

问题


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 "tusharmath@gmail.com"
  - 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"}

回答1:


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.




回答2:


The answer by henrikhodne is great, but the solution doesn't work with grunt-gh-pages because it creates another Git repository somewhere in .grunt/grunt-gh-pages/ sub-directory. Therefore git config made in after_script or after_success section is not used by grunt-gh-pages.

It's possible to add GH_TOKEN to repository URL used by grunt-gh-pages in Gruntfile.js like this:

'gh-pages': {
    // your common gh-pages config
    travis: {
        options: {
            repo: 'https://' + process.env.GH_TOKEN + '@github.com/dim2man/csbrowser.git',
            silent: true
        },
        src: ['**']
    }
}

Note the silent: true option, it prevents publishing your token value in Travis logs.

Then your after_script or after_success section can be modified like:

after_success: grunt gh-pages:travis


来源:https://stackoverflow.com/questions/18027115/committing-via-travis-ci-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!