-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (58 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const core = require('@actions/core');
const exec = require('@actions/exec');
const path = require('path');
/**
* Add common command parameters based on environment and config settings
* @param {string[]} command - The command array to add parameters to
* @param {string} environment - The environment parameter value
* @param {string} configFile - The config file parameter value
* @returns {string[]} - The updated command array
*/
function addCommandParameters(command, environment, configFile) {
// Add the `--destination` flag if environment is provided
if (environment) {
command.push(`--destination=${environment}`);
}
// Add the `--config-file` flag if configFile is provided
if (configFile) {
command.push(`--config-file=${configFile}`);
}
return command;
}
async function run() {
try {
const registryUsername = core.getInput('registry-username', { required: true });
const registryPassword = core.getInput('registry-password', { required: true });
const workdir = core.getInput('workdir');
const kamal = core.getInput('kamal');
const environment = core.getInput('environment');
const configFile = core.getInput('config-file');
core.exportVariable('KAMAL_REGISTRY_USERNAME', registryUsername);
core.exportVariable('KAMAL_REGISTRY_PASSWORD', registryPassword);
core.exportVariable('DOCKER_BUILDKIT', 1);
// Build the deploy command args as an array
let deployCommand = ['deploy'];
deployCommand = addCommandParameters(deployCommand, environment, configFile);
// Use the provided workdir
const cwd = path.resolve(workdir);
// Execute the deployment command
await exec.exec(kamal, deployCommand, { cwd });
// Handle cancellation
process.on('SIGINT', async () => {
core.warning('Action canceled, releasing resources...');
try {
// Build the lock command args as an array
let lockCommand = ['lock', 'release'];
lockCommand = addCommandParameters(lockCommand, environment, configFile);
await exec.exec(kamal, lockCommand, { cwd });
core.info('Kamal lock released successfully.');
} catch (error) {
core.setFailed(`Failed to release Kamal lock: ${error.message}`);
}
process.exit(1); // Exit the process
});
} catch (error) {
core.setFailed(error.message);
}
}
run();