Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add moderate way to retry docker login failures #849

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ jobs:
> Google Container Registry. As a fully-managed service with support for both
> container images and non-container artifacts. If you currently use Google
> Container Registry, use the information [on this page](https://cloud.google.com/artifact-registry/docs/transition/transition-from-gcr)
> to learn about transitioning to Google Artifact Registry.
> to learn about transitioning to Google Artifact Registry.

You can authenticate with workload identity federation or a service account.

Expand Down Expand Up @@ -421,7 +421,7 @@ must be placed in format `<tenancy>/<username>` (in case of federated tenancy us

For password [create an auth token](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#GetanAuthToken).
Save username and token [as a secrets](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets-for-a-repository)
in your GitHub repo.
in your GitHub repo.

```yaml
name: ci
Expand Down Expand Up @@ -500,13 +500,16 @@ jobs:

The following inputs can be used as `step.with` keys:

| Name | Type | Default | Description |
|------------|--------|---------|-------------------------------------------------------------------------------|
| `registry` | String | | Server address of Docker registry. If not set then will default to Docker Hub |
| `username` | String | | Username for authenticating to the Docker registry |
| `password` | String | | Password or personal access token for authenticating the Docker registry |
| `ecr` | String | `auto` | Specifies whether the given registry is ECR (`auto`, `true` or `false`) |
| `logout` | Bool | `true` | Log out from the Docker registry at the end of a job |
| Name | Type | Default | Description |
|-----------------------|--------|---------|-------------------------------------------------------------------------------|
| `registry` | String | | Server address of Docker registry. If not set then will default to Docker Hub |
| `username` | String | | Username for authenticating to the Docker registry |
| `password` | String | | Password or personal access token for authenticating the Docker registry |
| `ecr` | String | `auto` | Specifies whether the given registry is ECR (`auto`, `true` or `false`) |
| `logout` | Bool | `true` | Log out from the Docker registry at the end of a job |
| `http-codes-to-retry` | String | `408,500,502,504` | Comma separated list of HTTP error codes we want to retry |
| `max-attempts` | String | `1` | Overall maximum number of attempts we could make (`1` means no retries) |
| `retry-timeout` | String | `15` | Timeout between retries, in seconds |

## Contributing

Expand Down
46 changes: 46 additions & 0 deletions __tests__/retries_fail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {expect, jest, test} from '@jest/globals';

import {login} from '../src/docker';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';

test('login retries function', async () => {
let stderrStrings: string[] = [];
let callCount: number = -1;

// using spyOn() here isn't enough, as we alter the logic
// so use `jest.fn()` here for the `Docker.getExecOutput`
Docker.getExecOutput = jest.fn(async () => {
callCount++;
console.log(`Mock: ${callCount}, ${stderrStrings}`);
if (callCount >= stderrStrings.length) {
return {
exitCode: 0,
stdout: 'Mock success',
stderr: ''
};
}
return {
exitCode: 1,
stdout: '',
stderr: stderrStrings[callCount % stderrStrings.length]
};
});

const username = 'dbowie';
const password = 'groundcontrol';
const registry = 'https://ghcr.io';

stderrStrings = ['mock error, failed with status: 408 Request Timeout', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
callCount = -1;
await expect(async () => {
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1);
}).rejects.toThrow('mock error, failed with status: 502 Request Timeout');
expect(Docker.getExecOutput).toHaveBeenCalledTimes(2);

stderrStrings = ['not matching error', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
callCount = -1;
await expect(async () => {
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1);
}).rejects.toThrow('not matching error');
expect(Docker.getExecOutput).toHaveBeenCalledTimes(2 + 1);
});
42 changes: 42 additions & 0 deletions __tests__/retries_success.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {expect, jest, test} from '@jest/globals';

import {login} from '../src/docker';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';

test('login retries success function', async () => {
let stderrStrings: string[] = [];
let callCount: number = -1;

// using spyOn() here isn't enough, as we alter the logic
// so use `jest.fn()` here for the `Docker.getExecOutput`
Docker.getExecOutput = jest.fn(async () => {
callCount++;
console.log(`Mock: ${callCount}, ${stderrStrings}`);
if (callCount >= stderrStrings.length) {
return {
exitCode: 0,
stdout: 'Mock success',
stderr: ''
};
}
return {
exitCode: 1,
stdout: '',
stderr: stderrStrings[callCount % stderrStrings.length]
};
});

const username = 'dbowie';
const password = 'groundcontrol';
const registry = 'https://ghcr.io';

stderrStrings = [];
callCount = -1;
await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1);
expect(Docker.getExecOutput).toHaveBeenCalledTimes(1);

stderrStrings = ['mock error, failed with status: 408 Request Timeout', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
callCount = -1;
await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1);
expect(Docker.getExecOutput).toHaveBeenCalledTimes(1 + 4);
});
11 changes: 11 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ inputs:
description: 'Log out from the Docker registry at the end of a job'
default: 'true'
required: false
http-codes-to-retry:
description: 'Comma separated list of HTTP error codes we want to retry'
default: '408,500,502,504'
max-attempts:
description: 'Overall maximum number of attempts we will make trying to login'
default: '1'
required: false
retry-timeout:
description: 'Timeout between retries, in seconds'
default: '15'
required: false

runs:
using: 'node20'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface Inputs {
password: string;
ecr: string;
logout: boolean;
httpCodesToRetry: string[];
maxAttempts: number;
retryTimeout: number;
}

export function getInputs(): Inputs {
Expand All @@ -14,6 +17,9 @@ export function getInputs(): Inputs {
username: core.getInput('username'),
password: core.getInput('password'),
ecr: core.getInput('ecr'),
logout: core.getBooleanInput('logout')
logout: core.getBooleanInput('logout'),
httpCodesToRetry: core.getInput('http-codes-to-retry').split(','),
maxAttempts: Number.parseInt(core.getInput('max-attempts')),
retryTimeout: Number.parseInt(core.getInput('retry-timeout'))
};
}
34 changes: 29 additions & 5 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ 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> {
if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) {
await loginECR(registry, username, password);
} else {
await loginStandard(registry, username, password);
export async function login(registry: string, username: string, password: string, ecr: string, httpCodesToRetry: string[], maxAttempts: number, retryTimeout: number): Promise<void> {
let succeeded: boolean = false;
for (let attempt = 1; attempt <= maxAttempts && !succeeded; attempt++) {
try {
if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) {
await loginECR(registry, username, password);
} else {
await loginStandard(registry, username, password);
}
succeeded = true;
} catch (error) {
if (attempt < maxAttempts && isRetriableError(error.message, httpCodesToRetry)) {
core.info(`Attempt ${attempt} out of ${maxAttempts} failed, retrying after ${retryTimeout} seconds`);
await new Promise(r => setTimeout(r, retryTimeout * 1000));
} else {
throw error;
}
}
}
}

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

function isRetriableError(errorMessage: string, httpCodesToRetry: string[]): boolean {
for (const errCode of httpCodesToRetry) {
if (errorMessage.includes('failed with status: ' + errCode)) {
core.info(`Retryable match found in ${errorMessage} for retryable code: ${errCode}`);
return true;
}
}
core.info(`No matches in ${errorMessage} when lookging for retryable codes: ${httpCodesToRetry}`);
return false;
}

export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
if (!username && !password) {
throw new Error('Username and password required');
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.httpCodesToRetry, input.maxAttempts, input.retryTimeout);
}

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