Skip to content

Commit 896dcfc

Browse files
committed
feat: Add rsync filtering and optimize SFTP deployment process
- Introduced rsync support with customizable `rsyncArgs` for file filtering. - Ensured only filtered content is uploaded, avoiding `filtered_upload` folder inclusion. - Updated documentation to reflect the latest changes and usage examples.
1 parent 89e97d7 commit 896dcfc

5 files changed

Lines changed: 169 additions & 84 deletions

File tree

.github/workflows/main.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ jobs:
1212
- name: deploy file to server
1313
uses: ./ # Uses an action in the root directory
1414
with:
15-
username: 'wl'
16-
server: '${{ secrets.MAC_IP }}'
15+
username: '${{ secrets.USER }}'
16+
server: '${{ secrets.SERVER_IP }}'
1717
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
1818
local_path: './*'
19-
remote_path: '/Users/wl/Downloads/t/a'
19+
remote_path: '/tmp/testAction'
2020
sftpArgs: '-o ConnectTimeout=5'
21+
rsyncArgs: '--exclude *.yml'
22+
23+
2124
# sftp_only: true
22-
password: ${{secrets.SSH_PASSWORD}}
25+
# password: ${{secrets.SSH_PASSWORD}}
2326
# delete_remote_files: true

Dockerfile

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
# Container image that runs your code
2-
FROM alpine:3.13
1+
# Use an up-to-date and secure Alpine version
2+
FROM alpine:3.18
33

4-
# Copies your code file from your action repository to the filesystem path `/` of the container
5-
COPY entrypoint.sh /entrypoint.sh
6-
7-
#Make sure to make you entrypoint.sh file executable:
8-
RUN chmod 777 entrypoint.sh
9-
10-
RUN apk update
11-
RUN apk add --no-cache openssh
4+
# Install required packages in one RUN statement to reduce image layers
5+
RUN apk update && apk add --no-cache rsync sshpass openssh
126

7+
# Copy entrypoint script and set correct permissions
8+
COPY entrypoint.sh /entrypoint.sh
9+
RUN chmod +x /entrypoint.sh
1310

14-
# Code file to execute when the docker container starts up (`entrypoint.sh`)
15-
ENTRYPOINT ["/entrypoint.sh"]
11+
# Set the entrypoint for the container
12+
ENTRYPOINT ["/bin/sh", "/entrypoint.sh"]

README.md

Lines changed: 116 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,164 @@
1-
# SFTP Deploy action
21

3-
> Use this action to deploy your files to server using `SSH Private Key`
2+
# SFTP Deploy Action
43

5-
> 使用此`action`部署你的项目到服务器上,中文介绍链接:[使用Github Action 部署项目到云服务器](https://zhuanlan.zhihu.com/p/107545396)
4+
> Use this action to deploy your files to a server using `SSH Private Key`
65
7-
## 1. Inputs
6+
> 使用此 `action` 部署你的项目到服务器上,中文介绍链接:[使用Github Action 部署项目到云服务器](https://zhuanlan.zhihu.com/p/107545396)
87
9-
| Name | Required | Default | Description |
10-
|------------------------|----------------------|---------|-----------------------------------------------|
11-
`username` | yes| | SSH username
12-
`server` | yes| | Remote host
13-
`port`| yes | 22 | Remote host port
14-
`ssh_private_key`| no| | You can copy private key from your `ssh_private_key` file, and save to`repo/settings/secrets`![](./resource/secret.jpg)
15-
`local_path`| yes| ./* | `local_path` of you project, if you want put single file:use path like `./myfile`, if you want put directory: use path like `./static/*`, it will put all files under `static` directory. Default to `./*`(will put all files in your repo).
16-
`remote_path`|yes|| Remote path
17-
`sftp_only`| no| | If your port only accepts the sftp protocol, set this option to `true`. However, please note that when this option is set to `true`, the remote folder will not be created automatically.
18-
<strike>args</strike> `sftpArgs` | no| | other args yor want to use of sftp, E.g.`-o ConnectTimeout=5`
19-
`delete_remote_files` | no | false | Set to `true` will delete remote path folder and all files in it.
20-
`password`| no| | SSH passsword,If a password is set, `ssh_private_key` is ignored. `for @v1.2.4 and greater`
8+
---
219

22-
> **Warning**
10+
## 🚀 **1. Inputs**
2311

24-
> Be careful when use `delete_remote_files`, This will delete remote path folder and all files in it
12+
| Name | Required | Default | Description |
13+
|------------------------|----------|---------|----------------------------------------------------------------------------------------------------------------------|
14+
| `username` | yes | | SSH username |
15+
| `server` | yes | | Remote host |
16+
| `port` | yes | 22 | Remote host port |
17+
| `ssh_private_key` | no | | You can copy the private key from your `ssh_private_key` file and save it to `repo/settings/secrets`<br> ![](./resource/secret.jpg) |
18+
| `local_path` | yes | `./*` | Local path of your project. <br> - Single file: `./myfile` <br> - Directory: `./static/*` <br> Default: `./*` (will put all files in your repo). |
19+
| `remote_path` | yes | | The target folder on the remote server. |
20+
| `sftp_only` | no | | If your port only accepts the sftp protocol, set this option to `true`. However, when set to `true`, the remote folder won't be automatically created. |
21+
| `sftpArgs` | no | | Extra arguments you want to pass to `sftp`, for example: `-o ConnectTimeout=5` |
22+
| `delete_remote_files` | no | false | Set to `true` to delete the remote path folder and all files in it **before** uploading. |
23+
| `password` | no | | SSH password. If a password is set, `ssh_private_key` is ignored. *(for @v1.2.4 and greater)* |
24+
| `rsyncArgs` | no | | Additional arguments for the `rsync` command. You can customize file synchronization behavior, such as excluding files or directories. Example: `--exclude=node_modules --exclude=.git --exclude=*.log`. *(for @v1.2.5 and greater)* |
2525

26-
-----
26+
> ⚠️ **Warning:**
27+
> Be careful when using `delete_remote_files`. This will **permanently delete** the remote path folder and all files in it **before** uploading.
2728
28-
## 2.Action Examples
29+
---
30+
31+
## 📦 **2. Action Examples**
32+
33+
### **🔹 Basic Example**
2934

3035
```yaml
3136
on: [push]
3237

3338
jobs:
34-
deploy_job:
39+
deploy_job:
3540
runs-on: ubuntu-latest
36-
name: deploy
41+
name: Deploy Files
3742
steps:
38-
- name: Checkout
43+
- name: Checkout
3944
uses: actions/checkout@v2
40-
- name: deploy file
41-
uses: wlixcc/SFTP-Deploy-Action@v1.2.4
45+
46+
- name: Deploy to Server
47+
uses: wlixcc/SFTP-Deploy-Action@v1.2.5
4248
with:
43-
username: 'root'
44-
server: 'your server ip'
45-
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
46-
local_path: './static/*'
47-
remote_path: '/var/www/app'
48-
sftpArgs: '-o ConnectTimeout=5'
49+
username: 'root'
50+
server: 'your server ip'
51+
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
52+
local_path: './static/*'
53+
remote_path: '/var/www/app'
54+
sftpArgs: '-o ConnectTimeout=5'
4955
```
5056
57+
---
58+
59+
### **🔹 Example with rsyncArgs**
60+
5161
```yaml
5262
on: [push]
5363

5464
jobs:
55-
deploy_job:
65+
deploy_job:
5666
runs-on: ubuntu-latest
57-
name: deploy
67+
name: Deploy with rsync exclude
5868
steps:
59-
- name: Checkout
69+
- name: Checkout
6070
uses: actions/checkout@v2
61-
- name: Deploy file
62-
uses: wlixcc/SFTP-Deploy-Action@v1.2.4
71+
72+
- name: Deploy with Exclude Patterns
73+
uses: wlixcc/SFTP-Deploy-Action@v1.2.5
6374
with:
64-
username: ${{ secrets.FTP_USERNAME }}
65-
server: ${{ secrets.FTP_SERVER }}
66-
port: ${{ secrets.FTP_PORT }}
67-
local_path: './static/*'
68-
remote_path: '/var/www/app'
69-
sftp_only: true
70-
password: ${{ secrets.FTP_PASSWORD }}
75+
username: 'root'
76+
server: 'your server ip'
77+
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
78+
local_path: './'
79+
remote_path: '/var/www/app'
80+
rsyncArgs: '--exclude=node_modules --exclude=.git --exclude=*.log'
81+
sftpArgs: '-o ConnectTimeout=5'
7182
```
7283
73-
## 3. [Deploy React App Example](https://github.com/wlixcc/React-Deploy)
84+
---
7485
86+
### **🔹 Example with Password Authentication**
7587
7688
```yaml
7789
on: [push]
7890

7991
jobs:
80-
deploy_job:
92+
deploy_job:
8193
runs-on: ubuntu-latest
82-
name: build&deploy
94+
name: Deploy with Password
8395
steps:
84-
# To use this repository's private action, you must check out the repository
85-
- name: Checkout
96+
- name: Checkout
8697
uses: actions/checkout@v2
8798

88-
- name: Install Dependencies
99+
- name: Deploy with Password
100+
uses: wlixcc/SFTP-Deploy-Action@v1.2.5
101+
with:
102+
username: ${{ secrets.FTP_USERNAME }}
103+
server: ${{ secrets.FTP_SERVER }}
104+
port: ${{ secrets.FTP_PORT }}
105+
local_path: './static/*'
106+
remote_path: '/var/www/app'
107+
sftp_only: true
108+
password: ${{ secrets.FTP_PASSWORD }}
109+
```
110+
111+
---
112+
113+
## 🌐 **3. [Deploy React App Example](https://github.com/wlixcc/React-Deploy)**
114+
115+
```yaml
116+
on: [push]
117+
118+
jobs:
119+
deploy_job:
120+
runs-on: ubuntu-latest
121+
name: Build & Deploy React App
122+
steps:
123+
- name: Checkout
124+
uses: actions/checkout@v2
125+
126+
- name: Install Dependencies
89127
run: yarn
90-
- name: Build
128+
129+
- name: Build
91130
run: yarn build
92131

93-
- name: deploy file to server
94-
uses: wlixcc/SFTP-Deploy-Action@v1.2.4
132+
- name: Deploy Build Folder
133+
uses: wlixcc/SFTP-Deploy-Action@v1.2.5
95134
with:
96-
username: 'root'
97-
server: '${{ secrets.SERVER_IP }}'
98-
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
99-
local_path: './build/*'
100-
remote_path: '/var/www/react-app'
101-
sftpArgs: '-o ConnectTimeout=5'
135+
username: 'root'
136+
server: '${{ secrets.SERVER_IP }}'
137+
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
138+
local_path: './build/*'
139+
remote_path: '/var/www/react-app'
140+
rsyncArgs: '--exclude=*.map'
141+
sftpArgs: '-o ConnectTimeout=5'
102142
```
103143
104-
![](./resource/reactExample.jpg)
105-
106-
--------
144+
![](./resource/reactExample.jpg)
145+
146+
---
147+
148+
## 🛡️ **4. Invalid format? You need to keep the format**
149+
150+
If you use the **Ed25519** algorithm to generate an SSH key pair:
151+
152+
```sh
153+
ssh-keygen -t ed25519 -C "your_email@example.com"
154+
```
155+
156+
⚠️ **Important:**
157+
The last line of the private key **must remain a blank line** when adding it to **Repository Secrets**.
158+
If you remove it, you might encounter an `invalid format` error.
159+
160+
![](./resource/keepformat.jpg)
107161

108-
## 4.Invalid format? You need keep format
162+
---
109163

110-
If you use the Ed25519 algorithm to generate an SSH key pair `ssh-keygen -t ed25519 -C "your_email@example.com"`,
111-
you need to note that the last line of the private key is a blank line. You need to keep this line when adding Repository secrets, otherwise it may lead to an `invalid format` error.
112-
![](./resource/keepformat.jpg)
113164

114-

action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ inputs:
3636
password:
3737
description: "SSH passsword,If a password is set, the secret key pair is ignored"
3838
required: false
39+
rsyncArgs:
40+
description: "Additional arguments for the rsync command.
41+
You can use this parameter to customize the rsync behavior, such as excluding files or directories.
42+
Example: '--exclude=node_modules --exclude=.git --exclude=*.log'.
43+
If set, these arguments will be passed directly to the rsync command."
44+
required: false
45+
default: ""
3946

4047

4148
runs:
@@ -52,6 +59,8 @@ runs:
5259
- ${{ inputs.sftpArgs }}
5360
- ${{ inputs.delete_remote_files }}
5461
- ${{ inputs.password }}
62+
- ${{ inputs.rsyncArgs }}
63+
5564

5665
branding:
5766
icon: 'upload-cloud'

entrypoint.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,42 @@ set -eu
66
TEMP_SSH_PRIVATE_KEY_FILE='../private_key.pem'
77
TEMP_SFTP_FILE='../sftp'
88

9+
# 定义一个本地临时目录,用于 rsync 过滤后存放文件
10+
RSYNC_LOCAL_DEST="../filtered_upload"
11+
12+
RSYNC_ARGS="${11}"
13+
14+
# 如果用户“未”设置 exclude(EXCLUDE_PATTERNS 为空),则直接进入原逻辑
15+
if [ -z "$RSYNC_ARGS" ]; then
16+
echo "===> No rsync args provided, skip rsync filtering..."
17+
else
18+
echo "===> rsync args detected, start rsync filtering..."
19+
20+
# 先清理并创建该临时目录
21+
rm -rf "$RSYNC_LOCAL_DEST"
22+
mkdir -p "$RSYNC_LOCAL_DEST"
23+
24+
# 6) 打印完整的 rsync 命令,方便调试
25+
echo "===> rsync command: rsync -av $RSYNC_ARGS $5 $RSYNC_LOCAL_DEST/"
26+
27+
# 执行 rsync 命令
28+
rsync -av $RSYNC_ARGS $5 $RSYNC_LOCAL_DEST/
29+
30+
# 将 $5 替换为过滤后的目录下的所有文件,这样后面原脚本里 put -r $5 $6 就无需改动其他地方
31+
set -- "$1" "$2" "$3" "$4" "$RSYNC_LOCAL_DEST/*" "$6" "$7" "$8" "$9" "${10}"
32+
33+
echo "===> Done rsync filtering. Continue original script..."
34+
fi
35+
936
# make sure remote path is not empty
1037
if [ -z "$6" ]; then
1138
echo 'remote_path is empty'
1239
exit 1
1340
fi
1441

1542
# use password
16-
if [ -z != ${10} ]; then
43+
if [ -n "${10}" ]; then
1744
echo 'use sshpass'
18-
apk add sshpass
1945

2046
if test $9 == "true";then
2147
echo 'Start delete remote files'

0 commit comments

Comments
 (0)