You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在GitHub托管网站子目录用Jekyll生成静态站点(单仓库实现)?

Can I Use Jekyll for a Single Subdirectory in My GitHub Pages Main Repository?

Absolutely! You don’t have to spin up a separate blog repository to get example.github.io/blog running as a Jekyll-generated section—you can set this up entirely within your main example.github.io repo. Here’s a step-by-step breakdown of how to make it work:

  • Organize your repository structure
    Start by creating a blog folder in your main repo’s root. Inside this blog directory, set up a full Jekyll project structure: you’ll need _posts (for your blog posts), _layouts (for page templates), _includes (reusable components), and a _config.yml file. The root of your repo can stay as your regular static site (plain HTML, CSS, JS files that don’t need Jekyll processing).

  • Configure Jekyll for the subdirectory
    Open the _config.yml file inside your /blog folder and add this line to ensure Jekyll builds paths correctly for the subdirectory:

    baseurl: "/blog"
    

    This tells Jekyll that all links, assets, and generated content should be prefixed with /blog, so things like post URLs will point to example.github.io/blog/your-post instead of just example.github.io/your-post.

  • Set up a GitHub Actions workflow to build the blog
    By default, GitHub Pages will only build Jekyll from the repo root if it detects a _config.yml there. To make it build your /blog subdirectory instead, create a custom workflow:

    1. In your repo root, make a .github/workflows directory.
    2. Inside that, create a file (e.g., build-blog.yml) with this content:
      name: Auto-Build Jekyll Blog Subdirectory
      on: [push]
      jobs:
        build-blog:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v4
            - name: Set up Ruby environment
              uses: ruby/setup-ruby@v1
              with:
                ruby-version: '3.1' # Match the Ruby version GitHub Pages uses
                bundler-cache: true # Caches gems for faster builds
            - name: Build the Jekyll site
              working-directory: ./blog
              run: bundle exec jekyll build --destination ../blog
            - name: Commit and push the built files
              run: |
                git config --global user.name "GitHub Actions Bot"
                git config --global user.email "actions@github.com"
                git add blog/
                git commit -m "Auto-build Jekyll blog content" || echo "No changes to commit"
                git push
      

    This workflow runs every time you push code to your repo: it builds the Jekyll site from the /blog folder, outputs the static files back into the same /blog directory, and commits/pushes those built files. Since GitHub Pages serves static content directly from your repo, the built blog will be live at example.github.io/blog.

  • Key things to keep in mind

    • Don’t have a root-level _config.yml in your main repo—if you do, GitHub Pages will default to building the entire site with Jekyll, which you don’t want. If you need one for some reason, adjust your workflow to override this behavior.
    • To test locally, navigate to the /blog folder in your terminal, run bundle install to install Jekyll dependencies, then bundle exec jekyll serve—you’ll be able to preview your blog at localhost:4000/blog.

内容的提问来源于stack exchange,提问作者user6745003

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:38:00