hugo 部署github_从私有Hugo存储库部署公共GitHub Pages站点的两种方法

聂建茗
2023-12-01

hugo 部署github

通过使用持续部署工具从单独的私有存储库发布公共GitHub Pages网站,可以使草稿不受公众的关注。 (Keep your drafts out of the public eye by making use of continuous deployment tools to publish your public GitHub Pages site — from a separate private repository.)

Tools like Travis CI and Netlify offer some pretty nifty features, like seamlessly deploying your GitHub Pages site when changes are pushed to its repository. Along with a static site generator like Hugo, keeping a blog up to date is pretty painless.

Travis CI和Netlify等工具提供了一些漂亮的功能,例如在将更改推送到其存储库时无缝部署GitHub Pages站点。 与静态网站生成器(如Hugo)一起,保持博客最新非常容易。

I’ve used Hugo to build my site for years, but until this past week I’d never hooked up my Pages repository to any deployment service. Why? Because using a tool that built my site before deploying it seemed to require having the whole recipe in one place — and if you’re using GitHub Pages with the free version of GitHub, that place is public. That means that all my three-in-the-morning bright ideas and messy unfinished (and unfunny) drafts would be publicly available — and no amount of continuous convenience was going to convince me to do that.

多年来,我一直使用Hugo来构建网站,但是直到最近一周,我才从未将Pages存储库连接到任何部署服务。 为什么? 因为使用在部署之前构建我的网站的工具似乎需要将整个配方放在一个地方—如果您将GitHub Pages与GitHub的免费版本一起使用,则该地方是公共的 。 这意味着我所有在当下的三个好主意和凌乱的未完成(且很有趣)的草稿都可以公开获得,而持续不断的方便并不能说服我这样做。

So I kept things separated, with Hugo’s messy behind-the-scenes stuff in a local Git repository, and the generated public/ folder pushing to my GitHub Pages remote repository. Each time I wanted to deploy my site, I’d have to get on my laptop and hugo to build my site, then cd public/ && git add . && git commit… etc etc. And all was well, except for the nagging feeling that there was a better way to do this.

因此,我将事情分开了,在本地Git存储库中添加了Hugo凌乱的幕后内容,并将生成的public/文件夹推送到我的GitHub Pages远程存储库中。 每次我想部署站点时,都必须先用笔记本电脑和hugo来构建站点,然后再使用cd public/ && git add . && git commit cd public/ && git add . && git commit …等等等,一切都很好,除了the的感觉是有更好的方法可以做到这一点。

I wrote another article a little while back about using GitHub and Working Copy to make changes to my repositories on my iPad whenever I’m out and about. It seemed off to me that I could do everything except deploy my site from my iPad, so I set out to change that.

不久前,我写了另一篇文章,关于使用GitHub和Working Copy在外出时更改iPad上的存储库。 在我看来,除了可以通过iPad部署网站以外,我可以做所有事情,因此我决定进行更改。

A couple three-in-the-morning bright ideas and a revoked access token later (oops), I now have not one but two ways to deploy to my public GitHub Pages repository from an entirely separated, private GitHub repository. In this post, I’ll take you through achieving this with Travis CI or using Netlify and Make.

我有两个很早就提出的好主意,后来又撤消了访问令牌(哎呀),我现在没有一种方法,而是从完全独立的私有GitHub存储库中部署到我的公共GitHub Pages存储库的两种方法。 在本文中,我将指导您使用Travis CI或使用NetlifyMake实现这一目标。

There’s nothing hackish about it — my public GitHub Pages repository still looks the same as it does when I pushed to it locally from my terminal. Only now, I’m able to take advantage of a couple great deployment tools to have the site update whenever I push to my private repo, whether I’m on my laptop or out and about with my iPad.

它没有什么骇人听闻的地方–我的公共GitHub Pages存储库看起来仍然与从终端本地推送到它时的样子相同。 直到现在,无论我是在笔记本电脑上还是随身携带iPad,无论何时我推送到私人仓库,我都可以利用几个出色的部署工具来更新站点。

This article assumes you have working knowledge of Git and GitHub Pages. If not, you may like to spin off some browser tabs from my articles on using GitHub and Working Copy and building a site with Hugo and GitHub Pages first.

