簡體   English   中英

如何將 jekyll _site 目錄推送到 gh-pages 分支,並將源代碼保留在 master 中?

[英]How do I push jekyll _site directory to gh-pages branch, and leave the source in master?

我有一個由頁面(不是帖子)組成的基本 jekyll 站點,但是因為我想在列出頁面時對其進行排序,所以我不得不使用Jekyll-Sort插件(有點奇怪的排序頁面沒有內置到 jekyll 中)。

因為我使用的是插件,所以我無法利用 GitHub 的自動 jekylling。 所以我想將項目的源代碼推送到master分支,並將_site目錄推送到gh-pages分支。

我不知道如何做到這一點 - 我嘗試在_site目錄中添加一個 git repo 以將其推送到gh-pages但每次我運行jekyll它都會刪除整個目錄並且我丟失了.git文件夾。

有什么建議么? 還是一種本地排序的方法?

一個不那么痛苦的解決方案:

  1. 檢查您的分支,您的構建源所在的位置(可能是srcmaster
  2. (可選:將_site添加到您的.gitconfig中,因此如果尚未完成,它會被忽略)
  3. 刪除_site目錄的所有內容:

    $ rm -r _site/*

  4. 將你的 repo 的gh-pages分支克隆到_site目錄中:

    $ git clone -b gh-pages `git config remote.origin.url` _site

最后步驟:讓 jekyll 構建、提交和推送:

$ jekyll build ,

cd進入_site

$ cd _site ,

將所有文件作為提交的目標:

$ git add -A ,

提交他們:

git commit -am 'Yeah. Built from subdir'

並將您的網站推送到 GitHub-Pages:

git push

我自己用一個shell腳本做了一段時間。

解決方案 1。

創建一個排除 _site/ 文件夾的 .gitignore。 然后讓你的 shell 腳本檢查你是否在 master 上,如果是,添加所有更改的文件並提交它們。 然后將 _site/ 文件夾移動到一個臨時文件夾。 切換到 gh-pages 分支並復制臨時文件夾。 添加所有並提交。 同時推送 master 和 gh-pages 分支。

解決方案 2. 將 _site/ 文件夾的內容復制到另一個 repo,該 repo 是您正在使用的 repo 的精確克隆,但已在 gh-pages 分支上簽出。 然后只需從源 repo 推送 master 分支,從另一個 repo 推送 gh-pages 分支

我也在尋找它,所以如果你想使用當前 GitHub 頁面列表中不支持的第三方插件,你必須手動構建靜態文件並將其推送到遠程分支 gh-pages,讓它只使用靜態文件。 正如他們在他們的頁面GitHub Docs中指出的那樣

因此,要發送靜態文件並僅讓遠程分支 gh-pages 中的構建內容執行以下操作:

  1. 首先確保您的 GitHub 存儲庫中已經有分支 gh-pages;
  2. 在您的項目中創建一個名為“_site_ghpages”的文件夾;
  3. 將其添加到您的 .gitigore 文件中;
  4. 在 Git bash/shell 中,將目錄更改為文件夾“_site_ghpages”:
cd _site_ghpages
  1. 僅將 GitHub 的遠程分支 gh-pages 克隆到文件夾 '_site_ghpages' 中:
git clone --branch gh-pages `git config remote.origin.url` _site_ghpages
  1. 刪除所有內容(如果有);
  2. 復制構建文件夾“_site”的所有內容並粘貼到“_site_ghpages”文件夾中;
  3. 在文件夾 _site_ghpages 添加並提交所有文件:
  git add .
  git commit -m "My commit message"
  git push

  1. 返回上一個目錄。 cd ..

這樣每次你更新你的內容,你應該去文件夾'_site_ghpages'; 清理; 從文件夾“_site”復制內容; 粘貼到_site_ghpages,打開文件夾'_site_ghpages'中的git bash; 添加所有文件,提交,最后推送到遠程分支 gh-pages。


老實說,所有這些過程有點無聊。 因此,為了解決這個問題,我在 bash 中創建了一個腳本,它可以自動完成上述所有操作。

我把它放在一個存儲庫中,所以如果你喜歡你可以在這里查看並下載它。


如何使用bash 腳本

首先確保:

  1. 忽略 .gitignore 文件中的 PUSH_FOLDER ('_site_ghpages');
  2. 已經在您的存儲庫中創建了一個名為“ gh-pages ”的分支(運行腳本后,該分支將只有構建文件夾的靜態文件)。

要首先執行腳本,您應該允許它在您的機器中作為可執行文件運行,為此,請使用 Bash/Git Bash 運行:

chmod +x push_ghpages.sh

之后只需運行文件調用:

. push_ghpages.sh

當不帶參數調用時,腳本會使用“自動提交”消息推送。 因此,如果您想設置提交消息,只需將其作為參數傳遞,如下所示:

. push_ghpages.sh "Your commit message goes here."

如果您在執行時遇到任何問題,只需刪除 PUSH_FOLDER,清空遠程 gh-pages 並讓腳本重新構建並推送內容。

還要進入PUSH_FOLDER cd my_push_folder_directory並運行一個git branch來檢查它是否只有分支gh-pages ,如果它有一個 master 分支或任何其他分支,請將其刪除。

#! /bin/bash
# Author: kleber_germano@outlook.com
# This is a script used to automatically deploy a Jekyll website/blog with third party plugins to GitHub Pages.

BUILD_FOLDER="_site";
PUSH_FOLDER="_site_ghpages"; 
COMMIT_MESSAGE=$1

#Remove all the content from the "PUSH_FOLDER".
function removeAllContentFromPushFolder(){
    rm -r $PUSH_FOLDER/*;
}

#Create the folder "PUSH_FOLDER".
function createFolderToPush(){
    mkdir $PUSH_FOLDER
}

#Copy all the content from the folder _site to PUSH_FOLDER.
function copySiteToFolder(){
    cp -r $BUILD_FOLDER/. $PUSH_FOLDER
}

#Clone only the branch "gh-pages" to the folder "PUSH_FOLDER". 
function cloneGhpages(){
    git clone --branch gh-pages `git config remote.origin.url` $PUSH_FOLDER
}

function prepareThePushFolder(){
    if [[ -d ./$PUSH_FOLDER ]]
    then
        #Remove all the content from the folder "PUSH_FOLDER".
        removeAllContentFromPushFolder
    else
        #Create the folder "PUSH_FOLDER" if it doesn't exist.
        createFolderToPush
        #Call the function that clone the branch "gh-pages" to the folder "PUSH_FOLDER". 
        cloneGhpages
        #Remove any prevous content from the folder "PUSH_FOLDER".
        removeAllContentFromPushFolder

    fi
    #Call the function that copy all the content from the folder _site to "PUSH_FOLDER".
    copySiteToFolder
}

function changeDirectoryToGhpages(){
    cd $PUSH_FOLDER
}

function setMessageCommit(){
    if ! [ "$COMMIT_MESSAGE" ]
    then
      COMMIT_MESSAGE='Automatic Commit'
    fi
}

function pushBranchGhpages(){
    git add .
    git commit -m "$COMMIT_MESSAGE"
    git push
}

function changeDirectoryBack(){
   cd ..
}

prepareThePushFolder
changeDirectoryToGhpages
setMessageCommit
pushBranchGhpages
changeDirectoryBack

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM