From 3586e619fa989d42162239d9e9bb333446aca17c Mon Sep 17 00:00:00 2001 From: wl <356485255@qq.com> Date: Wed, 29 Jun 2022 20:47:36 +0800 Subject: [PATCH] 1. Option to let delete existing files --- .github/workflows/main.yml | 21 ++++++++++++++ README.md | 57 +++++++++++--------------------------- action.yml | 6 +++- entrypoint.sh | 6 +++- 4 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..0f9802e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,21 @@ +name: Continuous Deploy #action名称 +on: [push] #在推送的时候运行此action + +jobs: + deploy_job: + runs-on: ubuntu-latest #运行环境 + name: sftp + steps: + - name: Checkout + uses: actions/checkout@v2 + + # 利用action把build好的文件上传到服务器/var/www/react-app路径下,需要确认此目录已在服务端创建 + - name: deploy file to server + uses: ./ # Uses an action in the root directory + with: + username: 'root' #ssh user name + server: '${{ secrets.SERVER_IP }}' + ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} + local_path: './action.yml' # 对应我们项目build的文件夹路径 + remote_path: '/var/www/react-app/' + delete_remote_files: true \ No newline at end of file diff --git a/README.md b/README.md index 0e91568..03a3d3d 100644 --- a/README.md +++ b/README.md @@ -3,46 +3,21 @@ > Use this action to deploy your files to server using `SSH Private Key` > 使用此`action`部署你的项目到服务器上,`仅支持密钥对连接` +中文介绍链接:[使用Github Action 部署项目到云服务器](https://zhuanlan.zhihu.com/p/107545396) -> [使用Github Action 部署项目到云服务器](https://zhuanlan.zhihu.com/p/107545396) - - -## Inputs - -### `username` - -**Required** sftp username. - -### `server` - -**Required** sftp server address. - -### `port` - -sftp srever port , default `22` - -### `ssh_private_key` - - **Required** you can copy private_key from your `ssh_private_key.pem file`, keep format, and save at`repo/settings/secrets` - - -![](./resource/secret.jpg) - -### `local_path` - - **Required** `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). - -### `remote_path` - - **Required** remote_path - -### `sftp_only` - -connection via sftp protocol only, the default value is `false`. 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. - -### `args` -args of sftp cmd, E.g.`-o ConnectTimeout=5` +## 1. Inputs +| Name | Required | Default | Description | +|------------------------|----------------------|---------|-----------------------------------------------| +`username` | yes| | SSH username +`server` | yes| | Remote host +`port`| yes | 22 | Remote host port +`ssh_private_key`| yes| | You can copy private key from your `ssh_private_key.pem` file, and save to`repo/settings/secrets`![](./resource/secret.jpg) +`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). +`remote_path`|yes|/| Remote path +`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. +`args` | no| | other args yor want to use of sftp, E.g.`-o ConnectTimeout=5` +`delete_remote_files` | no | false | Set `true` will delete all files in the remote path before upload. Please be `careful` set this to true ## Action Example @@ -57,7 +32,7 @@ args of sftp cmd, E.g.`-o ConnectTimeout=5` - name: Checkout uses: actions/checkout@v2 - name: deploy file - uses: wlixcc/SFTP-Deploy-Action@v1.0 + uses: wlixcc/SFTP-Deploy-Action@v1.2.3 with: username: 'root' server: 'your server ip' @@ -87,7 +62,7 @@ args of sftp cmd, E.g.`-o ConnectTimeout=5` run: yarn build - name: deploy file to server - uses: wlixcc/SFTP-Deploy-Action@v1.0 + uses: wlixcc/SFTP-Deploy-Action@v1.2.3 with: username: 'root' server: '${{ secrets.SERVER_IP }}' @@ -121,7 +96,7 @@ args of sftp cmd, E.g.`-o ConnectTimeout=5` run: yarn build - name: deploy file to server - uses: wlixcc/SFTP-Deploy-Action@v1.0 + uses: wlixcc/SFTP-Deploy-Action@v1.2.3 with: username: 'root' server: '${{ secrets.SERVER_IP }}' diff --git a/action.yml b/action.yml index 9cd1561..0e7a38a 100644 --- a/action.yml +++ b/action.yml @@ -27,10 +27,13 @@ inputs: description: 'connection via sftp protocol only' required: false default: false - args: description: 'sftp args' required: false + delete_remote_files: + description: 'This operation will delete all files in the remote path before upload. Please be careful set this to true' + required: false + default: false runs: @@ -45,6 +48,7 @@ runs: - ${{ inputs.remote_path }} - ${{ inputs.sftp_only }} - ${{ inputs.args }} + - ${{ inputs.delete_remote_files }} branding: icon: 'upload-cloud' diff --git a/entrypoint.sh b/entrypoint.sh index 7b23391..15822ae 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -12,11 +12,15 @@ printf "%s" "$4" >$TEMP_SSH_PRIVATE_KEY_FILE # avoid Permissions too open chmod 600 $TEMP_SSH_PRIVATE_KEY_FILE +if test $9 == "true";then + echo 'start delete remote files' + ssh -o StrictHostKeyChecking=no -p $3 -i $TEMP_SSH_PRIVATE_KEY_FILE $1@$2 rm -rf $6 +fi + if test $7 = "true"; then echo "Connection via sftp protocol only, skip the command to create a directory" else echo 'ssh start' - ssh -o StrictHostKeyChecking=no -p $3 -i $TEMP_SSH_PRIVATE_KEY_FILE $1@$2 mkdir -p $6 fi