使用shell脚本来实现多平台一键提交和发布
·
大约 400 个字
·
预计 2
分钟 读完
最近将我的gitlab上面的博客倒腾了一番,结果发现访问速度堪忧啊!但是服务器又买不起,就又想到了码云,毕竟码云在国内访问速度比gitlab块不少,但是我又舍不得直接放弃gitlab,所以就想到了这个办法,gitlab和码云同时发布我的hexo博客文章。
shell基本语法
定义只读变量
使用readonly关键字来定义只读变量
1
2
| gitlab_path="/home/shuihan/Documents/lxqxsyu.gitlab.io"
readonly gitlab_path
|
注意定义变量的时候等号两边不要留空白,字符串可以用双引号也可以用单引号。
定义函数
1
2
3
4
5
6
7
| commitToGitlab(){
echo "开始提交代码到Gitlab"
cd $gitlab_path
git add .
git commit -m "$1"
echo "提交代码到Gitlab成功:$1"
}
|
函数的传参是通过$1,$2,…来实现的
1
2
| commitToGitlab "submit"
#输出为 提交代码到Gitlab成功:submit
|
注意shell的执行是顺序执行的,所以函数的定义要放在调用前。
逻辑判断
1
2
3
4
5
6
7
8
9
10
11
12
13
| echo -e "输入c只提交不发布\n输入p则发布到远程服务器\n输入cp提交并且发布"
read -t 5 ml
echo -e "\n"
if [ "$ml" == "p" ];then
publish
elif [ "$ml" == "c" ];then
commit
elif [ "$ml" == "cp" ];then
commit
publish
else
echo "请输入正确的命令,执行失败"
fi
|
注意:[ ]内左右两边要留空白, read是等待输入的命令, -t参数代表等待时间。
完整代码
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
| #!/bin/bash
gitlab_path="/home/shuihan/Documents/lxqxsyu.gitlab.io"
readonly gitlab_path
gitee_path="/media/shuihan/temp/lxqxsyu"
readonly gitee_path
commitToGitlab(){
echo "开始提交代码到Gitlab"
cd $gitlab_path
git add .
git commit -m "$1"
echo "提交代码到Gitlab成功:$1"
}
commitToGitee(){
echo "开始提交代码到码云"
cd $gitlab_path
echo "生成静态文件"
hexo g
echo "复制public目录到gitee目录下"
cp -a -f $gitlab_path/public/* $gitee_path
cd $gitee_path
git add .
git commit -m "$1"
echo "提交代码到码云成功:$1"
}
commit(){
read -p "输入commit的提交信息,忽略直接回车:" -r cmsg
commitmsg="提交"
if [ -n "$cmsg" ]
then
commitmsg=$cmsg
fi
echo "开始提交代码"
commitToGitlab $commitmsg
commitToGitee $commitmsg
}
publishToGitlab(){
echo "开始发布到Gitlab"
cd $gitlab_path
git push
echo "发布到Gitlab成功"
}
publishToGitee(){
echo "开始发布到码云"
cd $gitee_path
git push
echo "发布到码云成功"
}
publish(){
echo "开始发布到远程服务器"
publishToGitlab
publishToGitee
}
echo -e "输入c只提交不发布\n输入p则发布到远程服务器\n输入cp提交并且发布"
read -t 5 ml
echo -e "\n"
if [ "$ml" == "p" ];then
publish
elif [ "$ml" == "c" ];then
commit
elif [ "$ml" == "cp" ];then
commit
publish
else
echo "请输入正确的命令,执行失败"
fi
notify-send -u critical "发布文章任务执行完成"
|
如何运行
运行
新建文件test.sh然后将代码黏贴进去。
给test.sh添加可执行权限
执行脚本, 在test.sh所在目录打开命令行。
效果