Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to load some private ssh details from a .env file #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ deploy:
port: [port] # Default is 22
delete: [true|false] # Default is true
progress: [true|false] # Default is true
dotenv: [true|false] # Default is false
args: <rsync args>
rsh: <remote shell>
key: <key>
@@ -45,6 +46,16 @@ deploy:
- **ignore_errors**: Ignore errors
- **create_before_update**: First create non-existing files, then update existing files
# Env based options
When `deploy.dotenv` config option is set to TRUE, some options can be ommitted and they will be loaded from following ENV variables instead:
- `HEXO_RSYNC_HOST` -> maps to `deploy.host`
- `HEXO_RSYNC_PORT` -> maps to `deploy.port`
- `HEXO_RSYNC_USER` -> maps to `deploy.user`
- `HEXO_RSYNC_ROOT` -> maps to `deploy.root`

This can be useful in a situation when you do not want to store your SSH details in the repository, but load them from your deployment environment.

## License

MIT
32 changes: 30 additions & 2 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,10 @@
const color = require('picocolors');
const { spawn } = require('hexo-util');
const pathFn = require('path');
require('dotenv/config');

module.exports = function(args) {
if (!args.host || !args.user || !args.root) {
if (!args.dotenv && (!args.host || !args.user || !args.root)) {
let help = '';

help += 'You should configure deployment settings in _config.yml first!\n\n';
@@ -18,6 +19,7 @@ module.exports = function(args) {
help += ' port: [port] # Default is 22\n';
help += ' delete: [true|false] # Default is true\n';
help += ' progress: [true|false] # Default is true\n';
help += ' dotenv: [true|false] # Default is false\n';
help += ' args: <rsync args>\n';
help += ' rsh: <remote shell>\n';
help += ' key: <key>\n';
@@ -35,6 +37,32 @@ module.exports = function(args) {
if (!Object.prototype.hasOwnProperty.call(args, 'progress')) args.progress = true;
if (!Object.prototype.hasOwnProperty.call(args, 'ignore_errors')) args.ignore_errors = false;
if (!Object.prototype.hasOwnProperty.call(args, 'create_before_update')) args.create_before_update = false;
if (!Object.prototype.hasOwnProperty.call(args, 'dotenv')) args.dotenv = true;

if (args.dotenv === true) {
const {
HEXO_RSYNC_HOST,
HEXO_RSYNC_PORT,
HEXO_RSYNC_USER,
HEXO_RSYNC_ROOT,
} = process.env;

if (HEXO_RSYNC_HOST) {
args.host = HEXO_RSYNC_HOST;
}

if (HEXO_RSYNC_PORT) {
args.port = HEXO_RSYNC_PORT;
}

if (HEXO_RSYNC_USER) {
args.user = HEXO_RSYNC_USER;
}

if (HEXO_RSYNC_ROOT) {
args.root = HEXO_RSYNC_ROOT;
}
}

const params = [
'-az',
@@ -74,4 +102,4 @@ module.exports = function(args) {
});
}
return spawn('rsync', params, {verbose: true});
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
"mocha": "^10.2.0"
},
"dependencies": {
"dotenv": "^16.4.1",
"hexo-util": "^3.0.1",
"picocolors": "^1.0.0"
},