diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18fc9f1..7a04b0d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,21 +1,23 @@ -name: Continuous Deploy #action名称 -on: [push] #在推送的时候运行此action +name: Continuous Deploy +on: [push] jobs: deploy_job: - runs-on: ubuntu-latest #运行环境 + 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 }}' + username: 'wl' + server: '${{ secrets.MAC_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 + local_path: './*' + remote_path: '/Users/wl/Downloads/t/a' + sftpArgs: '-o ConnectTimeout=5' + # sftp_only: true + password: ${{secrets.SSH_PASSWORD}} + # delete_remote_files: true diff --git a/README.md b/README.md index 74405f7..e41d6aa 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,14 @@ `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 +args `sftpArgs` | no| | other args yor want to use of sftp, E.g.`-o ConnectTimeout=5` +`delete_remote_files` | no | false | **Warning** Set `true` will delete all files in the remote path before upload. Please be `careful` set this to true +`passowrd`| no| | SSH passsword,If a password is set, `ssh_private_key` is ignored + +> **Warning** +> be `careful` when use `delete_remote_files` This will remove all files in your remote path before uploading + + ## Action Example @@ -62,14 +68,14 @@ run: yarn build - name: deploy file to server - uses: wlixcc/SFTP-Deploy-Action@v1.2.3 + uses: wlixcc/SFTP-Deploy-Action@v1.2.4 with: username: 'root' server: '${{ secrets.SERVER_IP }}' ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} local_path: './build/*' remote_path: '/var/www/react-app' - args: '-o ConnectTimeout=5' + sftpArgs: '-o ConnectTimeout=5' ![](./resource/reactExample.jpg) @@ -96,14 +102,14 @@ run: yarn build - name: deploy file to server - uses: wlixcc/SFTP-Deploy-Action@v1.2.3 + uses: wlixcc/SFTP-Deploy-Action@v1.2.4 with: username: 'root' server: '${{ secrets.SERVER_IP }}' ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} local_path: './dist/*' remote_path: '/var/www/umiapp' - args: '-o ConnectTimeout=5' + sftpArgs: '-o ConnectTimeout=5' ![](./resource/umiExample.jpg) diff --git a/action.yml b/action.yml index d5d5cca..ce30e7c 100644 --- a/action.yml +++ b/action.yml @@ -26,13 +26,16 @@ inputs: description: 'connection via sftp protocol only' required: false default: false - args: + sftpArgs: 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 + passsword: + description: "SSH passsword,If a password is set, the secret key pair is ignored" + required: false runs: @@ -46,8 +49,9 @@ runs: - ${{ inputs.local_path }} - ${{ inputs.remote_path }} - ${{ inputs.sftp_only }} - - ${{ inputs.args }} + - ${{ inputs.sftpArgs }} - ${{ inputs.delete_remote_files }} + - ${{ inputs.password }} branding: icon: 'upload-cloud' diff --git a/entrypoint.sh b/entrypoint.sh index 186391c..ff988ef 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,16 +7,46 @@ set -eu TEMP_SSH_PRIVATE_KEY_FILE='../private_key.pem' TEMP_SFTP_FILE='../sftp' +# make sure remote path is not empty if [ -z "$6" ]; then echo 'remote_path is empty' exit 1 fi + +# use password +if [ -z != ${10} ]; then + echo 'use sshpass' + apk add sshpass + + if test $9 == "true";then + echo 'Start delete remote files' + sshpass -p ${10} ssh -o StrictHostKeyChecking=no -p $3 $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 'Create directory if needed' + sshpass -p ${10} ssh -o StrictHostKeyChecking=no -p $3 $1@$2 mkdir -p $6 + fi + + echo 'SFTP Start' + # create a temporary file containing sftp commands + printf "%s" "put -r $5 $6" >$TEMP_SFTP_FILE + #-o StrictHostKeyChecking=no avoid Host key verification failed. + SSHPASS=${10} sshpass -e sftp -oBatchMode=no -b $TEMP_SFTP_FILE -P $3 $8 -o StrictHostKeyChecking=no $1@$2 + + echo 'Deploy Success' + + exit 0 +fi + # keep string format printf "%s" "$4" >$TEMP_SSH_PRIVATE_KEY_FILE # avoid Permissions too open chmod 600 $TEMP_SSH_PRIVATE_KEY_FILE +# delete remote files if needed 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 @@ -25,7 +55,7 @@ fi if test $7 = "true"; then echo "Connection via sftp protocol only, skip the command to create a directory" else - echo 'SSH Start' + echo 'Create directory if needed' ssh -o StrictHostKeyChecking=no -p $3 -i $TEMP_SSH_PRIVATE_KEY_FILE $1@$2 mkdir -p $6 fi