-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
163 lines (136 loc) · 5.15 KB
/
run.js
File metadata and controls
163 lines (136 loc) · 5.15 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const path = require('path');
const { existsSync } = require('fs');
const { getInput, setFailed } = require('@actions/core');
const exec = require('@actions/exec');
// Writes a message to the console
// eslint-disable-next-line no-undef
const log = console.log.bind(console);
// Sleep function for delays between retries
// eslint-disable-next-line no-undef
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Retry configuration from environment variables with defaults
const RETRY_COUNT = parseInt(process.env.RETRY_COUNT || '3', 10);
const RETRY_DELAY = parseInt(process.env.RETRY_DELAY || '10000', 10); // default 10 seconds
// Retry wrapper function
async function withRetry(operation, operationName) {
let lastError;
for (let attempt = 1; attempt <= RETRY_COUNT; attempt++) {
try {
log(`${operationName}: Attempt ${attempt}/${RETRY_COUNT}`);
await operation();
log(`${operationName}: Succeeded on attempt ${attempt}`);
return;
} catch (error) {
lastError = error;
log(`${operationName}: Failed attempt ${attempt}/${RETRY_COUNT}`);
log(`Error: ${error.message}`);
if (attempt < RETRY_COUNT) {
log(`Waiting ${RETRY_DELAY / 1000} seconds before next attempt...`);
await sleep(RETRY_DELAY);
}
}
}
throw new Error(
`${operationName}: Failed after ${RETRY_COUNT} attempts. Last error: ${lastError.message}`,
);
}
// Sets the specified env variable if the value isn't empty
function setEnv(name, value) {
if (value) {
process.env[name] = value;
}
}
// Returns the value for an input variable, or null if it's not defined.
function getActionInput(name, required = false) {
const value = getInput(name) || null;
if (required && !value) {
throw new Error(`Missing required input: ${name}`);
}
return value;
}
// Executes the provided shell command and redirects stdout/stderr to the console
async function executeShellCommand(command, workingDirectory = null) {
const originalDirectory = process.cwd();
if (workingDirectory) {
process.chdir(workingDirectory);
}
try {
await exec.exec(command);
} finally {
if (workingDirectory) {
process.chdir(originalDirectory);
}
}
}
function getCurrentOS() {
switch (process.platform) {
case 'darwin':
return 'macos';
case 'win32':
return 'windows';
case 'linux':
return 'linux';
default:
return 'Unknown';
}
}
async function run() {
// Log retry configuration
log(`Retry Configuration - Count: ${RETRY_COUNT}, Delay: ${RETRY_DELAY}ms`);
const release = getActionInput('release', true) === 'false';
const buildScriptName = getActionInput('build-script-name', true);
const skipBuild = getActionInput('skip-build') === 'true';
const electronBuilderArgs = getActionInput('electron-builder-args') || '';
const workingDirectory = getActionInput('working-directory', true);
const githubToken = getActionInput('github-token', true);
const packageManager = getActionInput('package-manager') || 'npm';
const platform = getCurrentOS();
const macosCertificate = getActionInput('macos-certs');
const macosCertificatePassword = getActionInput('macos-certs-password');
const windowsCertificate = getActionInput('windows-certs');
const windowsCertificatePassword = getActionInput('windows-certs-password');
log(`Using package manager: ${packageManager}`);
const packageJsonPath = path.join(workingDirectory, 'package.json');
if (!existsSync(packageJsonPath)) {
setFailed(`package.json not found in ${workingDirectory}`);
return;
}
setEnv('GH_TOKEN', githubToken);
if (platform === 'macos') {
setEnv('CSC_LINK', macosCertificate);
setEnv('CSC_KEY_PASSWORD', macosCertificatePassword);
} else if (platform === 'windows') {
setEnv('CSC_LINK', windowsCertificate);
setEnv('CSC_KEY_PASSWORD', windowsCertificatePassword);
}
setEnv('ADBLOCK', true);
setEnv('NODE_AUTH_TOKEN', githubToken);
// Install dependencies with retry
const installCmd =
packageManager === 'pnpm' ? 'pnpm install --frozen-lockfile --prefer-offline' : 'npm ci';
await withRetry(async () => {
await executeShellCommand(installCmd, workingDirectory);
}, 'Dependencies installation');
if (!skipBuild) {
// Build script with retry
const buildCmd =
packageManager === 'pnpm'
? `pnpm run ${buildScriptName}`
: `npm run ${buildScriptName} --if-present`;
await withRetry(async () => {
await executeShellCommand(buildCmd, workingDirectory);
}, 'Build script execution');
} else {
log('Skipping build script because `skip-build` option is set');
}
// Electron builder with retry
const ebCmd =
packageManager === 'pnpm'
? `pnpm exec electron-builder --${platform} ${release ? '--publish always' : ''} ${electronBuilderArgs}`
: `npx --no-install electron-builder --${platform} ${release ? '--publish always' : ''} ${electronBuilderArgs}`;
await withRetry(async () => {
await executeShellCommand(ebCmd, workingDirectory);
}, 'Electron builder execution');
log('Electron application built and signed successfully');
}
run().catch((error) => setFailed(`Action failed with error: ${error}`));