Skip to content

Commit

Permalink
1. support password
Browse files Browse the repository at this point in the history
  • Loading branch information
wlixcc committed Jul 1, 2022
1 parent 33436f9 commit 6596a6c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
local_path: './*'
remote_path: '/Users/wl/Downloads/t/a'
sftpArgs: '-o ConnectTimeout=5'
# sftp_only: true
password: ${{secrets.SSH_PASSWORD}}
# delete_remote_files: true
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<strike>args</strike> `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

Expand Down Expand Up @@ -62,14 +68,14 @@
run: yarn build

- name: deploy file to server
uses: wlixcc/[email protected].3
uses: wlixcc/[email protected].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)

Expand All @@ -96,14 +102,14 @@
run: yarn build

- name: deploy file to server
uses: wlixcc/[email protected].3
uses: wlixcc/[email protected].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)


Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
32 changes: 31 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 6596a6c

Please sign in to comment.