30

我正在尝试使用grunt-gh-pages扩展来提交我的 gh-branch。它在本地运行良好,但是当我使用 TRAVIS-CI 时它失败了。它给出以下错误消息 -

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.

当我更新 repo 选项时,我收到以下错误 -

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.

所以基本上我只想让 Travis-ci 提交我的 repo 的 gh-pages 分支中的文件。有没有办法做到这一点?

更新.travis.yml解决问题的final

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"}
4

2 回答 2

40

你当然可以!如您所见,第一个问题是由于使用git://URL 推送到,但 git 协议只能用于克隆存储库。

至于“匿名访问被拒绝”的错误,那是因为你需要让 Travis 登录到你的 GitHub 账户才能推送到仓库。现在,您可能不想向 Travis 提供您的 GitHub 密码,当然也不必这样做。相反,我们将使用 OAuth 令牌。如果你不知道这意味着什么,别担心,我会解释的。在大多数情况下,OAuth 令牌的作用类似于密码,但更容易撤销对单个事物的访问。

要生成 OAuth 令牌,请转到GitHub 应用程序设置页面,然后单击“个人 API 访问令牌”下的“创建新令牌”。您可能想为这是什么添加注释,这样更容易跟踪,如果您将来需要,也更容易撤销。请注意,此令牌本质上是一个密码,因为它可以访问与密码相同的功能。

然后,您需要将令牌添加到您的 .travis.yml 文件中。首先,我们将加密令牌,以便只有 Travis 可以看到它。为此,您需要travis安装 Rubygem gem install travis:.

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

您的 .travis.yml 现在应该如下所示:

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

现在,为了让 Travis 真正使用这个令牌,您还需要在 .travis.yml 中添加更多内容。

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.

于 2013-08-03T04:33:55.393 回答
6

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
于 2014-03-13T08:58:36.467 回答