Skip to content

Commit 163d33a

Browse files
authored
Merge pull request #412 from crazy-max/docker-disable-dct
disable DCT for docker commands
2 parents d283be9 + d36bef4 commit 163d33a

File tree

5 files changed

+148
-35
lines changed

5 files changed

+148
-35
lines changed

__tests__/docker/docker.test.ts

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import osm = require('os');
2222
import * as rimraf from 'rimraf';
2323

2424
import {Docker} from '../../src/docker/docker';
25-
import {Exec} from '../../src/exec';
2625

2726
import {ConfigFile} from '../../src/types/docker/docker';
2827

@@ -105,48 +104,132 @@ describe('isAvailable', () => {
105104
});
106105
});
107106

107+
describe('exec', () => {
108+
it('returns docker version', async () => {
109+
const execSpy = jest.spyOn(Docker, 'exec');
110+
await Docker.exec(['version'], {
111+
ignoreReturnCode: true,
112+
silent: true
113+
});
114+
expect(execSpy).toHaveBeenCalledTimes(1);
115+
const callfunc = execSpy.mock.calls[0];
116+
expect(Object.keys(callfunc[1]?.env || {}).length).toBeGreaterThan(0);
117+
const env = callfunc[1]?.env;
118+
expect(env).toHaveProperty('DOCKER_CONTENT_TRUST');
119+
expect(env?.DOCKER_CONTENT_TRUST).toBe('false');
120+
if (callfunc[1]?.env) {
121+
// already checked env
122+
callfunc[1].env = undefined;
123+
}
124+
expect(callfunc).toEqual([
125+
['version'],
126+
{
127+
ignoreReturnCode: true,
128+
silent: true
129+
}
130+
]);
131+
});
132+
});
133+
134+
describe('getExecOutput', () => {
135+
it('returns docker version', async () => {
136+
const execSpy = jest.spyOn(Docker, 'getExecOutput');
137+
await Docker.getExecOutput(['version'], {
138+
ignoreReturnCode: true,
139+
silent: true
140+
});
141+
expect(execSpy).toHaveBeenCalledTimes(1);
142+
const callfunc = execSpy.mock.calls[0];
143+
expect(Object.keys(callfunc[1]?.env || {}).length).toBeGreaterThan(0);
144+
const env = callfunc[1]?.env;
145+
expect(env).toHaveProperty('DOCKER_CONTENT_TRUST');
146+
expect(env?.DOCKER_CONTENT_TRUST).toBe('false');
147+
if (callfunc[1]?.env) {
148+
// already checked env
149+
callfunc[1].env = undefined;
150+
}
151+
expect(callfunc).toEqual([
152+
['version'],
153+
{
154+
ignoreReturnCode: true,
155+
silent: true
156+
}
157+
]);
158+
});
159+
});
160+
108161
describe('context', () => {
109162
it('call docker context show', async () => {
110-
const execSpy = jest.spyOn(Exec, 'getExecOutput');
163+
const execSpy = jest.spyOn(Docker, 'getExecOutput');
111164
await Docker.context().catch(() => {
112165
// noop
113166
});
114-
expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format', '{{.Name}}'], {
115-
ignoreReturnCode: true,
116-
silent: true
117-
});
167+
expect(execSpy).toHaveBeenCalledTimes(1);
168+
const callfunc = execSpy.mock.calls[0];
169+
if (callfunc && callfunc[1]) {
170+
// we don't want to check env opt
171+
callfunc[1].env = undefined;
172+
}
173+
expect(callfunc).toEqual([
174+
['context', 'inspect', '--format', '{{.Name}}'],
175+
{
176+
ignoreReturnCode: true,
177+
silent: true
178+
}
179+
]);
118180
});
119181
});
120182

121183
describe('contextInspect', () => {
122184
it('call docker context inspect', async () => {
123-
const execSpy = jest.spyOn(Exec, 'getExecOutput');
185+
const execSpy = jest.spyOn(Docker, 'getExecOutput');
124186
await Docker.contextInspect('foo').catch(() => {
125187
// noop
126188
});
127-
expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format=json', 'foo'], {
128-
ignoreReturnCode: true,
129-
silent: true
130-
});
189+
expect(execSpy).toHaveBeenCalledTimes(1);
190+
const callfunc = execSpy.mock.calls[0];
191+
if (callfunc && callfunc[1]) {
192+
// we don't want to check env opt
193+
callfunc[1].env = undefined;
194+
}
195+
expect(callfunc).toEqual([
196+
['context', 'inspect', '--format=json', 'foo'],
197+
{
198+
ignoreReturnCode: true,
199+
silent: true
200+
}
201+
]);
131202
});
132203
});
133204

