1. support password

This commit is contained in:
wl
2022-07-01 09:23:58 +08:00
parent 33436f92f1
commit 6596a6c48c
4 changed files with 60 additions and 18 deletions

View File

@@ -1,21 +1,23 @@
name: Continuous Deploy #action名称 name: Continuous Deploy
on: [push] #在推送的时候运行此action on: [push]
jobs: jobs:
deploy_job: deploy_job:
runs-on: ubuntu-latest #运行环境 runs-on: ubuntu-latest
name: sftp name: sftp
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
# 利用action把build好的文件上传到服务器/var/www/react-app路径下,需要确认此目录已在服务端创建
- name: deploy file to server - name: deploy file to server
uses: ./ # Uses an action in the root directory uses: ./ # Uses an action in the root directory
with: with:
username: 'root' #ssh user name username: 'wl'
server: '${{ secrets.SERVER_IP }}' server: '${{ secrets.MAC_IP }}'
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: './action.yml' # 对应我们项目build的文件夹路径 local_path: './*'
remote_path: '/var/www/react-app/' remote_path: '/Users/wl/Downloads/t/a'
sftpArgs: '-o ConnectTimeout=5'
# sftp_only: true
password: ${{secrets.SSH_PASSWORD}}
# delete_remote_files: true # delete_remote_files: true

View File

@@ -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). `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 `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. `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` <strike>args</strike> `sftpArgs` | 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 `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 passswordIf 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 ## Action Example
@@ -62,14 +68,14 @@
run: yarn build run: yarn build
- name: deploy file to server - name: deploy file to server
uses: wlixcc/SFTP-Deploy-Action@v1.2.3 uses: wlixcc/SFTP-Deploy-Action@v1.2.4
with: with:
username: 'root' username: 'root'
server: '${{ secrets.SERVER_IP }}' server: '${{ secrets.SERVER_IP }}'
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: './build/*' local_path: './build/*'
remote_path: '/var/www/react-app' remote_path: '/var/www/react-app'
args: '-o ConnectTimeout=5' sftpArgs: '-o ConnectTimeout=5'
![](./resource/reactExample.jpg) ![](./resource/reactExample.jpg)
@@ -96,14 +102,14 @@
run: yarn build run: yarn build
- name: deploy file to server - name: deploy file to server
uses: wlixcc/SFTP-Deploy-Action@v1.2.3 uses: wlixcc/SFTP-Deploy-Action@v1.2.4
with: with:
username: 'root' username: 'root'
server: '${{ secrets.SERVER_IP }}' server: '${{ secrets.SERVER_IP }}'
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: './dist/*' local_path: './dist/*'
remote_path: '/var/www/umiapp' remote_path: '/var/www/umiapp'
args: '-o ConnectTimeout=5' sftpArgs: '-o ConnectTimeout=5'
![](./resource/umiExample.jpg) ![](./resource/umiExample.jpg)

View File

@@ -26,13 +26,16 @@ inputs:
description: 'connection via sftp protocol only' description: 'connection via sftp protocol only'
required: false required: false
default: false default: false
args: sftpArgs:
description: 'sftp args' description: 'sftp args'
required: false required: false
delete_remote_files: delete_remote_files:
description: 'This operation will delete all files in the remote path before upload. Please be careful set this to true' description: 'This operation will delete all files in the remote path before upload. Please be careful set this to true'
required: false required: false
default: false default: false
passsword:
description: "SSH passswordIf a password is set, the secret key pair is ignored"
required: false
runs: runs:
@@ -46,8 +49,9 @@ runs:
- ${{ inputs.local_path }} - ${{ inputs.local_path }}
- ${{ inputs.remote_path }} - ${{ inputs.remote_path }}
- ${{ inputs.sftp_only }} - ${{ inputs.sftp_only }}
- ${{ inputs.args }} - ${{ inputs.sftpArgs }}
- ${{ inputs.delete_remote_files }} - ${{ inputs.delete_remote_files }}
- ${{ inputs.password }}
branding: branding:
icon: 'upload-cloud' icon: 'upload-cloud'

View File

@@ -7,16 +7,46 @@ set -eu
TEMP_SSH_PRIVATE_KEY_FILE='../private_key.pem' TEMP_SSH_PRIVATE_KEY_FILE='../private_key.pem'
TEMP_SFTP_FILE='../sftp' TEMP_SFTP_FILE='../sftp'
# make sure remote path is not empty
if [ -z "$6" ]; then if [ -z "$6" ]; then
echo 'remote_path is empty' echo 'remote_path is empty'
exit 1 exit 1
fi 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 # keep string format
printf "%s" "$4" >$TEMP_SSH_PRIVATE_KEY_FILE printf "%s" "$4" >$TEMP_SSH_PRIVATE_KEY_FILE
# avoid Permissions too open # avoid Permissions too open
chmod 600 $TEMP_SSH_PRIVATE_KEY_FILE chmod 600 $TEMP_SSH_PRIVATE_KEY_FILE
# delete remote files if needed
if test $9 == "true";then if test $9 == "true";then
echo 'Start delete remote files' echo 'Start delete remote files'
ssh -o StrictHostKeyChecking=no -p $3 -i $TEMP_SSH_PRIVATE_KEY_FILE $1@$2 rm -rf $6 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 if test $7 = "true"; then
echo "Connection via sftp protocol only, skip the command to create a directory" echo "Connection via sftp protocol only, skip the command to create a directory"
else 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 ssh -o StrictHostKeyChecking=no -p $3 -i $TEMP_SSH_PRIVATE_KEY_FILE $1@$2 mkdir -p $6
fi fi