menu Chancel's blog
rss_feed
Chancel's blog
我就是这样的人

Bash脚本自动更新Git仓库

作者:Chancel, 时间:2022 May 26, 阅读:40

线上的一些小项目偶尔需要实现自动更新,那么可以考虑结合Git仓库的Master来做自动更新,小的项目没必要考虑去写一个Jenkins配置单

首先要配置项目无需验证账户密码就可以拉取文件,执行完下面命令,输入账户密码后下次拉取代码不会重新询问账户密码

git config --global credential.helper store
git pull

设置一下自动拉取Master分支,然后结合Supervisor重启一次应用程序即可啦

#!/bin/bash
# author: Chancel.Yang

projectPath='/home/Apps/hello-world'
projectName='hello-world'
cd $projectPath
git fetch
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @{0})
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @{0} "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
    echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
    supervisorctl stop $projectName
    # 脚本由Root执行,但拉取不可用Root用户否则项目执行用户Apps无权限执行项目
    su - Apps git pull
    supervisorctl start $projectName

执行这个脚本就可以获取到Github远程仓库与本地仓库的版本差别

注意git的旧版本不允许使用@,所以你可能需要使用@ {0}

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]