134205
describe('printVersion', () => {
135206
it('call docker version', async () => {
136-
const execSpy = jest.spyOn(Exec, 'exec');
207+
const execSpy = jest.spyOn(Docker, 'exec');
137208
await Docker.printVersion().catch(() => {
138209
// noop
139210
});
140-
expect(execSpy).toHaveBeenCalledWith(`docker`, ['version']);
211+
expect(execSpy).toHaveBeenCalledTimes(1);
212+
const callfunc = execSpy.mock.calls[0];
213+
if (callfunc && callfunc[1]) {
214+
// we don't want to check env opt
215+
callfunc[1].env = undefined;
216+
}
217+
expect(callfunc).toEqual([['version']]);
141218
});
142219
});
143220

144221
describe('printInfo', () => {
145222
it('call docker info', async () => {
146-
const execSpy = jest.spyOn(Exec, 'exec');
223+
const execSpy = jest.spyOn(Docker, 'exec');
147224
await Docker.printInfo().catch(() => {
148225
// noop
149226
});
150-
expect(execSpy).toHaveBeenCalledWith(`docker`, ['info']);
227+
expect(execSpy).toHaveBeenCalledTimes(1);
228+
const callfunc = execSpy.mock.calls[0];
229+
if (callfunc && callfunc[1]) {
230+
// we don't want to check env opt
231+
callfunc[1].env = undefined;
232+
}
233+
expect(callfunc).toEqual([['info']]);
151234
});
152235
});

src/buildkit/buildkit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import * as semver from 'semver';
1919

2020
import {Buildx} from '../buildx/buildx';
2121
import {Builder} from '../buildx/builder';
22+
import {Docker} from '../docker/docker';
2223
import {Config} from './config';
23-
import {Exec} from '../exec';
2424

2525
import {BuilderInfo, NodeInfo} from '../types/buildx/builder';
2626

