From ef945efb4486eb0dd52f704349aa92e0fd7fbf33 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 19:12:55 -0800 Subject: [PATCH 01/14] add support for docker build args --- dist/index.js | 17 ++++++++++++++--- src/docker-build-push.js | 3 ++- src/docker.js | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7f2a8c1..47f8e58 100644 --- a/dist/index.js +++ b/dist/index.js @@ -537,6 +537,16 @@ const isMasterBranch = ref => ref && ref === 'refs/heads/master'; const isNotMasterBranch = ref => ref && ref.includes('refs/heads/') && ref !== 'refs/heads/master'; +const createBuildCommand = (dockerfile, imageName, buildArgs) => { + let buildCommandPrefix = `docker build -f ${dockerfile} -t ${imageName}`; + if (buildArgs) { + const argsSuffix = buildArgs.map(arg => `--build-arg ${arg}`).join(' '); + buildCommandPrefix = `${buildCommandPrefix} ${argsSuffix}`; + } + + return `${buildCommandPrefix} .`; +}; + const createTag = () => { core.info('Creating Docker image tag...'); const { sha } = context; @@ -566,7 +576,7 @@ const createTag = () => { return dockerTag; }; -const build = imageName => { +const build = (imageName, buildArgs) => { const dockerfile = core.getInput('dockerfile'); if (!fs.existsSync(dockerfile)) { @@ -574,7 +584,7 @@ const build = imageName => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(`docker build -f ${dockerfile} -t ${imageName} .`); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; const isEcr = registry => registry && registry.includes('amazonaws'); @@ -1576,11 +1586,12 @@ const run = () => { const image = core.getInput('image', { required: true }); const registry = core.getInput('registry', { required: true }); const tag = core.getInput('tag') || docker.createTag(); + const buildArgs = core.getInput('buildArgs'); const imageName = `${registry}/${image}:${tag}`; docker.login(); - docker.build(imageName); + docker.build(imageName, buildArgs); docker.push(imageName); core.setOutput('imageFullName', imageName); diff --git a/src/docker-build-push.js b/src/docker-build-push.js index ad174f5..4ee49a6 100644 --- a/src/docker-build-push.js +++ b/src/docker-build-push.js @@ -7,11 +7,12 @@ const run = () => { const image = core.getInput('image', { required: true }); const registry = core.getInput('registry', { required: true }); const tag = core.getInput('tag') || docker.createTag(); + const buildArgs = core.getInput('buildArgs'); const imageName = `${registry}/${image}:${tag}`; docker.login(); - docker.build(imageName); + docker.build(imageName, buildArgs); docker.push(imageName); core.setOutput('imageFullName', imageName); diff --git a/src/docker.js b/src/docker.js index 44e69c2..cf62182 100644 --- a/src/docker.js +++ b/src/docker.js @@ -9,6 +9,16 @@ const isMasterBranch = ref => ref && ref === 'refs/heads/master'; const isNotMasterBranch = ref => ref && ref.includes('refs/heads/') && ref !== 'refs/heads/master'; +const createBuildCommand = (dockerfile, imageName, buildArgs) => { + let buildCommandPrefix = `docker build -f ${dockerfile} -t ${imageName}`; + if (buildArgs) { + const argsSuffix = buildArgs.map(arg => `--build-arg ${arg}`).join(' '); + buildCommandPrefix = `${buildCommandPrefix} ${argsSuffix}`; + } + + return `${buildCommandPrefix} .`; +}; + const createTag = () => { core.info('Creating Docker image tag...'); const { sha } = context; @@ -38,7 +48,7 @@ const createTag = () => { return dockerTag; }; -const build = imageName => { +const build = (imageName, buildArgs) => { const dockerfile = core.getInput('dockerfile'); if (!fs.existsSync(dockerfile)) { @@ -46,7 +56,7 @@ const build = imageName => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(`docker build -f ${dockerfile} -t ${imageName} .`); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; const isEcr = registry => registry && registry.includes('amazonaws'); From 1538283b8d9b4dd5dcfb8bacf98dd6fb21eda288 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 19:13:27 -0800 Subject: [PATCH 02/14] add test for buildArgs --- tests/docker-build-push.test.js | 51 ++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/tests/docker-build-push.test.js b/tests/docker-build-push.test.js index e188516..4c0d905 100644 --- a/tests/docker-build-push.test.js +++ b/tests/docker-build-push.test.js @@ -1,40 +1,77 @@ jest.mock('@actions/core'); +jest.mock('child_process'); const core = require('@actions/core'); +const cp = require('child_process'); const docker = require('../src/docker'); const run = require('../src/docker-build-push'); beforeAll(() => { - docker.build = jest.fn(); docker.push = jest.fn(); }); +const mockInputs = (image, registry, tag, buildArgs, dockerfile) => { + core.getInput = jest + .fn() + .mockReturnValueOnce(image) + .mockReturnValueOnce(registry) + .mockReturnValueOnce(tag) + .mockReturnValueOnce(buildArgs) + .mockReturnValueOnce(dockerfile); +}; + describe('Create & push Docker image', () => { test('Valid Docker inputs', () => { const image = 'gcp-project/image'; const registry = 'gcr.io'; const tag = 'dev-1234567'; + const buildArgs = null; + const dockerfile = 'Dockerfile'; + + docker.login = jest.fn(); + docker.createTag = jest.fn().mockReturnValueOnce(tag); + mockInputs(image, registry, null, buildArgs, dockerfile); + core.setOutput = jest.fn().mockReturnValueOnce('imageFullName', `${registry}/${image}:${tag}`); + cp.execSync = jest.fn(); + + run(); + + expect(docker.createTag).toHaveBeenCalledTimes(1); + expect(core.getInput).toHaveBeenCalledTimes(5); + expect(core.setOutput).toHaveBeenCalledWith('imageFullName', `${registry}/${image}:${tag}`); + expect(cp.execSync).toHaveBeenCalledWith(`docker build -f ${dockerfile} -t ${registry}/${image}:${tag} .`); + }); +}); + +describe('Create & push Docker image with build args', () => { + test('Valid Docker inputs with build args', () => { + const image = 'gcp-project/image'; + const registry = 'gcr.io'; + const tag = 'latest'; + const buildArgs = ['VERSION=1.1.1', 'BUILD_DATE=2020-01-14']; + const dockerfile = 'Dockerfile.custom'; docker.login = jest.fn(); docker.createTag = jest.fn().mockReturnValueOnce(tag); - core.getInput = jest - .fn() - .mockReturnValueOnce(image) - .mockReturnValueOnce(registry) - .mockReturnValueOnce(null); + mockInputs(image, registry, null, buildArgs, dockerfile); core.setOutput = jest.fn().mockReturnValueOnce('imageFullName', `${registry}/${image}:${tag}`); + cp.execSync = jest.fn(); run(); expect(docker.createTag).toHaveBeenCalledTimes(1); - expect(core.getInput).toHaveBeenCalledTimes(3); + expect(core.getInput).toHaveBeenCalledTimes(5); expect(core.setOutput).toHaveBeenCalledWith('imageFullName', `${registry}/${image}:${tag}`); + expect(cp.execSync).toHaveBeenCalledWith( + `docker build -f ${dockerfile} -t ${registry}/${image}:${tag} --build-arg VERSION=1.1.1 --build-arg BUILD_DATE=2020-01-14 .` + ); }); }); describe('Create Docker image causing an error', () => { test('Docker login error', () => { docker.createTag = jest.fn().mockRejectedValue('some-tag'); + docker.build = jest.fn(); const error = 'Error: Cannot perform an interactive login from a non TTY device'; docker.login = jest.fn().mockImplementation(() => { throw new Error(error); From 0c8a76689d9bc039292866f45f4e7c519a6d7ea0 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 19:13:42 -0800 Subject: [PATCH 03/14] add buildArgs paramater --- action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/action.yml b/action.yml index 46ae988..6b38ee5 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,9 @@ inputs: description: 'Location of Dockerfile, if not Dockerfile in root directory' required: false default: 'Dockerfile' + buildArgs: + description: 'Docker build arguments in format KEY=VALUE,KEY=VALUE' + required: false username: description: 'Docker registry username' required: false From 3507994f24fd61dfc8e5e7e53b96f3759f4621b4 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 20:51:39 -0800 Subject: [PATCH 04/14] handle build args string to array conversion --- dist/index.js | 12 +++++++++++- src/docker-build-push.js | 12 +++++++++++- tests/docker-build-push.test.js | 4 ++-- tests/docker.test.js | 13 +++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 47f8e58..abea991 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1580,13 +1580,23 @@ module.exports = require("child_process"); const core = __webpack_require__(470); const docker = __webpack_require__(95); +// Convert buildArgs from String to Array, as GH Actions currently does not support Arrays +const processBuildArgsInput = buildArgsInput => { + let buildArgs = null; + if (buildArgsInput) { + buildArgs = buildArgsInput.split(','); + } + + return buildArgs; +}; + const run = () => { try { // Get GitHub Action inputs const image = core.getInput('image', { required: true }); const registry = core.getInput('registry', { required: true }); const tag = core.getInput('tag') || docker.createTag(); - const buildArgs = core.getInput('buildArgs'); + const buildArgs = processBuildArgsInput(core.getInput('buildArgs')); const imageName = `${registry}/${image}:${tag}`; diff --git a/src/docker-build-push.js b/src/docker-build-push.js index 4ee49a6..cbb2156 100644 --- a/src/docker-build-push.js +++ b/src/docker-build-push.js @@ -1,13 +1,23 @@ const core = require('@actions/core'); const docker = require('./docker'); +// Convert buildArgs from String to Array, as GH Actions currently does not support Arrays +const processBuildArgsInput = buildArgsInput => { + let buildArgs = null; + if (buildArgsInput) { + buildArgs = buildArgsInput.split(','); + } + + return buildArgs; +}; + const run = () => { try { // Get GitHub Action inputs const image = core.getInput('image', { required: true }); const registry = core.getInput('registry', { required: true }); const tag = core.getInput('tag') || docker.createTag(); - const buildArgs = core.getInput('buildArgs'); + const buildArgs = processBuildArgsInput(core.getInput('buildArgs')); const imageName = `${registry}/${image}:${tag}`; diff --git a/tests/docker-build-push.test.js b/tests/docker-build-push.test.js index 4c0d905..0ebea26 100644 --- a/tests/docker-build-push.test.js +++ b/tests/docker-build-push.test.js @@ -25,7 +25,7 @@ describe('Create & push Docker image', () => { const image = 'gcp-project/image'; const registry = 'gcr.io'; const tag = 'dev-1234567'; - const buildArgs = null; + const buildArgs = ''; const dockerfile = 'Dockerfile'; docker.login = jest.fn(); @@ -48,7 +48,7 @@ describe('Create & push Docker image with build args', () => { const image = 'gcp-project/image'; const registry = 'gcr.io'; const tag = 'latest'; - const buildArgs = ['VERSION=1.1.1', 'BUILD_DATE=2020-01-14']; + const buildArgs = 'VERSION=1.1.1,BUILD_DATE=2020-01-14'; const dockerfile = 'Dockerfile.custom'; docker.login = jest.fn(); diff --git a/tests/docker.test.js b/tests/docker.test.js index 7808907..82489de 100644 --- a/tests/docker.test.js +++ b/tests/docker.test.js @@ -103,6 +103,19 @@ describe('core and cp methods', () => { expect(fs.existsSync).toHaveBeenCalledWith('Dockerfile'); expect(cp.execSync).toHaveBeenCalledWith(`docker build -f Dockerfile -t ${image} .`); }); + + test('Build with build args', () => { + core.getInput.mockReturnValue('Dockerfile'); + fs.existsSync.mockReturnValueOnce(true); + const image = 'docker.io/this-project/that-image:latest'; + const buildArgs = ['VERSION=latest', 'BUILD_DATE=2020-01-14']; + + docker.build(image, buildArgs); + expect(fs.existsSync).toHaveBeenCalledWith('Dockerfile'); + expect(cp.execSync).toHaveBeenCalledWith( + `docker build -f Dockerfile -t ${image} --build-arg VERSION=latest --build-arg BUILD_DATE=2020-01-14 .` + ); + }); }); describe('Registry login', () => { From 74eb84b11831109efbc2d9e502fee7e2aa939138 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 20:53:41 -0800 Subject: [PATCH 05/14] add build args info --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 61a837f..f9f79b1 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ steps: | tag | Docker image tag (see [Tagging the image with GitOps](#tagging-the-image-using-gitops)) | No | | registry | Docker registry host | Yes | | dockerfile | Location of Dockerfile (defaults to `Dockerfile`) | No | +| buildArgs | Docker build arguments in format `KEY=VALUE,KEY=VALUE` | No | | username | Docker registry username | No | | password | Docker registry password or token | No | From 636d208d845dfa2723675685be5ac8808352836b Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 20:53:55 -0800 Subject: [PATCH 06/14] change to valid color --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 6b38ee5..c0ba737 100644 --- a/action.yml +++ b/action.yml @@ -31,4 +31,4 @@ runs: main: 'dist/index.js' branding: icon: 'anchor' - color: 'black' + color: 'blue' From 6e628da255d105c160cf27bcc1b14fc31008a937 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 20:57:54 -0800 Subject: [PATCH 07/14] add author field --- action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/action.yml b/action.yml index c0ba737..f653be6 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,6 @@ name: 'Docker Build & Push' description: 'Builds a Docker image and pushes to a private registry' +author: 'Sean Smith' inputs: image: description: 'Name of the Docker image' From 82d0588c71eef2425df80186680fd0412f0b168f Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 21:13:07 -0800 Subject: [PATCH 08/14] debug build command --- dist/index.js | 1 + src/docker.js | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index abea991..37317c8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -584,6 +584,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); + core.info(`Build command: ${createBuildCommand(dockerfile, imageName, buildArgs)}`); cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; diff --git a/src/docker.js b/src/docker.js index cf62182..f8bc7d3 100644 --- a/src/docker.js +++ b/src/docker.js @@ -56,6 +56,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); + core.info(`Build command: ${createBuildCommand(dockerfile, imageName, buildArgs)}`); cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; From a9d50317e29d14bf8468b95becdcc5d8d2194cf9 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 21:14:58 -0800 Subject: [PATCH 09/14] remove debug --- dist/index.js | 1 - src/docker.js | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 37317c8..abea991 100644 --- a/dist/index.js +++ b/dist/index.js @@ -584,7 +584,6 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - core.info(`Build command: ${createBuildCommand(dockerfile, imageName, buildArgs)}`); cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; diff --git a/src/docker.js b/src/docker.js index f8bc7d3..cf62182 100644 --- a/src/docker.js +++ b/src/docker.js @@ -56,7 +56,6 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - core.info(`Build command: ${createBuildCommand(dockerfile, imageName, buildArgs)}`); cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); }; From 061cb22b5fbee24c34c0cbb709ab13e2db3189cd Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 21:15:31 -0800 Subject: [PATCH 10/14] update README --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f9f79b1..5a03966 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,21 @@ Builds a Docker image and pushes it to the private registry of your choosing. -## Requirements +## Basic usage -* [GitHub Actions Beta](https://github.com/features/actions) program participation -* Run [checkout action](https://github.com/actions/checkout) before using this action +* Ensure you run the [checkout action](https://github.com/actions/checkout) before using this action +* Add the following to a workflow `.yml` file in the `/.github` directory of your repo ```yaml steps: - uses: actions/checkout@v1.0 - - uses: mr-smithers-excellent/docker-build-push@v1.0 + - uses: mr-smithers-excellent/docker-build-push@v1.1 with: image: repo/image tag: latest registry: registry-url.io dockerfile: Dockerfile.ci - username: username + username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} ``` @@ -41,7 +41,7 @@ steps: * Modify sample below and include in your workflow `.github/workflows/*.yml` file ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.0 +uses: mr-smithers-excellent/docker-build-push@v1.1 with: image: docker-hub-repo/image-name registry: docker.io @@ -58,7 +58,7 @@ with: * Ensure you set the username to `_json_key` ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.0 +uses: mr-smithers-excellent/docker-build-push@v1.1 with: image: gcp-project/image-name registry: gcr.io @@ -74,7 +74,7 @@ with: * Modify sample below and include in your workflow `.github/workflows/*.yml` file ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.0 +uses: mr-smithers-excellent/docker-build-push@v1.1 with: image: image-name registry: [aws-account-number].dkr.ecr.[region].amazonaws.com From de9cd26e5410fd54dfb191b3f4b224c8e59a62d4 Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 22:53:30 -0800 Subject: [PATCH 11/14] use spawn instead of execSync --- dist/index.js | 3 ++- src/docker.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index abea991..0272e72 100644 --- a/dist/index.js +++ b/dist/index.js @@ -584,7 +584,8 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); + // cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); + cp.spawn(createBuildCommand(dockerfile, imageName, buildArgs)); }; const isEcr = registry => registry && registry.includes('amazonaws'); diff --git a/src/docker.js b/src/docker.js index cf62182..2e795ed 100644 --- a/src/docker.js +++ b/src/docker.js @@ -56,7 +56,8 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); + // cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); + cp.spawn(createBuildCommand(dockerfile, imageName, buildArgs)); }; const isEcr = registry => registry && registry.includes('amazonaws'); From 68d137363cf358e09811442db48837d9873761fb Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Tue, 14 Jan 2020 23:06:55 -0800 Subject: [PATCH 12/14] increase buffer size --- dist/index.js | 3 +-- src/docker.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0272e72..e34f11a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -584,8 +584,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - // cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); - cp.spawn(createBuildCommand(dockerfile, imageName, buildArgs)); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: 50 * 1024 * 1024 }); }; const isEcr = registry => registry && registry.includes('amazonaws'); diff --git a/src/docker.js b/src/docker.js index 2e795ed..5ce083e 100644 --- a/src/docker.js +++ b/src/docker.js @@ -56,8 +56,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - // cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs)); - cp.spawn(createBuildCommand(dockerfile, imageName, buildArgs)); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: 50 * 1024 * 1024 }); }; const isEcr = registry => registry && registry.includes('amazonaws'); From be26f355c6d5a0b6d3499cbcc2804c3af27306bc Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Thu, 16 Jan 2020 21:38:25 -0800 Subject: [PATCH 13/14] update version to next release --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5a03966..7c72723 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Builds a Docker image and pushes it to the private registry of your choosing. steps: - uses: actions/checkout@v1.0 - - uses: mr-smithers-excellent/docker-build-push@v1.1 + - uses: mr-smithers-excellent/docker-build-push@v2 with: image: repo/image tag: latest @@ -41,7 +41,7 @@ steps: * Modify sample below and include in your workflow `.github/workflows/*.yml` file ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.1 +uses: mr-smithers-excellent/docker-build-push@v2 with: image: docker-hub-repo/image-name registry: docker.io @@ -58,7 +58,7 @@ with: * Ensure you set the username to `_json_key` ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.1 +uses: mr-smithers-excellent/docker-build-push@v2 with: image: gcp-project/image-name registry: gcr.io @@ -74,7 +74,7 @@ with: * Modify sample below and include in your workflow `.github/workflows/*.yml` file ```yaml -uses: mr-smithers-excellent/docker-build-push@v1.1 +uses: mr-smithers-excellent/docker-build-push@v2 with: image: image-name registry: [aws-account-number].dkr.ecr.[region].amazonaws.com From 3d24566c6381a235e0b68a667002b7c4ed52a63f Mon Sep 17 00:00:00 2001 From: mr-smithers-excellent Date: Thu, 16 Jan 2020 22:03:51 -0800 Subject: [PATCH 14/14] externalize maxBufferSize constant --- dist/index.js | 13 ++++++++++++- src/docker.js | 3 ++- src/settings.js | 3 +++ tests/docker-build-push.test.js | 10 ++++++++-- tests/docker.test.js | 10 ++++++++-- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/settings.js diff --git a/dist/index.js b/dist/index.js index e34f11a..9237051 100644 --- a/dist/index.js +++ b/dist/index.js @@ -530,6 +530,7 @@ const cp = __webpack_require__(129); const core = __webpack_require__(470); const fs = __webpack_require__(747); const { context } = __webpack_require__(469); +const maxBufferSize = __webpack_require__(535); const isGitHubTag = ref => ref && ref.includes('refs/tags/'); @@ -584,7 +585,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: 50 * 1024 * 1024 }); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: maxBufferSize }); }; const isEcr = registry => registry && registry.includes('amazonaws'); @@ -7368,6 +7369,16 @@ const factory = __webpack_require__(47); module.exports = factory(); +/***/ }), + +/***/ 535: +/***/ (function(module) { + +const maxBufferSize = 50 * 1024 * 1024; + +module.exports = maxBufferSize; + + /***/ }), /***/ 536: diff --git a/src/docker.js b/src/docker.js index 5ce083e..4a3ec93 100644 --- a/src/docker.js +++ b/src/docker.js @@ -2,6 +2,7 @@ const cp = require('child_process'); const core = require('@actions/core'); const fs = require('fs'); const { context } = require('@actions/github'); +const maxBufferSize = require('../src/settings'); const isGitHubTag = ref => ref && ref.includes('refs/tags/'); @@ -56,7 +57,7 @@ const build = (imageName, buildArgs) => { } core.info(`Building Docker image: ${imageName}`); - cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: 50 * 1024 * 1024 }); + cp.execSync(createBuildCommand(dockerfile, imageName, buildArgs), { maxBuffer: maxBufferSize }); }; const isEcr = registry => registry && registry.includes('amazonaws'); diff --git a/src/settings.js b/src/settings.js new file mode 100644 index 0000000..5635725 --- /dev/null +++ b/src/settings.js @@ -0,0 +1,3 @@ +const maxBufferSize = 50 * 1024 * 1024; + +module.exports = maxBufferSize; diff --git a/tests/docker-build-push.test.js b/tests/docker-build-push.test.js index 0ebea26..0f3594d 100644 --- a/tests/docker-build-push.test.js +++ b/tests/docker-build-push.test.js @@ -5,6 +5,7 @@ const core = require('@actions/core'); const cp = require('child_process'); const docker = require('../src/docker'); const run = require('../src/docker-build-push'); +const maxBufferSize = require('../src/settings'); beforeAll(() => { docker.push = jest.fn(); @@ -39,7 +40,9 @@ describe('Create & push Docker image', () => { expect(docker.createTag).toHaveBeenCalledTimes(1); expect(core.getInput).toHaveBeenCalledTimes(5); expect(core.setOutput).toHaveBeenCalledWith('imageFullName', `${registry}/${image}:${tag}`); - expect(cp.execSync).toHaveBeenCalledWith(`docker build -f ${dockerfile} -t ${registry}/${image}:${tag} .`); + expect(cp.execSync).toHaveBeenCalledWith(`docker build -f ${dockerfile} -t ${registry}/${image}:${tag} .`, { + maxBuffer: maxBufferSize + }); }); }); @@ -63,7 +66,10 @@ describe('Create & push Docker image with build args', () => { expect(core.getInput).toHaveBeenCalledTimes(5); expect(core.setOutput).toHaveBeenCalledWith('imageFullName', `${registry}/${image}:${tag}`); expect(cp.execSync).toHaveBeenCalledWith( - `docker build -f ${dockerfile} -t ${registry}/${image}:${tag} --build-arg VERSION=1.1.1 --build-arg BUILD_DATE=2020-01-14 .` + `docker build -f ${dockerfile} -t ${registry}/${image}:${tag} --build-arg VERSION=1.1.1 --build-arg BUILD_DATE=2020-01-14 .`, + { + maxBuffer: maxBufferSize + } ); }); }); diff --git a/tests/docker.test.js b/tests/docker.test.js index 82489de..3afcb8f 100644 --- a/tests/docker.test.js +++ b/tests/docker.test.js @@ -5,6 +5,7 @@ const core = require('@actions/core'); const cp = require('child_process'); const fs = require('fs'); const docker = require('../src/docker.js'); +const maxBufferSize = require('../src/settings'); describe('Create Docker image tag from git ref', () => { test('Create from tag push', () => { @@ -101,7 +102,9 @@ describe('core and cp methods', () => { docker.build(image); expect(fs.existsSync).toHaveBeenCalledWith('Dockerfile'); - expect(cp.execSync).toHaveBeenCalledWith(`docker build -f Dockerfile -t ${image} .`); + expect(cp.execSync).toHaveBeenCalledWith(`docker build -f Dockerfile -t ${image} .`, { + maxBuffer: maxBufferSize + }); }); test('Build with build args', () => { @@ -113,7 +116,10 @@ describe('core and cp methods', () => { docker.build(image, buildArgs); expect(fs.existsSync).toHaveBeenCalledWith('Dockerfile'); expect(cp.execSync).toHaveBeenCalledWith( - `docker build -f Dockerfile -t ${image} --build-arg VERSION=latest --build-arg BUILD_DATE=2020-01-14 .` + `docker build -f Dockerfile -t ${image} --build-arg VERSION=latest --build-arg BUILD_DATE=2020-01-14 .`, + { + maxBuffer: maxBufferSize + } ); }); });