本文假定您具有Git和GitHub Pages的使用知识。 如果不是这样,您可能希望从我有关使用GitHub和工作副本的文章中剥离一些浏览器选项卡, 首先使用Hugo和GitHub Pages构建网站

Let’s do it!

我们开始做吧!

使用Travis CI进行私有到公共的GitHub Pages部署 (Private-to-public GitHub Pages deployment with Travis CI)

Travis CI has the built-in ability (♪) to deploy to GitHub Pages following a successful build. They do a decent job in the docs of explaining how to add this feature, especially if you’ve used Travis CI before… which I haven’t. Don’t worry, I did the bulk of the figuring-things-out for you.

Travis CI具有内置功能(♪), 在成功构建后部署到GitHub Pages 。 他们在解释如何添加此功能的文档中做得不错,尤其是如果您以前使用过Travis CI……我还没有。 不用担心,我为您做了很多事情。

  • Travis CI gets all its instructions from a configuration file in the root of your repository called .travis.yml

    Travis CI从名为.travis.yml的存储库根目录中的配置文件中获取所有指令。

  • You need to provide a GitHub personal access token as a secure encrypted variable, which you can generate using travis on the command line

    您需要提供GitHub个人访问令牌作为安全的加密变量,您可以在命令行上使用travis生成该变量。

  • Once your script successfully finishes doing what you’ve told it to do (not necessarily what you want it to do but that’s a whole other blog post), Travis will deploy your build directory to a repository you can specify with the repo configuration variable.

    一旦脚本成功完成了您要求的操作(不一定是您想要的操作,但这是另一篇博客文章),Travis会将您的构建目录部署到您可以使用repo配置变量指定的存储库中。

设置Travis配置文件 (Setting up the Travis configuration file)

Create a new configuration file for Travis with the filename .travis.yml (note the leading “.”). These scripts are very customizable and I struggled to find a relevant example to use as a starting point - luckily, you don’t have that problem!

为Travis创建一个新的配置文件,文件.travis.yml (注意.travis.yml的“。”)。 这些脚本非常可定制,我很难找到一个相关的示例作为起点-幸运的是,您没有这个问题!

Here’s my basic .travis.yml:

这是我的基本.travis.yml

git:
 depth: false

env:
 global:
 - HUGO_VERSION="0.54.0"
 matrix:
 - YOUR_ENCRYPTED_VARIABLE

install:
 - wget -q https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz
 - tar xf hugo_${HUGO_VERSION}_Linux-64bit.tar.gz
 - mv hugo ~/bin/

script:
 - hugo --gc --minify

deploy:
 provider: pages
 skip-cleanup: true
 github-token: $GITHUB_TOKEN
 keep-history: true
 local-dir: public
 repo: gh-username/gh-username.github.io
 target-branch: master
 verbose: true
 on:
 branch: master

This script downloads and installs Hugo, builds the site with the garbage collection and minify flags, then deploys the public/ directory to the specified repo - in this example, your public GitHub Pages repository. You can read about each of the deploy configuration options here.

该脚本下载并安装雨果,建立与垃圾收集和缩小的部位标志 ,那么部署public/目录到指定的repo -在这个例子中,你的公共GitHub的页面库。 您可以在此处阅读有关每个deploy配置选项的信息

To add the GitHub personal access token as an encrypted variable, you don’t need to manually edit your .travis.yml. The travis gem commands below will encrypt and add the variable for you when you run them in your repository directory.

要将GitHub个人访问令牌添加为加密变量 ,您无需手动编辑.travis.yml 。 在存储库目录中运行它们时,下面的travis gem命令将为您加密并添加变量。

First, install travis with sudo gem install travis.

首先,安装travissudo gem install travis

Then generate your GitHub personal access token, copy it (it only shows up once!) and run the commands below in your repository root, substituting your token for the kisses:

然后生成您的GitHub个人访问令牌 ,将其复制(它只会显示一次!),并在您的存储库根目录中运行以下命令,将您的令牌替换为亲吻:

travis login --pro --github-token xxxxxxxxxxxxxxxxxxxxxxxxxxx
travis encrypt GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxx --add env.matrix

