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 1f164c9 commit 9d0aa91
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 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.

15 changes: 8 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 Expand Up @@ -55673,6 +55673,7 @@ async function run() {
// when https://github.com/actions/toolkit/pull/725 is released
release: (core.getInput('release') === 'true'),
});

await pushMetadata(metadataBucket, data);
core.endGroup();

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}`,
);
});
});
1 change: 1 addition & 0 deletions push-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async function run() {
// when https://github.com/actions/toolkit/pull/725 is released
release: (core.getInput('release') === 'true'),
});

await pushMetadata(metadataBucket, data);
core.endGroup();

Expand Down
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
4 changes: 1 addition & 3 deletions push-image/push.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,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 9d0aa91

Please sign in to comment.