menu E4b9a6's blog
rss_feed
E4b9a6's blog
有善始者实繁,能克终者盖寡。

Bash脚本自动更新Git仓库

作者:E4b9a6, 创建:2021-06-15, 字数:842, 已阅:70, 最后更新:2024-03-10

这篇文章更新于 226 天前,文中部分信息可能失效,请自行甄别无效内容。

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

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

Bash
git config --global credential.helper store
git pull

设置一下自动拉取Master分支,并结合Supervisor重启一次应用程序

脚本如下

Bash
#!/bin/bash

# Author: chancel.yang
# Date: 2023-12-15
# Description: Automatically update Git and restart the project


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

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


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

account_circle
email
web_asset
textsms

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

还没有可以显示的留言...
gravatar
[[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)]]