Your encrypted token magically appears in the file. Once you’ve committed .travis.yml to your private Hugo repository, Travis CI will run the script and if the build succeeds, will deploy your site to your public GitHub Pages repo. Magic!

您的加密令牌会神奇地出现在文件中。 将.travis.yml到私有Hugo存储库后,Travis CI将运行该脚本,如果构建成功,会将您的站点部署到公共GitHub Pages回购中。 魔法!

Travis will always run a build each time you push to your private repository. If you don’t want to trigger this behavior with a particular commit, add the skip command to your commit message.

每当您推送到私有存储库时,Travis始终会运行一个构建。 如果您不想通过特定的提交来触发此行为, 请将skip命令添加到您的提交消息中

Yo that’s cool but I like Netlify.

哟,很酷,但我喜欢Netlify。

Okay fine.

好的。

使用Netlify和Make部署到单独的存储库 (Deploying to a separate repository with Netlify and Make)

We can get Netlify to do our bidding by using a Makefile, which we’ll run with Netlify’s build command.

我们可以通过使用Makefile来使Netlify进行投标,该文件将与Netlify的build命令一起运行。

Here’s what our Makefile looks like:

这是我们的Makefile样子:

SHELL:=/bin/bash
BASEDIR=$(CURDIR)
OUTPUTDIR=public
.PHONY: all
all: clean get_repository build deploy
.PHONY: clean
clean:
@echo "Removing public directory"
rm -rf $(BASEDIR)/$(OUTPUTDIR)
.PHONY: get_repository
get_repository:
@echo "Getting public repository"
git clone https://github.com/gh-username/gh-username.github.io.git public
.PHONY: build
build:
@echo "Generating site"
hugo --gc --minify
.PHONY: deploy
deploy:
@echo "Preparing commit"
@cd $(OUTPUTDIR) \
 && git config user.email "you@youremail.com" \
 && git config user.name "Your Name" \
 && git add . \
 && git status \
 && git commit -m "Deploy via Makefile" \
 && git push -f -q https://$(GITHUB_TOKEN)@github.com/gh-username/gh-username.github.io.git master
@echo "Pushed to remote"

To preserve the Git history of our separate GitHub Pages repository, we’ll first clone it, build our new Hugo site to it, and then push it back to the Pages repository. This script first removes any existing public/ folder that might contain files or a Git history. It then clones our Pages repository to public/, builds our Hugo site (essentially updating the files in public/), then takes care of committing the new site to the Pages repository.

为了保留我们单独的GitHub Pages存储库的Git历史记录,我们首先将其克隆,为其构建新的Hugo站点,然后将其推回到Pages存储库。 该脚本首先删除可能包含文件或Git历史记录的所有现有public/文件夹。 然后,它将Pages存储库克隆到public/ ,构建我们的Hugo网站(实质上更新public/的文件),然后负责将新站点提交到Pages存储库。

In the deploy section, you’ll notice lines starting with &&. These are chained commands. Since Make invokes a new sub-shell for each line, it starts over with every new line from our root directory. To get our cd to stick and avoid running our Git commands in the project root directory, we’re chaining the commands and using the backslash character to break long lines for readability.

deploy部分,您会注意到以&&开头的行。 这些是链接的命令。 由于Make 为每一行调用一个新的子外壳程序 ,因此它将从根目录中的每一行开始。 为了使cd保持粘贴状态并避免在项目根目录中运行Git命令,我们将命令链接起来,并使用反斜杠字符将长行打断以提高可读性。

By chaining our commands, we’re able to configure our Git identity, add all our updated files, and create a commit for our Pages repository.

通过链接命令,我们可以配置Git身份 ,添加所有更新的文件并为Pages存储库创建提交。

Similarly to using Travis CI, we’ll need to pass in a GitHub personal access token to push to our public GitHub Pages repository — only Netlify doesn’t provide a straightforward way to encrypt the token in our Makefile.

与使用Travis CI相似,我们需要传递GitHub个人访问令牌以推送到我们的公共GitHub Pages存储库-只有Netlify不能提供在Makefile中加密令牌的直接方法。

Instead, we’ll use Netlify’s Build Environment Variables, which live safely in our site settings in the Netlify app. We can then call our token variable in the Makefile. We use it to push (quietly, to avoid printing the token in logs) to our Pages repository by passing it in the remote URL.

