git 如何从 Travis CI 发布到 Github Pages?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23277391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:58:48  来源:igfitidea点击:

How to publish to Github Pages from Travis CI?

gitgithubtravis-cidoxygengithub-pages

提问by Stasik

We are compiling Doxygen docs on the travis-ci server and want to push them onto our gh-pages branch.

我们正在 travis-ci 服务器上编译 Doxygen 文档,并希望将它们推送到我们的 gh-pages 分支。

How do I handle the authorization for git push? Does someone have an example for using encrypted variables in travis-ci? Should I go for https authorization or for an SSH key?

我如何处理授权git push?有人有在 travis-ci 中使用加密变量的例子吗?我应该去 https 授权还是 SSH 密钥?

回答by Yann TM

I don't know how recent it is, but Travis now have a built-in deployment option, basically add to your travis file :

我不知道它有多近,但 Travis 现在有一个内置的部署选项,基本上添加到你的 travis 文件中:

deploy:
  provider: pages
  skip_cleanup: true
  local_dir: myfolder/  # or remove this line to upload from root of repo
  github_token: $GITHUB_TOKEN # Set in travis-ci.org dashboard
  on:
    branch: master

Make sure you don't have a .gitignore in the uploaded folder ; it only uploads non ignored files.

确保上传的文件夹中没有 .gitignore ;它只上传未被忽略的文件。

See the online official doc from travis : https://docs.travis-ci.com/user/deployment/pages/

请参阅 travis 的在线官方文档:https: //docs.travis-ci.com/user/deployment/pages/

There is no public key issue using "Repository Settings" approach, you generate a key in Github then copy paste it into secret/non visible fields of Travis.

使用“存储库设置”方法没有公钥问题,您在 Github 中生成一个密钥,然后将其复制粘贴到 Travis 的秘密/不可见字段中。

Upload history issue :Note that each upload crushes any previously uploaded data, without preserving history.

上传历史问题:请注意,每次上传都会粉碎任何先前上传的数据,而不会保留历史记录。

  • You can now (Nov 2017+) instead preserve history by adding a keep_history: trueline

  • This may be desirable as these snapshot builds can be voluminous, and they are reproducible at will anyway (simply branch your depot back from the revision you want). Pointing to such artifacts is typically pointing to a last successful build of a snapshot.

  • However to trigger storage to a stable place, simply edit your travis to add flag :
    target_branch: Branch to push force to, defaults to gh-pages
    E.g target_branch : rc1.2

  • 您现在可以(2017 年 11 月+)通过添加keep_history: true一行来代替保留历史记录

  • 这可能是可取的,因为这些快照构建可能很庞大,而且它们无论如何都可以随意复制(只需将您的仓库从您想要的修订版分支回来)。指向此类工件通常是指向上次成功构建的快照。

  • 然而,要触发存储到一个稳定的地方,只需编辑你的 travis 以添加标志:
    target_branch:强制推送到的分支,默认为 gh-pages
    例如target_branch:rc1.2

And run it once before setting it back to snapshot mode.

并在将其设置回快照模式之前运行一次。

Another alternative that might be good for releases (I haven't personally tested though) is to publish to a Tag see : https://docs.travis-ci.com/user/deployment/releases/

另一种可能适用于发布的替代方法(不过我还没有亲自测试过)是发布到标签,请参阅:https: //docs.travis-ci.com/user/deployment/releases/

回答by joshua-anderson

The travis-ci documentation hererecommends adding this to push to a git repo:

此处travis-ci 文档建议将此添加到推送到 git 存储库:

after_success:
   - chmod 600 .travis/deploy_key.pem # this key should have push access
   - ssh-add .travis/deploy_key.pem
   - git remote add deploy DEPLOY_REPO_URI_GOES_HERE
   - git push deploy

However, this is insecureas it has you store your unprotected private key in the github repository.

但是,这是不安全的,因为它让您将未受保护的私钥存储在 github 存储库中。

Instead you can add your ssh key as a encrypted environmental variableusing the travis tool:

相反,您可以使用 travis 工具将 ssh 密钥添加为加密的环境变量

travis encrypt DEPLOY_KEY=<private ssh key with write access> --add env.matrix

Now you just need to add this line to the beginning of after_success:

现在您只需要将此行添加到 after_success 的开头:

cat $DEPLOY_KEY > .travis/deploy_key.pem

Please note that after_success will toggle in every build in the build matrix so if you have multiple jobs per build your code will get pushed multiple times, which won't do anything but is good to know that it is occurring.

请注意 after_success 将在构建矩阵中的每个构建中切换,因此如果您每个构建有多个作业,您的代码将被多次推送,这不会做任何事情,但很高兴知道它正在发生。

回答by Stasik

Just to add another solution, I used a HTTPS token from github, encrypted it and used HTTPS for checkouts and pushes

只是为了添加另一个解决方案,我使用了来自 github 的 HTTPS 令牌,对其进行加密并使用 HTTPS 进行结账和推送

回答by iBug

I just wrote a blog about this some days ago. Here's the brief:

几天前我刚刚写了一篇关于这个的博客。这是简短的:

I wrote a custom deploy scriptfor this purpose. The core functionality of the script looks like this:

为此,我编写了一个自定义部署脚本。该脚本的核心功能如下所示:

#!/bin/bash

git clone --depth=1 --branch=master "https://github.com/iBug/iBug.github.io.git" deploy
cd deploy
git rm -rf .
cd ..
mv _site/* deploy
cd deploy
git add --all
git config user.name "Travis CI"
git config user.email "[email protected]"
git commit --message "Auto deploy from Travis CI"
git remote add deploy "https://[email protected]/iBug/iBug.github.io.git" &>/dev/null
git push deploy master &>/dev/null

Now go to https://github.com/settings/tokensand generate a token. Grant it public_repoprivilege. Go to repository settings on Travis CI and store the token with the variable name being GH_TOKEN.

现在去https://github.com/settings/tokens并生成一个令牌。授予它public_repo特权。转到 Travis CI 上的存储库设置并使用变量名称存储令牌GH_TOKEN

Add the deploy script to travis:

将部署脚本添加到 travis:

script: bundle exec jekyll build
after_success:
    - bash .travis/deploy.sh

Push these things to GitHub and Travis will be triggered.

把这些东西推送到 GitHub 上就会触发 Travis。



My blog is here. It's comprehensive and thus redundant if posted as an answer here (because Stack Overflow users are mostly experienced developers). The script I posted in my blog also lacks a functionality: It does not preserve commit history of the built site, whereas the script in this answer above does.

我的博客在这里。如果在这里作为答案发布,它是全面的,因此是多余的(因为 Stack Overflow 用户大多是有经验的开发人员)。我在我的博客中发布的脚本也缺乏一个功能:它不保留已建站点的提交历史记录,而上面这个答案中的脚本却保留了。