add retries logic

Signed-off-by: Fedor Dikarev <fedor.dikarev@gmail.com>
This commit is contained in:
Fedor Dikarev 2025-01-23 02:47:49 +01:00
parent 2bc89718bc
commit b909aa9ffe
2 changed files with 28 additions and 6 deletions

View file

@ -3,12 +3,24 @@ import * as core from '@actions/core';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker'; import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
export async function login(registry: string, username: string, password: string, ecr: string): Promise<void> { export async function login(registry: string, username: string, password: string, ecr: string, http_errors_to_retry: string[], max_attempts: number, retry_timeout: number): Promise<void> {
let succeeded: boolean = false;
for (let attempt = 1; (attempt <= max_attempts) && (!succeeded); attempt++) {
try {
if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) { if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) {
await loginECR(registry, username, password); await loginECR(registry, username, password);
} else { } else {
await loginStandard(registry, username, password); await loginStandard(registry, username, password);
} }
} catch (error) {
if ((attempt < max_attempts) && (isRetriableError(error, http_errors_to_retry))) {
core.info("Attempt ", attempt, "out of ", max_attempts, "failed, retrying after ", retry_timeout, "seconds");
await new Promise(r => setTimeout(r, retry_timeout * 1000));
} else {
throw new Error(error);
}
}
}
} }
export async function logout(registry: string): Promise<void> { export async function logout(registry: string): Promise<void> {
@ -21,6 +33,16 @@ export async function logout(registry: string): Promise<void> {
}); });
} }
function isRetriableError(stderr: string, http_errors_to_retry: string[]): boolean {
const trimmedError = stderr.trim();
for (const err_code in http_errors_to_retry) {
if (trimmedError.includes("failed with status: " + err_code)) {
return true;
}
}
return false;
}
export async function loginStandard(registry: string, username: string, password: string): Promise<void> { export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
if (!username && !password) { if (!username && !password) {
throw new Error('Username and password required'); throw new Error('Username and password required');

View file

@ -8,7 +8,7 @@ export async function main(): Promise<void> {
const input: context.Inputs = context.getInputs(); const input: context.Inputs = context.getInputs();
stateHelper.setRegistry(input.registry); stateHelper.setRegistry(input.registry);
stateHelper.setLogout(input.logout); stateHelper.setLogout(input.logout);
await docker.login(input.registry, input.username, input.password, input.ecr); await docker.login(input.registry, input.username, input.password, input.ecr, input.http_errors_to_retry, input.max_attempts, input.retry_timeout);
} }
async function post(): Promise<void> { async function post(): Promise<void> {