Skip to content

Commit fc4dae4

Browse files
committed
docker: return actual error message when pull fails
Signed-off-by: CrazyMax <[email protected]>
1 parent aa0228d commit fc4dae4

2 files changed

Lines changed: 52 additions & 20 deletions

File tree

__tests__/docker/docker.test.itg.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,34 @@ const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'tr
2323
maybe('pull', () => {
2424
// prettier-ignore
2525
test.each([
26-
'busybox',
27-
'busybox:1.36',
28-
'busybox@sha256:7ae8447f3a7f5bccaa765926f25fc038e425cf1b2be6748727bbea9a13102094'
29-
])(
30-
'pulling %s', async (image) => {
31-
await expect((async () => {
26+
[
27+
'busybox',
28+
undefined,
29+
],
30+
[
31+
'busybox:1.36',
32+
undefined,
33+
],
34+
[
35+
'busybox@sha256:7ae8447f3a7f5bccaa765926f25fc038e425cf1b2be6748727bbea9a13102094',
36+
undefined,
37+
],
38+
[
39+
'doesnotexist:foo',
40+
`pull access denied for doesnotexist`,
41+
],
42+
])('pulling %p', async (image: string, err: string | undefined) => {
43+
try {
3244
await Docker.pull(image, true);
33-
})()).resolves.not.toThrow();
45+
if (err !== undefined) {
46+
throw new Error('Expected an error to be thrown');
47+
}
48+
} catch (e) {
49+
if (err === undefined) {
50+
throw new Error(`Expected no error, but got: ${e.message}`);
51+
}
52+
// eslint-disable-next-line jest/no-conditional-expect
53+
expect(e.message).toContain(err);
54+
}
3455
}, 600000);
3556
});

src/docker/docker.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,43 @@ export class Docker {
122122
cacheFoundPath = await imageCache.find();
123123
if (cacheFoundPath) {
124124
core.info(`Image found from cache in ${cacheFoundPath}`);
125-
await Exec.getExecOutput(`docker`, ['load', '-i', cacheFoundPath]).catch(e => {
126-
core.warning(`Failed to load image from cache: ${e}`);
125+
await Exec.getExecOutput(`docker`, ['load', '-i', cacheFoundPath], {
126+
ignoreReturnCode: true
127+
}).then(res => {
128+
if (res.stderr.length > 0 && res.exitCode != 0) {
129+
core.warning(`Failed to load image from cache: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
130+
}
127131
});
128132
}
129133
}
130134

131135
let pulled = true;
132-
await Exec.getExecOutput(`docker`, ['pull', image]).catch(e => {
136+
await Exec.getExecOutput(`docker`, ['pull', image], {
137+
ignoreReturnCode: true
138+
}).then(res => {
133139
pulled = false;
134-
if (cacheFoundPath) {
135-
core.warning(`Failed to pull image, using one from cache: ${e}`);
136-
} else {
137-
throw new Error(e);
140+
if (res.stderr.length > 0 && res.exitCode != 0) {
141+
const err = res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error';
142+
if (cacheFoundPath) {
143+
core.warning(`Failed to pull image, using one from cache: ${err}`);
144+
} else {
145+
throw new Error(err);
146+
}
138147
}
139148
});
140149

141150
if (cache && pulled) {
142151
const imageTarPath = path.join(Context.tmpDir(), `${Util.hash(image)}.tar`);
143-
await Exec.getExecOutput(`docker`, ['save', '-o', imageTarPath, image])
144-
.then(async () => {
152+
await Exec.getExecOutput(`docker`, ['save', '-o', imageTarPath, image], {
153+
ignoreReturnCode: true
154+
}).then(async res => {
155+
if (res.stderr.length > 0 && res.exitCode != 0) {
156+
core.warning(`Failed to save image: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
157+
} else {
145158
const cachePath = await imageCache.save(imageTarPath);
146159
core.info(`Image cached to ${cachePath}`);
147-
})
148-
.catch(e => {
149-
core.warning(`Failed to save image: ${e}`);
150-
});
160+
}
161+
});
151162
}
152163
}
153164
}

0 commit comments

Comments
 (0)