Committing via travis ci failing

孤街浪徒 提交于 2019-11-28 04:42:47

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.

dim2man

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