Skip to content

Commit

Permalink
feat(push-image): use execFilerSync instead of docker-cli-js to execu…
Browse files Browse the repository at this point in the history
…te docker
  • Loading branch information
josa42 committed Jan 4, 2024
1 parent 8b4daf5 commit 7c8ad1f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 36 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions push-image/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49329,16 +49329,16 @@ exports["default"] = _default;
/***/ 29018:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(75316);

const { execFileSync } = __nccwpck_require__(32081);

function docker(command, ...args) {
const out = execFileSync('docker', [command, ...args], { env: process.env }).toString('utf-8');

/* eslint-disable no-console */
console.log(`::group::> docker ${command} ${args.join(' ')}`);
console.log(out);
console.log('::endgroup::');
/* eslint-enable no-console */
core.group(`::group::> docker ${command} ${args.join(' ')}`, () => {
core.info(out);
});

return out;
}
Expand All @@ -49349,7 +49349,7 @@ function dockerJSON(command, ...args) {
try {
return JSON.parse(out);
} catch (err) {
console.error(`:error::Failed to parse json output for "docker ${command} ${args.join(' ')}"`);
core.error(`Failed to parse json output for "docker ${command} ${args.join(' ')}"`);
throw err;
}
}
Expand Down Expand Up @@ -49414,7 +49414,7 @@ async function buildMetadata(input) {
data.build = metadata.getBuildData(input.event);
data.labels = metadata.getLabels(input.labels);
data.release = input.release;
data.dockerInspect = dockerJSON('inspect', input.repo).object;
data.dockerInspect = dockerJSON('inspect', input.repo);

return data;
}
Expand Down
12 changes: 6 additions & 6 deletions push-image/docker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const core = require('@actions/core');

const { execFileSync } = require('child_process');

function docker(command, ...args) {
const out = execFileSync('docker', [command, ...args], { env: process.env }).toString('utf-8');

/* eslint-disable no-console */
console.log(`::group::> docker ${command} ${args.join(' ')}`);
console.log(out);
console.log('::endgroup::');
/* eslint-enable no-console */
core.group(`::group::> docker ${command} ${args.join(' ')}`, () => {
core.info(out);
});

return out;
}
Expand All @@ -18,7 +18,7 @@ function dockerJSON(command, ...args) {
try {
return JSON.parse(out);
} catch (err) {
console.error(`:error::Failed to parse json output for "docker ${command} ${args.join(' ')}"`);
core.error(`Failed to parse json output for "docker ${command} ${args.join(' ')}"`);
throw err;
}
}
Expand Down
8 changes: 4 additions & 4 deletions push-image/docker.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
process.env.GITHUB_RUN_NUMBER = '2345';

const { execFileSync } = require('child_process');
const { EOL } = require('os');
const { docker, dockerJSON } = require('./docker');

jest.mock('child_process', () => ({
Expand All @@ -13,8 +14,7 @@ beforeEach(() => {
execFileSync.mockReset();
execFileSync.mockReturnValue(Buffer.alloc(0));

jest.spyOn(global.console, 'error').mockImplementation(() => {});
jest.spyOn(global.console, 'log').mockImplementation(() => {});
jest.spyOn(process.stdout, 'write').mockImplementation(() => {});

process.env = { FOO: 'bar' };
});
Expand Down Expand Up @@ -42,8 +42,8 @@ describe('dockerJSON())', () => {
execFileSync.mockReturnValue(Buffer.from('{'));

expect(() => dockerJSON('docker', 'context', 'inspect')).toThrow();
expect(global.console.error).toBeCalledWith(
':error::Failed to parse json output for "docker docker context inspect"',
expect(process.stdout.write).toBeCalledWith(
`::error::Failed to parse json output for "docker docker context inspect"${EOL}`,
);
});
});
3 changes: 1 addition & 2 deletions push-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"docker-cli-js": "^2.8.0"
"@actions/exec": "^1.1.1"
}
}
2 changes: 1 addition & 1 deletion push-image/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function buildMetadata(input) {
data.build = metadata.getBuildData(input.event);
data.labels = metadata.getLabels(input.labels);
data.release = input.release;
data.dockerInspect = dockerJSON('inspect', input.repo).object;
data.dockerInspect = dockerJSON('inspect', input.repo);

return data;
}
Expand Down
13 changes: 1 addition & 12 deletions push-image/push.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ describe('passes corrects args to docker build', () => {
expect(docker.docker.mock.calls[1])
.toEqual(['build', '--tag', 'thing1', '--file', 'dockf1', '--build-arg', 'k1=v1', '--build-arg', 'k2=v2', '--secret', 'id=npmrc,src=.npmrc', '--ssh', 'default=foo', 'path1']);
});
// test('passes process.env', async () => {
// await buildImage({
// repo: 'thing1',
// dockerfile: 'dockf1',
// path: 'path1',
// });
// expect(docker.docker.mock.calls[0][1])
// .toStrictEqual({ env: { FOO: 'bar' } });
// });
});

test('calls tagImage correctly', async () => {
Expand Down Expand Up @@ -110,9 +101,7 @@ describe('getTags', () => {
});

test('builds correct metadata', async () => {
docker.dockerJSON.mockReturnValue({
object: [{ dummy: 'inspect' }],
});
docker.dockerJSON.mockReturnValue([{ dummy: 'inspect' }]);
const data = await buildMetadata({
event: {
pull_request: {
Expand Down

0 comments on commit 7c8ad1f

Please sign in to comment.