Skip to content

Commit

Permalink
v2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyucode committed Oct 26, 2024
1 parent 443c2fa commit 13867bb
Show file tree
Hide file tree
Showing 11 changed files with 33,728 additions and 31,016 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
id
node_modules/
node_modules/
package-lock.json
test.js
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

this is a github action script for upload files to server via SFTP protocol.

![Snyk Vulnerabilities for GitHub Repo](https://img.shields.io/snyk/vulnerabilities/github/wangyucode/sftp-upload-action)
![release](https://flat.badgen.net/github/release/wangyucode/sftp-upload-action)
[![Depfu](https://badges.depfu.com/badges/4b5cc2f5563a240e7b6c6106ded3e4c0/overview.svg)](https://depfu.com/github/wangyucode/sftp-upload-action?project_id=37917)

Expand All @@ -15,7 +14,6 @@ this is a github action script for upload files to server via SFTP protocol.
password: 'password', # Optional.
privateKey: '', # Optional, your private key(Raw content or key path).
passphrase: '', # Optional.
agent: '', # Optional, path to the ssh-agent socket.
compress: false, # Optional, compress for ssh connection. Default to false.
localDir: 'dist', # Required, Absolute or relative to cwd.
remoteDir: '/path/to/dest' # Required, Absolute path only.
Expand All @@ -31,7 +29,7 @@ this is a github action script for upload files to server via SFTP protocol.

```yml
- name: SFTP uploader
uses: wangyucode/[email protected].2
uses: wangyucode/[email protected].3
with:
host: 'wycode.cn'
password: ${{ secrets.password }}
Expand All @@ -43,7 +41,7 @@ this is a github action script for upload files to server via SFTP protocol.
```yml
- name: SFTP uploader
uses: wangyucode/[email protected].2
uses: wangyucode/[email protected].3
with:
host: 'wycode.cn'
privateKey: ${{ secrets.key }}
Expand All @@ -67,7 +65,7 @@ jobs:
uses: actions/checkout@v2

- name: 📂 SFTP uploader # Upload to SFTP
uses: wangyucode/[email protected].2
uses: wangyucode/[email protected].3
with:
host: ${{ secrets.HOST }} # Recommended to put the credentials in github secrets.
username: ${{ secrets.USER }}
Expand Down
5 changes: 1 addition & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ inputs:
passphrase:
required: false
default: ''
agent:
required: false
default: ''
exclude:
required: false
default: ''
Expand All @@ -44,5 +41,5 @@ inputs:
required: false
default: false
runs:
using: 'node16'
using: 'node20'
main: 'dist/index.js'
Binary file added dist/build/Release/cpufeatures.node
Binary file not shown.
64,293 changes: 33,470 additions & 30,823 deletions dist/index.js

Large diffs are not rendered by default.

Binary file not shown.
53 changes: 38 additions & 15 deletions dist/licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@actions/exec
MIT
The MIT License (MIT)

Copyright 2019 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@actions/http-client
MIT
Actions Http Client for Node.js
Expand All @@ -35,9 +47,11 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


@vercel/ncc
@actions/io
MIT
Copyright 2018 ZEIT, Inc.
The MIT License (MIT)

Copyright 2019 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down Expand Up @@ -240,6 +254,28 @@ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cpu-features
MIT
Copyright Brian White. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

err-code
MIT

Expand Down Expand Up @@ -828,16 +864,3 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.


uuid
MIT
The MIT License (MIT)

Copyright (c) 2010-2020 Robert Kieffer and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129 changes: 72 additions & 57 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { lstat, opendir } = require('fs/promises');
const Client = require('ssh2-sftp-client');
const path = require('path');
const { default: minimatch } = require('minimatch');
const { minimatch } = require('minimatch');

class Deployer {

Expand All @@ -20,46 +20,54 @@ class Deployer {
this.sftp = new Client();
this.localFiles = [];
this.remoteFiles = [];
this.removeExtraFiles = [];

}

async sync() {
await this.readLocalFiles(this.config.localDir);
await this.sftp.connect(this.config);
console.log('connected to server');

await this.listLocalFiles(this.config.localDir);
console.log(`found ${this.localFiles.length} local files`);

await this.sftp.connect(this.config).catch(console.error);
await this.readRemoteFiles(this.config.remoteDir);

await this.listRemoteFiles(this.config.remoteDir);
console.log(`found ${this.remoteFiles.length} remote files`);

await this.upload();
console.log('upload successfully');
if(this.options.removeExtraFilesOnServer) await this.deleteExtraFilesOnServer();

await this.sftp.end();
console.log('disconnected from server');
}

async upload() {
// upload files to remote
for (const l of this.localFiles) {
if (this.isIgnoreFile(`${l.path}${l.isDirectory ? '/' : ''}`)) {
console.log(`ignoring local ${l.path}`);
continue;
}
const r = this.remoteFiles.find(r => r.path === l.path);
const r = this.remoteFiles.find(r => r.path === l.path.replace(/\\/g, '/'));
const localFile = path.join(this.config.localDir, l.path);
const remoteFile = path.join(this.config.remoteDir, l.path).replace(/\\/g, '/');
const remoteFile = path.posix.join(this.config.remoteDir, l.path.replace(/\\/g, '/'));
if (this.options.forceUpload) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''} force upload -> ${l.path}`);
if (!this.options.dryRun) {
// remove before upload
if (r) {
if (r.isDirectory) {
await this.sftp.rmdir(remoteFile, true);
} else {
await this.sftp.delete(remoteFile, true);
}
// remove before upload
if (r) {
if (r.isDirectory) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}deleting folder -> ${remoteFile}`);
if (!this.options.dryRun) await this.sftp.rmdir(remoteFile, true);
} else {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}deleting file -> ${remoteFile}`);
if (!this.options.dryRun) await this.sftp.delete(remoteFile, true);
}
await this.sftp.put(localFile, remoteFile);
}
if (l.isDirectory) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}creating folder -> ${remoteFile}`);
if (!this.options.dryRun) await this.sftp.mkdir(remoteFile, true);
} else {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}uploading file -> ${remoteFile}`);
if (!this.options.dryRun) await this.sftp.put(localFile, remoteFile);
}

} else {
if (r) {
if (r.isDirectory !== l.isDirectory) {
Expand All @@ -77,10 +85,10 @@ class Deployer {
}
} else if (!l.isDirectory) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}creating file -> ${remoteFile}`);
if (!this.options.dryRun){
if (!this.options.dryRun) {
await this.sftp.mkdir(path.dirname(remoteFile), true);
await this.sftp.put(localFile, remoteFile);
}
}
} else {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}creating folder -> ${remoteFile}`);
if (!this.options.dryRun) await this.sftp.mkdir(remoteFile, true);
Expand All @@ -90,87 +98,94 @@ class Deployer {
}


async readLocalFiles(localPath) {
async listLocalFiles(localPath) {
const stats = await lstat(localPath);
if (stats.isDirectory()) {
const dir = await opendir(localPath);
for await (const file of dir) {
const filePath = path.join(localPath, file.name);
const relativePath = this.getRelativePath(filePath, false);
if (this.isIgnoreFile(path.posix.resolve(relativePath))) {
console.log(`ignored local -> ${relativePath}`);
continue;
}
if (file.isDirectory()) {
console.log(`reading local folder -> ${filePath}`);
this.localFiles.push({ path: this.getRelativePath(filePath, false), isDirectory: true });
this.localFiles.push({ path: relativePath, isDirectory: true });
// recursion
await this.readLocalFiles(filePath);
await this.listLocalFiles(filePath);
} else {
console.log(`reading local file -> ${filePath}`);
const fileState = await lstat(filePath);
this.localFiles.push({ path: this.getRelativePath(filePath, false), size: fileState.size, mtime: fileState.mtimeMs });
this.localFiles.push({ path: relativePath, size: fileState.size, mtime: fileState.mtimeMs });
}
}
} else {
console.log(`reading local file -> ${localPath}`);
this.localFiles.push({ path: this.getRelativePath(localPath, false), size: stats.size, mtime: stats.mtimeMs });
}
}

async readRemoteFiles(remotePath) {
let canNotReadFolder = false;
const stats = await this.sftp.stat(remotePath).catch(() => {
console.error(`can not read remote folder -> ${remotePath}`);
canNotReadFolder = true;
});
if (canNotReadFolder) return;
if (stats.isDirectory) {
async listRemoteFiles(remotePath) {
let fileInfo = await this.sftp.exists(remotePath);
if (!fileInfo) {
console.log(`remote folder not exist -> ${remotePath}`);
return;
}
if (fileInfo === 'd') {
const filesOnServer = await this.sftp.list(remotePath);
for (const file of filesOnServer) {
const filePath = path.posix.join(remotePath, file.name);
const relativePath = this.getRelativePath(filePath, true);
let removed = false;
// remove extra files on remote side

if (this.isIgnoreFile(`${relativePath}${file.type === 'd' ? '/' : ''}`)) {
console.log(`ignoring remote ${relativePath}`);
continue;
}

if (this.options.removeExtraFilesOnServer) {
removed = await this.removeExtraFilesOnServer(relativePath, file.type === 'd');
const localFile = this.localFiles.find(l => relativePath === l.path.replace(/\\/g, '/'));
if(!localFile){
console.log(`found extra remote file -> ${filePath}`);
this.removeExtraFiles.push({filePath, isDirectory: file.type === 'd'});
}
}

if (file.type === 'd') {
// recursion
if (!removed) {
console.log(`reading remote folder -> ${filePath}`);
this.remoteFiles.push({ path: relativePath, isDirectory: true });
await this.readRemoteFiles(filePath);
}
this.remoteFiles.push({ path: relativePath, isDirectory: true });
await this.listRemoteFiles(filePath);
} else {
if (!removed) {
console.log(`reading remote file -> ${filePath}`);
this.remoteFiles.push({ path: relativePath, size: file.size, mtime: file.modifyTime });
}
this.remoteFiles.push({ path: relativePath, size: file.size, mtime: file.modifyTime });
}
}
} else {
console.log(`reading remote file -> ${remotePath}`);
this.remoteFiles.push({ path: this.getRelativePath(remotePath, true), size: stats.size, mtime: stats.modifyTime });
}
}


async deleteExtraFilesOnServer() {
for (const file of this.removeExtraFiles) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}removing extra ${file.isDirectory ? 'folder' : 'file'} '${file.filePath}' on remote.`);
if (!this.options.dryRun) file.isDirectory ? await this.sftp.rmdir(file.filePath, true) : await this.sftp.delete(file.filePath);
}
}

async removeExtraFilesOnServer(remotePath, isDirectory) {
const exist = this.localFiles.some(l => l.path === remotePath);
if (exist) return false;
if (this.isIgnoreFile(`${remotePath}${isDirectory ? '/' : ''}`)) {
console.log(`ignoring remote ${remotePath}`);
return false;
}
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}removing extra ${isDirectory ? 'folder' : 'file'} '${remotePath}' on remote.`);
const remoteFile = path.posix.join(this.config.remoteDir, remotePath);
if (!this.options.dryRun) isDirectory ? await this.sftp.rmdir(remoteFile, true) : await this.sftp.delete(remoteFile);
return true;
}

getRelativePath(file, isRemote) {
const dirPath = isRemote ? path.resolve(this.config.remoteDir) : path.resolve(this.config.localDir);
const filePath = isRemote ? path.resolve(file) : path.resolve(file);
const dirPath = isRemote ? path.posix.resolve(this.config.remoteDir) : path.resolve(this.config.localDir);
const filePath = isRemote ? path.posix.resolve(file) : path.resolve(file);
return dirPath === filePath ? filePath : filePath.replace(dirPath, '');
}

isIgnoreFile(path) {
return this.options.exclude.some(pattern => minimatch(path.replace(/^\//, ''), pattern));
return this.options.exclude.some(pattern => minimatch(path, pattern));
}
}

Expand Down
Loading

0 comments on commit 13867bb

Please sign in to comment.