别名未产生预期结果

Alias not producing expected result

在我的 .bashrc 中,我有以下别名设置:

alias sitebuild='bundle exec jekyll build; git checkout gh-pages; git rm * && mkdir temp && mv * temp/ && mv temp/_site/* . && rm -rf temp && touch .nojekyll; git status; git add .; git commit -am "update"; git push --all origin'

关于我为什么要这样做的一些解释:我正在使用 Jekyll 和 gh-pages 构建一个站点,除了我已经开始使用不受 GitHub 支持的插件和所以我必须在本地构建站点并将其推送到存储库(而不是让 GH 远程为我构建站点)。

我的系统如下:我在分支 static_build 上的网站上工作,进行提交并构建 _site/(包含所有最终 HTML)。然后我需要将 static_build:_site/ 的内容复制到 gh-pages:_site/ 推送那个并且只推送那个 .

据我所知,上面的命令 sitebuild 应该为我做到这一点(事实上 当我 运行 每个参数分别在 shell);然而,它没有。

知道为什么吗?


编辑:我觉得这可能与

有关
mv * temp/

当我 运行 每个命令单独输出时

mv: rename temp to temp/temp: Invalid argument

似乎在这种情况下终端会抱怨但仍然符合(ls 表明目录中只剩下 temp),但是当别名是运行?

编辑 2: 使用

mv `ls -A | grep -v temp` ./temp

解决了将文件夹移动到自身内部的错误,但不是主要问题。

正如评论中指出的那样,一个问题(如果不是问题)是试图移动temp进入自身。避免这种情况(并用 shell 函数替换您的别名)的一种方法是使您的临时目录成为 * 不匹配的内容。假设您没有使用 bashdotglob 选项:

sitebuild () {
    bundle exec jekyll build
    git checkout gh-pages
    git rm * -r --ignore-unmatch &&
      mkdir .temp && mv * .temp/ && mv .temp/_site/* . && rm -rf .temp &&
      touch .nojekyll
    git status
    git add .
    git commit -am "update"
    git push --all origin
}

[automatically] building a site using Jekyll and gh-pages (...) locally and push it to the repo

OP temp 问题

首先我认为 temp 本身的使用是一个容易产生问题的来源

mv * .temp/               
&& mv .temp/_site/* .        # keep the generated _site folder
&& rm -rf .temp              # and remove ALL the jekyll sources!
&& touch .nojekyll`

运行源文件夹中的上述命令将删除重建站点所需的所有源。

一般解决方案

  1. 从源文件夹构建:./
  2. 转到目标文件夹:_site/,并将其内容推送到您的 deploy_branch:gh-pages
  3. 可选,返回源文件夹,源分支:static_build

使用以下命令:

# 1
bundle exec jekyll build
# 2
&& cd _site
&& git checkout -b gh-pages
&& touch .nojekyll
&& git add . && git commit -am "update"
&& git push origin gh-pages
# 3
&& cd ../ && git checkout -b static_build

(另外:上面的命令应该是 one-liner,删除 #,然后删除 line break 或使用 \ 转义以使其工作)

有关如何配置 GitHub 以使用 non-supported Jekyll 站点插件的详细说明,请查看此

+ 使用 Rake.

自动化部署过程

rake 是一个简单的 ruby 构建程序,可以让您更好地控制构建任务。所以你可以在出错的情况下提前退出,或者如果 git 仓库是干净的则不要更新。

  • 添加一个Rakefile到主文件夹
  • 在其中定义您的任务(例如 deploy 任务)
  • 如果您将 "deploy" 定义为默认任务,则按 运行 rake deployrake 部署。

例如 一个简单的 Rakefile 可以帮助您完成上述操作。

# == Helpers ======================================

# return `false` in case of error
#        `nil`   if git output doesn't contain "clean"
#        "clean" (a truthy value) if it contains it
def clean?
  puts status = `git status`
  clean = ($? == 0) && status.match(/clean/)
end

# == Tasks ========================================

task :build do
  system "bundle exec jekyll build"
end

task :deploy => [:build] do

  Dir.chdir("_site") do

    system "git checkout -b gh-pages"

    exit if clean? == false
    unless clean?
      system "git add . && git commit -am 'update'"
    end

    system "git push --all origin"
  end
end