取而代之的是,我们将使用Netlify的Build Environment Variables ( 构建环境变量) ,该变量安全地存在于Netlify应用程序的站点设置中。 然后,我们可以在Makefile中调用令牌变量。 我们通过将其传递到远程URL来将其推入(很安静,以避免在日志中打印令牌)到Pages存储库。

To avoid printing the token in Netlify’s logs, we suppress recipe echoing for that line with the leading @ character.

为了避免在Netlify的日志中打印令牌,我们禁止在行中使用前导@字符来回显配方

With your Makefile in the root of your private GitHub repository, you can set up Netlify to run it for you.

将Makefile放在私有GitHub存储库的根目录中,您可以设置Netlify来为您运行它。

设置Netlify (Setting up Netlify)

Getting set up with Netlify via the web UI is straightforward. Once you sign in with GitHub, choose the private GitHub repository where your Hugo site lives. The next page Netlify takes you to lets you enter deploy settings:

通过Web UI设置Netlify很简单。 使用GitHub登录后,选择Hugo网站所在的私有GitHub存储库。 Netlify的下一页让您输入部署设置:

You can specify the build command that will run your Makefile (make all for this example). The branch to deploy and the publish directory don’t matter too much in our specific case, since we’re only concerned with pushing to a separate repository. You can enter the typical master deploy branch and public publish directory.

您可以指定将运行您的Makefile的build命令(在此示例中make all )。 在我们的特定情况下,要部署的分支和publish目录没有太大关系,因为我们只关心推送到单独的存储库。 您可以输入典型的master部署分支和public发布目录。

Under “Advanced build settings” click “New variable” to add your GitHub personal access token as a Build Environment Variable. In our example, the variable name is GITHUB_TOKEN. Click “Deploy site” to make the magic happen.

在“高级构建设置”下,单击“新变量”以将您的GitHub个人访问令牌添加为构建环境变量。 在我们的示例中,变量名称为GITHUB_TOKEN 。 单击“部署站点”使魔术发生。

If you’ve already previously set up your repository with Netlify, find the settings for Continuous Deployment under Settings > Build & deploy.

如果您以前已经使用Netlify设置了存储库,请在“设置”>“构建和部署”下找到“连续部署”的设置。

Netlify will build your site each time you push to the private repository. If you don’t want a particular commit to trigger a build, add [skip ci] in your Git commit message.

每当您推送到私有存储库时,Netlify都会构建您的站点。 如果您不希望特定的提交触发构建,请在您的Git提交消息中添加[skip ci]

相识又有差别 (Same same but different)

One effect of using Netlify this way is that your site will be built in two places: one is the separate, public GitHub Pages repository that the Makefile pushes to, and the other is your Netlify site that deploys on their CDN from your linked private GitHub repository. The latter is useful if you’re going to play with Deploy Previews and other Netlify features, but those are outside the scope of this post.

使用Netlify的一种效果是,您的站点将在两个地方构建:一个是Makefile推送到的单独的公共GitHub Pages存储库,另一个是您的Netlify站点,它从链接的私有GitHub部署在其CDN上资料库。 如果您要使用Deploy Previews和其他Netlify功能,但是后者不在本文讨论范围之内,则后者很有用。

The main point is that your GitHub Pages site is now updated in your public repo. Yay!

要点是,您的GitHub Pages网站现已在您的公共仓库中更新。 好极了!

勇往直前部署 (Go forth and deploy fearlessly)

I hope the effect of this new information is that you feel more able to update your sites, wherever you happen to be. The possibilities are endless — at home on your couch with your laptop, out cafe-hopping with your iPad, or in the middle of a first date on your phone. Endless!

我希望这些新信息的作用是,无论您身在何处,您都可以更轻松地更新自己的网站。 可能性无穷无尽–在家中坐在笔记本电脑上的沙发上,在iPad上逛咖啡馆,或在手机初次约会期间。 没完没了!

翻译自: https://www.freecodecamp.org/news/two-ways-to-deploy-a-public-github-pages-site-from-a-private-hugo-repository-627312ec63b9/

hugo 部署github

 类似资料: