auto-deploy-sh 2.0.2 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/dist/bin/config.js +28 -1
- package/dist/bin/deploy.d.ts +1 -0
- package/dist/bin/deploy.js +49 -3
- package/dist/bin/index.js +1 -1
- package/dist/bin/type/types.d.ts +2 -1
- package/dist/bin/utils/inquirer.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,7 +73,12 @@ The configuration file is **fixed as `deploy-config.json`**. If missing, the too
|
|
|
73
73
|
- `dockerBuildFiles`: Files/directories to include in the Docker build context (required for Dockerfile execution).
|
|
74
74
|
- `imageTag`: Docker image tag, formatted as `[registry-url/][username/project-name]:[tag]` (e.g., `my-registry.com/user/my-app:v1.0`).
|
|
75
75
|
- `containerName`: Unique name for the running container (ensures no conflicts with existing containers).
|
|
76
|
-
- `BindPorts
|
|
76
|
+
- `BindPorts` (optional): Port mapping, formatted as `<host-port>:<container-port>` (e.g., `"8080:80"`).
|
|
77
|
+
- `restart` (optional): Container restart policy, controls how Docker handles container restarts. The following options are supported:
|
|
78
|
+
- `no`(default): The container **will not restart automatically after it exits**. It will also remain stopped after Docker or system restarts. Suitable for one-off tasks or debugging scenarios.
|
|
79
|
+
- `always`: The container **will always restart automatically** if it stops. It will also start automatically after Docker or system restarts.Even if the container is manually stopped, it will be restarted again after Docker restarts.
|
|
80
|
+
- `unless-stopped`(recommended): The container will automatically restart on failure and start after Docker or system restarts.**If the container is manually stopped, it will not be restarted again**, making it suitable for long-running production services.
|
|
81
|
+
- `on-failure`: The container **will restart only if it exits with a non-zero status code**. It will not restart on normal exits (exit code 0).
|
|
77
82
|
- `Options`: **Optional** advanced settings (all sub-properties are optional)
|
|
78
83
|
- `volumes`: Volume mappings (array of strings), formatted as `[host-path/volume-name]:[container-path]:[optional-flags]` (e.g., `["/host/data:/container/data:ro"]`).
|
|
79
84
|
- `networks`: Connect the container to a custom Docker network (created via `docker network create <network-name>`) for inter-container communication.
|
|
@@ -92,9 +97,10 @@ The configuration file is **fixed as `deploy-config.json`**. If missing, the too
|
|
|
92
97
|
"imageTag": "string",
|
|
93
98
|
"containerName": "string",
|
|
94
99
|
"BindPorts": "string",
|
|
100
|
+
"restart": "'no' | 'always' | 'unless-stopped' | 'on-failure'",
|
|
95
101
|
"Options": {
|
|
96
102
|
"volumes": "string[]",
|
|
97
|
-
"networks": "string"
|
|
103
|
+
"networks": "string[]"
|
|
98
104
|
}
|
|
99
105
|
}
|
|
100
106
|
```
|
|
@@ -113,9 +119,10 @@ The configuration file is **fixed as `deploy-config.json`**. If missing, the too
|
|
|
113
119
|
"imageTag": "my-app:latest",
|
|
114
120
|
"containerName": "my-app-container",
|
|
115
121
|
"BindPorts": "80:80",
|
|
122
|
+
"restart": "unless-stopped",
|
|
116
123
|
"Options": {
|
|
117
124
|
"volumes": ["/host/logs:/app/logs:rw"],
|
|
118
|
-
"networks": "my-custom-network"
|
|
125
|
+
"networks": ["my-custom-network", "my-custom-network-1"]
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
128
|
```
|
package/dist/bin/config.js
CHANGED
|
@@ -43,6 +43,32 @@ const configMethod = {
|
|
|
43
43
|
type: 'input',
|
|
44
44
|
message: 'Bind Ports(such as 8080:8080): ',
|
|
45
45
|
},
|
|
46
|
+
restart: {
|
|
47
|
+
type: 'select',
|
|
48
|
+
message: 'Container restart policy (when should Docker restart this container?)',
|
|
49
|
+
choices: [
|
|
50
|
+
{
|
|
51
|
+
name: 'no (default)',
|
|
52
|
+
value: 'no',
|
|
53
|
+
description: 'Never restart. Container stays stopped even after Docker or system restarts.',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'always',
|
|
57
|
+
value: 'always',
|
|
58
|
+
description: 'Always restart if it stops. Will also start automatically after Docker or system reboot.',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'unless-stopped (recommended)',
|
|
62
|
+
value: 'unless-stopped',
|
|
63
|
+
description: 'Restart automatically unless you manually stop it. Will NOT restart again if stopped by user.',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: 'on-failure',
|
|
67
|
+
value: 'on-failure',
|
|
68
|
+
description: 'Restart only on non-zero exit code. Useful for jobs or scripts that may fail.',
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
46
72
|
};
|
|
47
73
|
const optionsMethod = {
|
|
48
74
|
volumes: {
|
|
@@ -52,7 +78,8 @@ const optionsMethod = {
|
|
|
52
78
|
},
|
|
53
79
|
networks: {
|
|
54
80
|
type: 'input',
|
|
55
|
-
message: 'Bound
|
|
81
|
+
message: 'Bound networks (separated by , ): ',
|
|
82
|
+
handleFn: (value) => value.split(','),
|
|
56
83
|
},
|
|
57
84
|
};
|
|
58
85
|
export async function createConfig(rootPath) {
|
package/dist/bin/deploy.d.ts
CHANGED
|
@@ -29,5 +29,6 @@ export declare class Deploy {
|
|
|
29
29
|
dockcerImageBuild(ssh: NodeSSH): Promise<void>;
|
|
30
30
|
buildDocker(ssh: NodeSSH, IMAGE_TAG: string, REMOTEAPPPATH: string, TARGET_DIR: string, CONTAINER_NAME: string): Promise<void>;
|
|
31
31
|
runDocker(ssh: NodeSSH, CONTAINER_NAME: string): Promise<void>;
|
|
32
|
+
judgeNetwork(ssh: NodeSSH, networks: string[]): Promise<void>;
|
|
32
33
|
clear(ssh: NodeSSH): Promise<void>;
|
|
33
34
|
}
|
package/dist/bin/deploy.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { log, ora, run, fs, exit, chalk } from "./utils/index.js";
|
|
1
|
+
import { log, ora, run, fs, exit, chalk, inquirer } from "./utils/index.js";
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import archiver from 'archiver';
|
|
4
4
|
export class Deploy {
|
|
@@ -186,6 +186,13 @@ export class Deploy {
|
|
|
186
186
|
const REMOTE_PATH = this.remotePath;
|
|
187
187
|
const IMAGE_TAG = this.config.imageTag;
|
|
188
188
|
const TARGET_DIR = `./${CONTAINER_NAME}`;
|
|
189
|
+
if (typeof this.config.Options?.networks === 'string') {
|
|
190
|
+
this.config.Options.networks = [this.config.Options.networks];
|
|
191
|
+
}
|
|
192
|
+
const networks = this.config.Options?.networks || [];
|
|
193
|
+
if (networks.length > 0) {
|
|
194
|
+
await this.judgeNetwork(ssh, networks);
|
|
195
|
+
}
|
|
189
196
|
await this.execCommand(ssh, {
|
|
190
197
|
startMsg: 'Start decompress project....',
|
|
191
198
|
succMsg: 'Decompress project completed.',
|
|
@@ -234,7 +241,10 @@ export class Deploy {
|
|
|
234
241
|
`);
|
|
235
242
|
}
|
|
236
243
|
async runDocker(ssh, CONTAINER_NAME) {
|
|
237
|
-
let optionsCLI = `--name ${CONTAINER_NAME} -p ${this.config.BindPorts}`;
|
|
244
|
+
let optionsCLI = `--name ${CONTAINER_NAME}${this.config.BindPorts ? ` -p ${this.config.BindPorts}` : ''}`;
|
|
245
|
+
if (this.config.restart) {
|
|
246
|
+
optionsCLI += ` --restart ${this.config.restart}`;
|
|
247
|
+
}
|
|
238
248
|
const options = this.config.Options;
|
|
239
249
|
if (options) {
|
|
240
250
|
const optionsKeys = Object.keys(options);
|
|
@@ -248,7 +258,14 @@ export class Deploy {
|
|
|
248
258
|
break;
|
|
249
259
|
}
|
|
250
260
|
case 'networks': {
|
|
251
|
-
|
|
261
|
+
let cli = '';
|
|
262
|
+
if (Array.isArray(options.networks)) {
|
|
263
|
+
cli = options.networks.map((network) => `--network ${network}`).join(' ');
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
cli = `--network ${options.networks}`;
|
|
267
|
+
}
|
|
268
|
+
optionsCLI += ` ${cli}`;
|
|
252
269
|
break;
|
|
253
270
|
}
|
|
254
271
|
default:
|
|
@@ -265,6 +282,35 @@ export class Deploy {
|
|
|
265
282
|
docker run -d ${optionsCLI}
|
|
266
283
|
`);
|
|
267
284
|
}
|
|
285
|
+
async judgeNetwork(ssh, networks) {
|
|
286
|
+
const { stderr, code, stdout } = await ssh.execCommand(`docker network ls --format '{{.Name}}'`);
|
|
287
|
+
if (code !== 0 || (stderr && code !== 0)) {
|
|
288
|
+
stderr && log.error(stderr);
|
|
289
|
+
throw new Error();
|
|
290
|
+
}
|
|
291
|
+
const existNetworks = new Set(stdout
|
|
292
|
+
.split('\n')
|
|
293
|
+
.map(item => item.trim())
|
|
294
|
+
.filter(Boolean));
|
|
295
|
+
const missingNetworks = networks.filter(network => !existNetworks.has(network));
|
|
296
|
+
if (missingNetworks.length > 0) {
|
|
297
|
+
const answer = await inquirer.invoke({
|
|
298
|
+
type: 'confirm',
|
|
299
|
+
message: `The following Docker networks do not exist:\n${JSON.stringify(missingNetworks)}\nDo you want to create them now?`,
|
|
300
|
+
});
|
|
301
|
+
if (answer) {
|
|
302
|
+
let cli = missingNetworks.map(name => `docker network create ${name}`).join(' && ');
|
|
303
|
+
await this.execCommand(ssh, {
|
|
304
|
+
startMsg: `Creating missing Docker networks...`,
|
|
305
|
+
succMsg: `Missing Docker networks created successfully.`,
|
|
306
|
+
}, cli);
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
log.error('Deployment aborted due to missing Docker networks.');
|
|
310
|
+
throw new Error();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
268
314
|
async clear(ssh) {
|
|
269
315
|
await ssh.execCommand(`rm -f ${this.remotePath}`);
|
|
270
316
|
await ssh.execCommand(`rm -rf ${this.REMOTEAPPPATH}/${this.config.containerName}`);
|
package/dist/bin/index.js
CHANGED
|
@@ -9,7 +9,7 @@ export async function deploySSH(configFile = 'deploy-config.json') {
|
|
|
9
9
|
const CONFIG_FILE_PATH = path.resolve(process.cwd(), CONFIG_FILE);
|
|
10
10
|
if (await fs.exist(CONFIG_FILE_PATH)) {
|
|
11
11
|
let config = JSON.parse((await fs.readDirOrFile(CONFIG_FILE_PATH, { encoding: 'utf-8' })));
|
|
12
|
-
if (!config.BindPorts.includes(':')) {
|
|
12
|
+
if (config.BindPorts && !config.BindPorts.includes(':')) {
|
|
13
13
|
log.error('Config BindPorts do not meet the standard');
|
|
14
14
|
exit(1);
|
|
15
15
|
}
|
package/dist/bin/type/types.d.ts
CHANGED
|
@@ -10,9 +10,10 @@ export type sshConfig = {
|
|
|
10
10
|
imageTag: string;
|
|
11
11
|
containerName: string;
|
|
12
12
|
BindPorts: string;
|
|
13
|
+
restart?: 'no' | 'always' | 'on-failure' | 'unless-stopped';
|
|
13
14
|
Options: {
|
|
14
15
|
volumes: string[];
|
|
15
|
-
networks: string;
|
|
16
|
+
networks: string[];
|
|
16
17
|
};
|
|
17
18
|
};
|
|
18
19
|
export type fileData = string | NodeJS.ArrayBufferView;
|