一,概述
万能shell版,对git项目无任何要求。
二,环境准备
云主机,centos7系统,安装组件:git
组件 | 作用 |
---|---|
git | 源码下载 |
crontab | 定时作业列表,linux内置,一般无需安装 |
安装git
#安装git
yum install -y git
三,项目代码
任意项目代码,通过git下载代码到任意目录
1. 示例项目
https://gitcode.com/00fly/demo/tree/main
示例目录: /work/abc/gitcode/abc
mkdir -p /work/abc/gitcode/abc
cd /work/abc/gitcode/abc
#下载git项目源码
git clone https://gitcode.com/00fly/demo.git
2. 配置项目用户名、密码(个人令牌)
下面的代码需要进入git项目目录里面执行
#当前git项目代码目录保存
cd /work/abc/gitcode/abc/demo
git config user.name 00fly
git config user.email 00fly@noreply.gitcode.com
git config credential.helper store
可参考 程序猿Git项目代码自动提交神器-shell版 相关章节。
保证设置后git提交代码无需输入用户名、密码
四,运行
重要:一定要在git项目当前目录操作
#wget网络下载start.sh保存到git项目当前目录
cd /work/abc/gitcode/abc
wget https://gitee.com/00fly/git-cron/raw/master/all-in-one-cron-push.sh -O start.sh
#测试
sh start.sh
start.sh
#!/bin/sh
# defines variable
git=`ls -d */` && path=`pwd` && gitpath=$path/$git
# git pull
cd $gitpath && git pull \
&& now=`git log -1 --format="%at"` \
&& last=`cat $path/last`
# check timestamp, if changed print git log
if [ ! $last ] || [ $now -gt $last ] ; then
echo "git changed!!" && echo $now>$path/last && echo `git log -1`
else
echo "git not change"
fi
# git push
date > date.md
if [ -n "$(git status -s)" ] ; then
git add .
git commit -m "update: `date +"%Y-%m-%d %H:%M:%S"` auto commit"
git push
fi
五,配置定时任务
1. 配置定时任务
#查看作业任务
crontab -l
#编辑作业任务
crontab -e
添加内容
0 */1 * * * cd /work/abc/gitcode/abc&&sh start.sh
每1小时执行一次
或者
*/5 * * * * cd /work/abc/gitcode/abc&&sh start.sh
每5分钟执行一次
0 */4 * * * cd /work/abc/gitcode/abc&&sh start.sh
每4小时执行一次
有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
-over-