文章

git_push_ObsidianNote_success

git_push_ObisidianNote_success.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash

## 配置变量
LOCAL_REPO=$(cd "$(dirname "$0")"; pwd)
LOCAL_REPO=$( cd "${LOCAL_REPO}";cd ../../; pwd )
REMOTE_REPO=$( cd ${LOCAL_REPO}; /usr/bin/git remote get-url origin )
REMOTE_REPO_HTTPS=$( echo "${REMOTE_REPO}" | awk -F":" '{print "https://gitlab.fscloude.cn/"$2}' )
BRANCH=master
SERVICE_NAME="$( echo "${REMOTE_REPO}" | awk -F"[/.]" '{print $(NF-1)}' )"

## 钉钉机器人信息
DINGTALK_ACCESS_TOKEN=bcdf2e90931150c5ff3492d78af2f69cdda649a757e54a46096ba4121d6a974c
DINGTALK_SECRET=SEC11a84470340ed508ed2413835938cbdcf50410ed4149b658ff80383aef5e00c0
# @饶宸玮一三班-28号——饶宸嘉小三班5号 
#DINGTALK_ACCESS_TOKEN=638c357ef2545675f288eae602c0242a3499a971bb80e2110f805579c9367fa8
#DINGTALK_SECRET=SEC7bbc1324c1c2125d13994639143239298b0de21e0c2b73c0b0fab82319e0b941
## 验签
read timestamp sign < <( python3 ${LOCAL_REPO}/Script/git_rsync/get_signs.py ${DINGTALK_SECRET} )

## 获取当前节点系统信息:
HostName=$( /bin/hostname )
InternetIP=$( curl -s cip.cc | grep IP | awk '{print $NF}' )
NowTime="$( date "+%Y-%m-%d %H:%M:%S" )"


## 消息体颜色变量:
Pale_Green="#00CC00"  # 浅绿色
Pale_Blue="#0000dd"   # 浅蓝色
Pale_Red="#dd0000"    # 浅红色
Pale_Orange="#FF3333" # 红橙色
Orange_Red="#FF9966"  # 正橘红色
Red_Brown="#990000"   # 红棕色

# 默认颜色
Topic_Color="${Pale_Green}" # 浅绿色
GigRo_Color="${Red_Brown}"  # 浅蓝色



## 进入本地仓库目录
cd $LOCAL_REPO

## 检查远端仓库是否存在
if ! git ls-remote --quiet --exit-code $REMOTE_REPO $BRANCH >> /dev/null; then
  exit 0
fi

## 提交变更
push_info=$( git pull --quiet 2>&1 && git add --all 2>&1 && git commit -m "Auto commit changes" 2>&1 && git push --quiet $REMOTE_REPO $BRANCH 2>&1 )

if [[ $? -eq 0 ]]; then
  push_info="Changes pushed successfully to remote repository."
  health_status="Success"
elif [[ -n "$( echo "${push_info}" | grep "nothing to commit" )" ]]; then
  push_info="nothing to commit, working tree clean."
  health_status="Success"
else
    exit 1
fi


## 消息体变量:
# 颜色变动
if [[ ${health_status} == "Success" ]]; then
    Topic_Mk_Cnt='<font color='"${Topic_Color}"'>【Ob_NoteGit推送】凌晨每日一检</font>'
fi

GitRepo_Mk_Cnt=' **仓库名称:** <font color='"${GigRo_Color}"'>['"${SERVICE_NAME}"']('"${REMOTE_REPO_HTTPS}"')</font>'
HostName_Mk_Cnt='**推送主机:** '"${HostName}"''
HostIPid_Mk_Cnt='**主机地址:** '"${InternetIP}"''
NowTime_Mk_Cnt=' **推送时间:** '"${NowTime}"''
Health_Mk_Cnt='  **健康状态:** **<font color='${Topic_Color}'>'${health_status}'</font>**'
StaInfo_Mk_Cnt=' **状态信息:** \n <font color='${Topic_Color}'>'${push_info}'</font>'

## 消息体
message='{
    "msgtype": "markdown",
    "markdown": {
        "title": "Ob_NoteGit告警详情",
        "text": "## '${Topic_Mk_Cnt}'\n> - '${GitRepo_Mk_Cnt}'\n> - '${HostName_Mk_Cnt}'\n> - '${HostIPid_Mk_Cnt}'\n> - '${NowTime_Mk_Cnt}'\n --- \n> - '${Health_Mk_Cnt}'\n\n> - '${StaInfo_Mk_Cnt}'"
    }
}'

## 发送钉钉机器人消息
curl 'https://oapi.dingtalk.com/robot/send?access_token='"${DINGTALK_ACCESS_TOKEN}"'&timestamp='${timestamp}'&sign='${sign}'' \
     -H 'Content-Type: application/json' \
     -d "${message}"

本文由作者按照 CC BY 4.0 进行授权