天天看点

linux定时对git拉取和推送

准备工作

查看当前拉取和推送地址

git remote -v
           
linux定时对git拉取和推送

更改当前地址为ssh地址,通过将ssh公钥放到git实现免密登陆

git remote set-url ssh地址
           
linux定时对git拉取和推送

pull时告警去除

linux定时对git拉取和推送
# 3种pull策略根据自己的需求选择
git config pull.rebase false  # 合并(缺省策略)
git config pull.rebase true   # 变基
git config pull.ff only       # 仅快进
           
pull
#!/bin/bash

dir=`ls /mygit`
echo "git dir list:${dir}---"
for d in $dir
do
    if [ -d $d ]
    then
        echo "d:$d"
        cd /mygit/$d
        git pull >/mygit/$d/pull_logs.txt 2>&1
    fi
done
           
push
#!/bin/bash

localtime=`date "+%Y-%m-%d_%H:%M:%S"`
dir=`ls /mygit`
echo "git dir list:${dir}---"
for d in $dir
do
    if [ -d $d ]
    then
        echo "d:$d"
        cd /mygit/$d
        echo "-------------localtime:$localtime------------" >> /mygit/${d}/push_logs.txt
        git push >> /mygit/${d}/push_logs.txt 2>&1
        git add .
        git commit -am "cron task auto push" >> /mygit/${d}/push_logs.txt 2>&1
        git push >> /mygit/${d}/push_logs.txt 2>&1
    fi
done
           

合并

#!/usr/bin/env bash
set -x
# backup ansible hosts to git
gitDir=/home/Jenkins_install

# git pull
cd $gitDir
date "+%Y-%m-%d_%H:%M:%S" >> logs/pull.log
git pull >> logs/pull.log 2>&1

# git push
git add .
date "+%Y-%m-%d_%H:%M:%S" >> logs/push.log
git commit -am "cron task auto push" >> logs/push.log 2>&1
git push >> logs/push.log

           
定时执行脚本

因为是在docker容器里面执行,注意时差

crontab -e
0 16 * * 1-5 sh /home/Jenkins_install/ansibleBak.sh