我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

此版本的 GitHub Enterprise 已停止服务 2020-11-12. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

使用 Jekyll 创建 GitHub Pages 站点

您可以使用 Jekyll 在新仓库或现有仓库中创建 GitHub Pages 站点。

拥有仓库管理员权限的人员可以使用 Jekyll 创建 GitHub Pages 站点。

GitHub Pages is available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server.

本文内容

基本要求

必须安装 Jekyll 和 Git 后才可使用 Jekyll 创建 GitHub Pages 站点。 更多信息请参阅 Jekyll 文档中的安装和“设置 Git”。

我们建议使用 Bundler 安装和运行 Jekyll。 Bundler 可管理 Ruby gem 依赖项,减少 Jekyll 构建错误和阻止环境相关的漏洞。 要安装 Bundler:

  1. 安装 Ruby。 更多信息请参阅 Ruby 文档中的“安装 Ruby”。
  2. 安装 Bundler。 更多信息请参阅“Bundler”。

提示:如果您在尝试使用 Bundler 安装 Jekyll 时看到 Ruby 错误,您可能需要使用包管理器(例如 RVMHomebrew)来管理您的 Ruby 安装。 更多信息请参阅 Jekyll 文档中的“故障排除”。

为站点创建仓库

如果站点是一个独立的项目,您可以创建新仓库来存储站点源代码。 If your site is associated with an existing project, you can add the source code for your site to a gh-pages branch or a docs folder on the master branch in that project's repository. For example, if you're creating a site to publish documentation for a project that's already on GitHub Enterprise Server, you may want to store the source code for the site in the same repository as the project.

如果要在现有仓库中创建站点,请跳至“创建站点”一节。

Warning: If your site administrator has enabled Public Pages, GitHub Pages sites are publicly available on the internet, even if the repository for the site is private. 如果站点的仓库中有敏感数据,您可能想要在发布前删除它。 For more information, see "Configuring GitHub Pages for your enterprise" and "About repository visibility."

  1. 在任何页面的右上角,使用 下拉菜单选择 New repository(新建仓库)
    带有创建新仓库选项的下拉菜单
  2. 使用 Owner(所有者)下拉菜单选择你想要拥有仓库的帐户。
    所有者下拉菜单
  3. 输入仓库的名称和说明(可选)。 如果您创建的是用户或组织站点,仓库名称必须为 <user>.github.io<organization>.github.io。 更多信息请参阅“关于 GitHub Pages”。
    创建仓库字段
  4. 选择仓库可见性。 更多信息请参阅“关于仓库可见性”。
    选择仓库可见性的单选按钮

创建站点

必须先在 GitHub Enterprise Server 上有站点的仓库,然后才可创建站点。 如果未在现有仓库中创建站点,请参阅“为站点创建仓库”。

  1. 打开 Terminal(终端)Terminal(终端)Git Bash

  2. 如果您还没有本地版仓库,请导航到您想要存储站点源文件的位置,将 PARENT-FOLDER 替换为要包含仓库文件夹的文件夹。

    $ cd PARENT-FOLDER
  3. 如果尚未初始化本地 Git 仓库,请将 REPOSITORY-NAME 替换为仓库名称。

    $ git init REPOSITORY-NAME
    > Initialized empty Git repository in /Users/octocat/my-site/.git/
    # Creates a new folder on your computer, initialized as a Git repository
  4. 将目录更改为仓库。

    $ cd REPOSITORY-NAME
    # Changes the working directory
  5. If you're creating a project site, decide which publishing source you want to use. 更多信息请参阅“关于 GitHub Pages”。

  6. 导航到站点的发布来源。 有关发布源的更多信息,请参阅“关于 GitHub Pages”。 例如,如果选择从默认分支上的 docs 文件夹发布站点,则创建并切换目录到 docs 文件夹。

    $ mkdir docs
    # Creates a new folder called docs
    $ cd docs

    如果选择从 gh-pages 分支发布站点,则创建并检出 gh-pages 分支。

    $ git checkout --orphan gh-pages
    # Creates a new branch, with no history or contents, called gh-pages and switches to the gh-pages branch
  7. 要创建新的 Jekyll 站点,请使用 jekyll new 命令,将 VERSION 替换为当前的 Jekyll 依赖项版本。 更多信息请参阅 GitHub Pages 网站上的“依赖项版本”。

    • 如已安装 Bundler:
      $ bundle exec jekyll VERSION new .
      # Creates a Jekyll site in the current directory
    • 如果尚未安装 Bundler:
      $ jekyll VERSION new .
      # Creates a Jekyll site in the current directory
  8. 打开已创建的 Gemfile,并按照 Gemfile 注释中的说明使用 GitHub Pages。

    更新 Gemfile 的说明

  9. 更新 gem "github-pages" 行,使该行类似如下,将 VERSION 替换为 github-pages 的当前依赖项版本。 更多信息请参阅 GitHub Pages 网站上的“依赖项版本”。

    gem "github-pages", "~> VERSION", group: :jekyll_plugins
  10. 保存并关闭 Gemfile。

  11. (可选)在本地测试您的站点。 更多信息请参阅“使用 Jekyll 在本地测试 GitHub Pages 站点”。

  12. Add your GitHub Enterprise Server repository as a remote, replacing HOSTNAME with your enterprise's hostname, USER with the account that owns the repository, and REPOSITORY with the name of the repository.

    $ git remote add origin https://HOSTNAME/USER/REPOSITORY.git
    
  13. 将仓库推送到 GitHub Enterprise Server,BRANCH 替换为您所操作的分支的名称。

    $ git push -u origin BRANCH
  14. 配置发布源。 更多信息请参阅“配置 GitHub Pages 站点的发布来源”。

  15. 在 GitHub Enterprise Server 上,导航到站点的仓库。

  16. 在仓库名称下,单击 Settings(设置)

    仓库设置按钮

  17. 要查看您已发布的站点,请在“GitHub Pages”下点击您的站点 URL。

    已发布站点的 URL

    注:对站点的更改在推送到 GitHub Enterprise Server 后,最长可能需要 20 分钟才会发布。 如果一小时后仍然在浏览器中看不到您的更改,请参阅“关于 GitHub Pages 站点的 Jekyll 构建错误”。

:如果网站 的源文件位于默认发布源 - master(对于用户和组织站点)或 gh-pages(对于项目站点)- 但您的站点 未自动发布,请确保有一个拥有管理权限和经验证的电子邮件地址的人已推送到默认发布源。

后续步骤

要向站点添加新页面或帖子,请参阅“使用 Jekyll 添加内容到 GitHub Pages 站点”。

您可以将 Jekyll 主题添加到 GitHub Pages 站点,以自定义站点的外观。更多信息请参阅“使用 Jekyll 添加主题到 GitHub Pages 站点”。