利用Git进行自动化多分支部署

创建Git用户用于自动部署

新建用户

useradd git

设置密码

passwd git

配置无法shell登录

1
vi /etc/passwd
2
git:x:1000:1000::/home/git:/usr/bin/git-shell

云端自动部署配置

新建裸仓库

1
git init --bare /opt/webapps/project-bare.git
2
cd /opt/webapps/project-bare.git/hooks
3
cp post-update.sample post-update
4
vi post-update

多分支部署

1
#!/bin/sh
2
3
unset GIT_DIR
4
mkdir -p /opt/webapps/project
5
cd /opt/webapps/project || exit
6
7
# 获取当前分支和目标分支
8
CURR_BRANCH=$(git symbolic-ref --short -q HEAD)
9
echo "current branch: ${CURR_BRANCH}"
10
TARGET_BRANCH=$(echo "$1" | awk -F/ '{print $3}')
11
echo "target branch: ${TARGET_BRANCH}"
12
13
# 初始化仓库
14
git init
15
git remote add origin /opt/webapps/project-bare.git
16
# 清除未跟踪或未暂存的代码
17
git reset --hard
18
git clean -df
19
20
# 拉取远端所有分支最新代码
21
git fetch origin
22
# 如果是删除分支操作,则切到master分支并删除该分支,要求不能删除远端master分支
23
if [ ! -f "/opt/webapps/project-bare.git/refs/heads/${TARGET_BRANCH}" ]
24
then
25
    echo "target branch ${TARGET_BRANCH} has been deleted"
26
    # 如果是当前分支,需要切换到master分支
27
    if [ "${TARGET_BRANCH}" = "${CURR_BRANCH}" ]
28
    then
29
        git checkout master
30
    fi
31
    # 删除本地同名分支
32
    git branch -D "${TARGET_BRANCH}"
33
else
34
    if [ "${TARGET_BRANCH}" = "${CURR_BRANCH}" ]
35
    then
36
        echo "same branch! rebase origin/${TARGET_BRANCH}."
37
        git rebase origin/"${TARGET_BRANCH}"
38
    else
39
        echo "different branch! checkout to ${TARGET_BRANCH}."
40
        git branch -D "${TARGET_BRANCH}"
41
        git checkout -b "${TARGET_BRANCH}" origin/"${TARGET_BRANCH}"
42
    fi
43
fi
44
45
echo "pull done"
46
47
# 业务服务启动逻辑
48
npm install
49
50
rm -rf dist
51
npm run build
52
53
pm2 restart project
54
echo "deployment done"

设置权限

1
chown -R git.git /opt/webapps/project-bare.git

新建代码仓库目录

1
mkdir /opt/webapps/project
2
chown -R git.git /opt/webapps/project

本地设置自动部署

1
git remote add deployment git@www.huhaoyu.com:/opt/webapps/project-bare.git
2
git push deployment master

Troubleshooting GitHub Pages build failures——解决GitHub Pages部署失败问题

If your GitHub Pages site fails to build on our servers, we’ll send you an email letting you know about the failure. In most cases, we’ll be able to identify the exact file and error so you can fix it and push again.

如果你的GitHub Pages网站无法在服务器上部署,我们讲发送一封邮件给您,让您了解失败的原因。大多数情况下,我们能够找到发生错误的文件以及错误的类型,你可以修复问题并重新推送到GitHub。

Generic failures–一般故障

These build failures will not produce an email with specific file and error information. If you receive an email that simply says “Page build failed” with no further details, or your GitHub Pages site is not showing up after the first push, check for the following common errors:

这类错误不会在email中显示确切的文件及错误信息。如果你收到的邮件简单地显示“Page build failed”,且没有更多额外信息,或你的GitHub Pages网站在您推送到GitHub后并没有显示,请检查如下几个常见的错误:

Unverified email address–未认证的email地址

To trigger a build, the user who pushes a commit to the Pages repository must have a verified email address.

After you verify an email address, you’ll need to push to the repository again to trigger the build process on our servers. You can also contact GitHub support to manually run a build.

为部署您的个人网页,用户推送的commit必须有一个经认证的email地址。

当email经过认证后,你需要再次向你的远程仓库推送commit以保证服务器为您生成网页。你也可以联系GitHub支持来人工地生成网页。

Deploy key used for push–推送所需的部署公钥

Pushes must come from a user account with a verified email address. You won’t trigger a build if you use a deploy key to push to an organization’s Pages repository.

Instead, you can set up a machine user as a member of your organization.

推送必须来自一个经email地址认证的帐户。如果您使用一个部署公钥去推送commit到一个组织的网页仓库,这将不能触发部署动作。

