大致流程

autodeploy001

从gitee私有仓库拉取代码,通过本地jenkins将代码push到指定的git服务器(通过ssh),最后rsync同步到指定的网站目录当中。

脚本相关(jenkins)

git hooks

内容很简单,在此不再赘述,初始化空的服务端git仓库,参考我之前写的一篇博文:git-hook介绍与使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[dmy@www2020 ~]$ cat ~/web.com.git/hooks/post-receive 
#!/bin/bash
GIT_REPO=/home/dmy/web.com.git
TMP_GIT_CLONE=/tmp/web.com.git
PUBLIC_WWW=/var/www/html/test
rm -rf ${TMP_GIT_CLONE}
git clone -b main ${GIT_REPO} ${TMP_GIT_CLONE}
sleep 1
cd ${TMP_GIT_CLONE}
# rm -rf ${PUBLIC_WWW}/*
# cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
rsync -av --progress ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

shell脚本

  1. gitee私有仓库拉取最新main分支代码到jenkins服务器本地;
  2. 检测jenkins本地仓库是否存在,如果存在,则直接pull,如果不存在,则clone一份;
  3. 添加官网git服务器的地址(通过ssh进行连接,这里可以新建一个无login shell的专门用户),远程git仓库名称deploy,分支同样为main
  4. 使用expect免交互自动输入ssh密码;
  5. 另,如果jenkins本地仓库有变更,添加本地文件,提交并push到官网git服务器上;
 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
#!/bin/bash
#
#**************************************************
# Author:         AGou-ops                        *
# E-mail:         agou-ops@foxmail.com            *
# Date:           2022-04-02                      *
# Description:                              *
# Copyright 2022 by AGou-ops.All Rights Reserved  *
#**************************************************

# jenkins 服务器
localDir=~/Documents/web-workspace/web_web/www2022

rmAndInitRemote() {
    cd ${localDir} 
    # 官网远程git仓库地址,通过ssh连接
    git remote add deploy ssh://<YOUR_SSH_USERNAME>@<YOUR_GIT_SERVER_NAME_OR_IP_HERE>:/home/dmy/web.com.git
}

# ([ -d ${localDir} ] || echo "目录不存在") && mkdir -pv ${localDir}

ls  ${localDir}/.git/config > /dev/null 2>&1
if [ $? -eq 0 ]  
then  
    cd ${localDir}
    # MacOS
    sed -i '' -e '14,$d'   ${localDir}/.git/config
    # Linux 
    # sed -i '14,$d'   ${localDir}/.git/config
    git pull origin main
    rmAndInitRemote
else
    git clone -b main <YOUR_REMOTE_PRIVATE_GIT_REPO_HERE> ${localDir}
    rmAndInitRemote
fi  

sleep 1
cd ${localDir}
# --- 临时
git add -A
git commit -m "rebuilding site $(date)" || true
# ---

# 推送到远程
PASS=<YOUR_SSH_PASSWORD_HERE>

expect << EOF
set timeout 5
spawn git push -f -u deploy main
expect {
    "(yes/no)" {send "yes\r"; exp_continue}
    "password:" {send "$PASS\r"}
}
expect eof
EOF

jenkins中构建运行结果

image-20220406103914712

Done.