GitHub Actions 同步代码到多台服务器上(git clone 方式)

GitHub Actions 同步代码到多台服务器上(git clone 方式)

.github/workflows/deploy.yml

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
name: Deploy to Server

on:
push:
branches:
- main

jobs:
base:
runs-on: ubuntu-22.04
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
strategy:
max-parallel: 2 # 同时部署的数量
matrix:
ip: [47.106.245.55, 47.106.246.19, 47.106.248.33]
steps:
- uses: actions/checkout@v3
- uses: cross-the-world/ssh-pipeline@master
env:
SCRIPT: |
#!/bin/sh
# 变量
GITHUB_OWNER="hongfs"
GITHUB_REPO="仓库名称"
GITHUB_TOKEN="github_pat_11AFSLBeYl9QX8Mjn1LpIeWXXLFJwCCH5I466QbDI1JVd6"
BASEPATH="/home/wwwroot/"
BASENAME="web-api"

if ! command -v git &> /dev/null; then
curl -s https://i.hongfs.cn/i_git | sh
fi

if [ ! -d "$BASEPATH" ]; then
mkdir -p $BASEPATH
fi

cd $BASEPATH

if [ ! -d "$BASEPATH$BASENAME" ]; then
# 如果文件夹不存在,那就拉取代码
git clone https://$GITHUB_OWNER:$GITHUB_TOKEN@github.com/$GITHUB_OWNER/$GITHUB_REPO.git $BASENAME
fi

# 进入项目目录
cd $BASENAME

# 回滚+拉取
git reset --hard HEAD
git pull
with:
host: ${{ matrix.ip }}
user: root
pass: ${{ secrets.PASSWORD }}
port: 22
connect_timeout: 3000s
script: |
echo '$SCRIPT' | sh
往上