相反的,你能够设置一个machine用户作为你组织的一个成员。

git合并多个commit的方法:git rebase -i

前言

虽然看到博主都写了有关git rebase -i合并commit的博文,但为巩固知识也在自己的博客中稍微做一些解释。
过去总是使用git reset –soft回退到之前的状态,再commit后push -f强推到远程库,能够覆盖掉之前的commit。

但在团队协作时,每次commit前还需要rebase upstream,这会自动将一些其他人做的修改也自动merge到本地的源码中。如果此时希望覆盖前一次commit,reset到之前的状态后,再次commit的内容就包含了其他人的修改,这不是我们希望看到的。
因此,我们需要使用git rebase -i。

基本步骤

  • 1 git log查看所有commit的情况,找到自己想要合并的commit之前的那个commit的ssh码,比如43jk2l3ba343;
  • 2 git rebase -i 43jk2l3ba343,这样会弹出一个文本编辑器;
  • 3 修改pick为squash会将这个commit合并到前一个commit中,保存退出;
  • 4 提示写下新的commit message,之前的message可以用#注释掉,保存退出;
  • 5 此时再git log就会发现,两个commit被合并到一个commit中。

补充

  • 如果两个或多个commit不是相邻关系,可以先git rebase -i一次,这次只调整commit的顺序而不做修改,时所有要修改的commit相邻,同时必须保持原有的先后顺序。第二次rebase与上面基本步骤相同。
  • 当然可以git commit –fixup结合git rebase -i –autosquash,食用风味更佳:)

Using Jekyll with Pages——使用Jekyll框架

In addition to supporting regular HTML content, GitHub Pages support Jekyll, a simple, blog-aware static site generator. Jekyll makes it easy to create site-wide headers and footers without having to copy them across every page. It also offers some other advanced templating features.

为了支持HTML内容,GitHub Pages支持了Jekyll框架,Jekyll是一个简单的博客类静态网站生成器。Jekyll能够简单地在各个页面中使用全局的headers和footers。同时Jekyll也能提供一些其他的模版特性。

Using Jekyll——使用Jekyll

Every GitHub Page is run through Jekyll when you push content to a specially named branch within your repository. For User Pages, use the master branch in your username.github.io repository. For Project Pages, use the gh-pages branch in your project’s repository. Because a normal HTML site is also a valid Jekyll site, you don’t have to do anything special to keep your standard HTML files unchanged. Jekyll has thorough documentation that covers its features and usage. Simply start committing Jekyll formatted files and you’ll be using Jekyll in no time.

当你向一个特殊命名的远程仓库推送内容后,GitHub Page就能够通过Jekyll来运行。对于用户页面,可以在名为username.github.io的仓库中设置master分支。对于项目页面,在项目仓库中设置gh-pages分支。因为一个普通的HTML网页也是一个Jekyll框架网页,所以你无需对你的标准HTML文件做任何改动。Jekyll有专门描述其特性和使用方法的文档。你可以轻轻松松使用Jekyll框架来实现你的网站。

Installing Jekyll——安装Jekyll

We highly recommend installing Jekyll on your computer to preview your site and help diagnose troubled builds before publishing your site on GitHub Pages.

Luckily, installing Jekyll on your computer, and ensuring your computer most closely matches the GitHub Pages settings is easy, thanks to the GitHub Pages Gem and our dependency versions page. To install Jekyll, you’ll need a few things:

我们强烈推荐你在你的电脑上安装Jekyll来预览你的网站,同时这也能帮助你在GitHub Pages上发布网站之前,诊断因为部署失败而引发的错误。非常幸运地,在电脑上安装Jekyll和相关设置非常的简单易懂。安装Jekyll的步骤如下:

git整理commit的基本方法

觉得整理commit还是非常重要的一种技能,看到有人已经很好地整理过了,也搬运到自己的博客中,以备不时之需。本文转载自:Git整理Patch的一些经验

U-Boot升级到了最后,需要将之前比较杂乱的commit重新整理,有的需要整合,有的需要拆分。在这个过程中深刻的体会到了git的强大和灵活性。这里总结一下大的步骤和中间用到的各种小技巧。

步骤

commands usage
git checkout –b 新建一个branch来做整理的工作,保持原来的branch作为工作记录
git rebase –i 在新建的branch上,用rebase -i接squash的方法,将所有零碎的commit合成一个
git reset –soft HEAD~1 这一步只将object store还原到working_base上。所有需要整理的改动都留在index上,以备下一步做stash
git stash save “stash message” 将所有的改动放进stash中

至此,我们有了一个工作的基础,所有杂乱的commit被合成一个,并被放进stash中,以备后面一点点的commit进去。

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×