@@ -51,13 +51,13 @@ export class BuildKit {
5151

5252
private async getVersionWithinImage(nodeName: string): Promise<string> {
5353
core.debug(`BuildKit.getVersionWithinImage nodeName: ${nodeName}`);
54-
return Exec.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], {
54+
return Docker.getExecOutput(['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], {
5555
ignoreReturnCode: true,
5656
silent: true
5757
}).then(bkitimage => {
5858
if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) {
5959
core.debug(`BuildKit.getVersionWithinImage image: ${bkitimage.stdout.trim()}`);
60-
return Exec.getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout.trim(), '--version'], {
60+
return Docker.getExecOutput(['run', '--rm', bkitimage.stdout.trim(), '--version'], {
6161
ignoreReturnCode: true,
6262
silent: true
6363
}).then(bkitversion => {

src/buildx/history.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ export class History {
137137
]
138138
core.info(`[command]docker ${dockerRunArgs.join(' ')}`);
139139
dockerRunProc = spawn('docker', dockerRunArgs, {
140-
stdio: ['pipe', 'pipe', 'inherit']
140+
stdio: ['pipe', 'pipe', 'inherit'],
141+
env: {
142+
...process.env,
143+
DOCKER_CONTENT_TRUST: 'false'
144+
}
141145
});
142146
fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin);
143147
dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath));

src/docker/docker.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import fs from 'fs';
1818
import os from 'os';
1919
import path from 'path';
2020
import * as core from '@actions/core';
21+
import {ExecOptions, ExecOutput} from '@actions/exec';
2122
import * as io from '@actions/io';
2223

2324
import {Context} from '../context';
@@ -53,12 +54,36 @@ export class Docker {
5354
});
5455
}
5556

57+
public static async exec(args?: string[], options?: ExecOptions): Promise<number> {
58+
return Exec.exec('docker', args, Docker.execOptions(options));
59+
}
60+
61+
public static async getExecOutput(args?: string[], options?: ExecOptions): Promise<ExecOutput> {
62+
return Exec.getExecOutput('docker', args, Docker.execOptions(options));
63+
}
64+
65+
private static execOptions(options?: ExecOptions): ExecOptions {
66+
if (!options) {
67+
options = {};
68+
}
69+
if (!options.env) {
70+
options.env = Object.assign({}, process.env, {
71+
DOCKER_CONTENT_TRUST: 'false'
72+
}) as {
73+
[key: string]: string;
74+
};
75+
} else {
76+
options.env.DOCKER_CONTENT_TRUST = 'false';
77+
}
78+
return options;
79+
}
80+
5681
public static async context(name?: string): Promise<string> {
5782
const args = ['context', 'inspect', '--format', '{{.Name}}'];
5883
if (name) {
5984
args.push(name);
6085
}
61-
return await Exec.getExecOutput(`docker`, args, {
86+
return await Docker.getExecOutput(args, {
6287
ignoreReturnCode: true,
6388
silent: true
6489
}).then(res => {
@@ -74,7 +99,7 @@ export class Docker {
7499
if (name) {
75100
args.push(name);
76101
}
77-
return await Exec.getExecOutput(`docker`, args, {
102+
return await Docker.getExecOutput(args, {
78103
ignoreReturnCode: true,
79104
silent: true
80105
}).then(res => {
@@ -86,11 +111,11 @@ export class Docker {
86111
}
87112

88113
public static async printVersion(): Promise<void> {
89-
await Exec.exec('docker', ['version']);
114+
await Docker.exec(['version']);
90115
}
91116

92117
public static async printInfo(): Promise<void> {
93-
await Exec.exec('docker', ['info']);
118+
await Docker.exec(['info']);
94119
}
95120

96121
public static parseRepoTag(image: string): {repository: string; tag: string} {
@@ -138,7 +163,7 @@ export class Docker {
138163
cacheFoundPath = await imageCache.find();
139164
if (cacheFoundPath) {
140165
core.info(`Image found from cache in ${cacheFoundPath}`);
141-
await Exec.getExecOutput(`docker`, ['load', '-i', cacheFoundPath], {
166+
await Docker.getExecOutput(['load', '-i', cacheFoundPath], {
142167
ignoreReturnCode: true
143168
}).then(res => {
144169
if (res.stderr.length > 0 && res.exitCode != 0) {
@@ -149,7 +174,7 @@ export class Docker {
149174
}
150175

151176
let pulled = true;
152-
await Exec.getExecOutput(`docker`, ['pull', image], {
177+
await Docker.getExecOutput(['pull', image], {
153178
ignoreReturnCode: true
154179
}).then(res => {
155180
if (res.stderr.length > 0 && res.exitCode != 0) {
@@ -165,7 +190,7 @@ export class Docker {
165190

166191
if (cache && pulled) {
167192
const imageTarPath = path.join(Context.tmpDir(), `${Util.hash(image)}.tar`);
168-
await Exec.getExecOutput(`docker`, ['save', '-o', imageTarPath, image], {
193+
await Docker.getExecOutput(['save', '-o', imageTarPath, image], {
169194
ignoreReturnCode: true
170195
}).then(async res => {
171196
if (res.stderr.length > 0 && res.exitCode != 0) {

src/docker/install.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as io from '@actions/io';
2828
import * as tc from '@actions/tool-cache';
2929

3030
import {Context} from '../context';
31+
import {Docker} from './docker';
3132
import {Exec} from '../exec';
3233
import {Util} from '../util';
3334
import {limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1} from './assets';
@@ -219,8 +220,8 @@ export class Install {
219220
});
220221

221222
await core.group('Create Docker context', async () => {
222-
await Exec.exec('docker', ['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
223-
await Exec.exec('docker', ['context', 'use', this.contextName]);
223+
await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
224+
await Docker.exec(['context', 'use', this.contextName]);
224225
});
225226

226227
return dockerHost;
@@ -309,8 +310,8 @@ EOF`,
309310
});
310311

311312
await core.group('Create Docker context', async () => {
312-
await Exec.exec('docker', ['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
313-
await Exec.exec('docker', ['context', 'use', this.contextName]);
313+
await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
314+
await Docker.exec(['context', 'use', this.contextName]);
314315
});
315316

316317
return dockerHost;
@@ -352,8 +353,8 @@ EOF`,
352353
});
353354

354355
await core.group('Create Docker context', async () => {
355-
await Exec.exec('docker', ['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
356-
await Exec.exec('docker', ['context', 'use', this.contextName]);
356+
await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
357+
await Docker.exec(['context', 'use', this.contextName]);
357358
});
358359

359360
return dockerHost;
@@ -395,7 +396,7 @@ EOF`,
395396
await Exec.exec('limactl', ['delete', '--tty=false', this.limaInstanceName, '--force']);
396397
});
397398
await core.group('Removing Docker context', async () => {
398-
await Exec.exec('docker', ['context', 'rm', '-f', this.contextName]);
399+
await Docker.exec(['context', 'rm', '-f', this.contextName]);
399400
});
400401
await core.group(`Cleaning up runDir`, async () => {
401402
await Exec.exec('sudo', ['rm', '-rf', this.runDir]);
@@ -411,7 +412,7 @@ EOF`,
411412
await Util.sleep(5);
412413
});
413414
await core.group('Removing Docker context', async () => {
414-
await Exec.exec('docker', ['context', 'rm', '-f', this.contextName]);
415+
await Docker.exec(['context', 'rm', '-f', this.contextName]);
415416
});
416417
await core.group(`Cleaning up runDir`, async () => {
417418
await Exec.exec('sudo', ['rm', '-rf', this.runDir], {
@@ -427,7 +428,7 @@ EOF`,
427428
await Exec.exec(logCmd.command, logCmd.args);
428429
});
429430
await core.group('Removing Docker context', async () => {
430-
await Exec.exec('docker', ['context', 'rm', '-f', this.contextName]);
431+
await Docker.exec(['context', 'rm', '-f', this.contextName]);
431432
});
432433
}
433434

0 commit comments

Comments
 (0)