Skip to content

Commit

Permalink
add retries for 502
Browse files Browse the repository at this point in the history
  • Loading branch information
fedordikarev committed Oct 29, 2024
1 parent 0689575 commit e5b3ebc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Inputs {
password: string;
ecr: string;
logout: boolean;
attempts: number;
}

export function getInputs(): Inputs {
Expand All @@ -14,6 +15,7 @@ export function getInputs(): Inputs {
username: core.getInput('username'),
password: core.getInput('password'),
ecr: core.getInput('ecr'),
logout: core.getBooleanInput('logout')
logout: core.getBooleanInput('logout'),
attempts: Number.parseInt(core.getInput('attempts'))
};
}
37 changes: 25 additions & 12 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as core from '@actions/core';

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, attempts: number): Promise<void> {
if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) {
await loginECR(registry, username, password);
} else {
await loginStandard(registry, username, password);
await loginStandard(registry, username, password, attempts);
}
}

Expand All @@ -21,7 +21,7 @@ export async function logout(registry: string): Promise<void> {
});
}

export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
export async function loginStandard(registry: string, username: string, password: string, attempts: number): Promise<void> {
if (!username && !password) {
throw new Error('Username and password required');
}
Expand All @@ -41,16 +41,29 @@ export async function loginStandard(registry: string, username: string, password
} else {
core.info(`Logging into Docker Hub...`);
}
await Docker.getExecOutput(loginArgs, {
ignoreReturnCode: true,
silent: true,
input: Buffer.from(password)
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
let attempt: number = 1
let succeeded: boolean = false
for (let attempt = 1; (attempt <= attempts) && (!succeeded); attempt++) {
await Docker.getExecOutput(loginArgs, {
ignoreReturnCode: true,
silent: true,
input: Buffer.from(password)
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
let isRetriable: boolean
isRetriable = res.stderr.endsWith("502 Bad Gateway")
if (!isRetriable || (attempt >= attempts) {
throw new Error(res.stderr.trim());
}
} else {
core.info(`Login Succeeded!`);
succeeded = true;
}
});
if ((attempt < attempts) && !succeeded) {
await new Promise(r => setTimeout(r, 10000))
}
core.info(`Login Succeeded!`);
});
}
}

export async function loginECR(registry: string, username: string, password: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function main(): Promise<void> {
const input: context.Inputs = context.getInputs();
stateHelper.setRegistry(input.registry);
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.attempts);
}

async function post(): Promise<void> {
Expand Down

0 comments on commit e5b3ebc

Please sign in to comment.