标签 git 下的文章

原理

当版本库代码更新时,通过 git 的 webhook(git web 钩子)触发 push 事件。用户提交代码(git push)服务器的宝塔 webhook 插件拉取当前 git 最新代码(git pull)。

步骤

1、CentOS 服务器安装宝塔面板:宝塔面板
linux_pc_free.png

2、安装 git:

yum install git

Tips: Git 生成 SSH 公钥

3、宝塔面板软件商店安装 宝塔WebHook;
20191110195119.png

4、添加 HOOK 命令:

#!/bin/bash
echo ""
#输出当前时间
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
#判断宝塔WebHook参数是否存在
if [ ! -n "$1" ];
then
          echo "param参数错误"
          echo "End"
          exit
fi
#git项目路径
gitPath="/www/wwwroot/web/$1"
#git 网址
gitHttp="http://git.xxxxx.com/web/$1.git"

echo "Web站点路径:$gitPath"

#判断项目路径是否存在
if [ -d "$gitPath" ]; then
        cd $gitPath
        #判断是否存在git目录
        if [ ! -d ".git" ]; then
                echo "在该目录下克隆 git"
                git clone $gitHttp gittemp
                mv gittemp/.git .
                rm -rf gittemp
        fi
        #拉取最新的项目文件
        git reset --hard origin/master
        git pull
        #设置目录权限
        chown -R www:www $gitPath
        echo "End"
        exit
else
        echo "该项目路径不存在"
        echo "End"
        exit
fi

- 阅读剩余部分 -

071305064859837.png

SSH 存储位置

默认情况下,用户的 SSH 密钥存储在其 ~/.ssh 目录下。(CentOS 存储在 /root/.ssh;Windows 存储在 C:\Users\Administrator\.ssh)

查看目录下是否存在 SSH 公钥:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub

id_dsa 或 id_rsa 文件应该成对出现,其中一个带有 .pub 扩展名。.pub 文件是你的公钥,另一个则是私钥。

运行 ssh-keygen 生成

ssh-keygen -t rsa -C "your@example.com"

your@example.com 是你的邮箱。

测试连通性

ssh -T git@github.com

配置 name 和 email

git config --global user.name "yourname"
git config --global user.email "your@example.com"