Skip to content

Commit 6387d35

Browse files
Add optional pushImage input (#111)
1 parent 3cedd44 commit 6387d35

File tree

9 files changed

+76
-14
lines changed

9 files changed

+76
-14
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ jobs:
1010
- name: Check out code
1111
uses: actions/checkout@v2
1212

13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 16
16+
1317
- name: Build
1418
run: npm ci
1519

.github/workflows/pr.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ jobs:
1010
- name: Check out code
1111
uses: actions/checkout@v2
1212

13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 16
16+
1317
- name: Build
1418
run: npm ci
1519

16-
- name: Run unit tests
20+
- name: Run unit tests
1721
run: npm run test

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ steps:
5858
| password | Docker registry password or token | No | String |
5959
| githubOrg | GitHub organization to push image to (if not current) | No | String |
6060
| enableBuildKit | Enables Docker BuildKit support | No | Boolean |
61+
| pushImage | Flag for disabling the login & push steps, set to `true` by default | No | Boolean |
6162

6263
## Outputs
6364

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ inputs:
5151
description: "Enables Docker BuildKit support"
5252
required: false
5353
default: "false"
54+
pushImage:
55+
description: "Flag for disabling the login & push steps, set to true by default"
56+
required: false
57+
default: "true"
5458
outputs:
5559
imageFullName:
5660
description: "Full name of the Docker image with registry prefix and tag suffix"
@@ -59,7 +63,7 @@ outputs:
5963
tags:
6064
description: "Tags for the Docker image"
6165
runs:
62-
using: "node12"
66+
using: "node16"
6367
main: "dist/index.js"
6468
branding:
6569
icon: "anchor"

dist/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10050,7 +10050,8 @@ const buildOpts = {
1005010050
target: undefined,
1005110051
buildDir: undefined,
1005210052
enableBuildKit: false,
10053-
platform: undefined
10053+
platform: undefined,
10054+
skipPush: false
1005410055
};
1005510056

1005610057
const run = () => {
@@ -10071,15 +10072,16 @@ const run = () => {
1007110072
buildOpts.buildDir = core.getInput('directory') || '.';
1007210073
buildOpts.enableBuildKit = core.getInput('enableBuildKit') === 'true';
1007310074
buildOpts.platform = core.getInput('platform');
10075+
buildOpts.skipPush = core.getInput('pushImage') === 'false';
1007410076

1007510077
// Create the Docker image name
1007610078
const imageFullName = docker.createFullImageName(registry, image, githubOwner);
1007710079
core.info(`Docker image name used for this build: ${imageFullName}`);
1007810080

1007910081
// Log in, build & push the Docker image
10080-
docker.login(username, password, registry);
10082+
docker.login(username, password, registry, buildOpts.skipPush);
1008110083
docker.build(imageFullName, dockerfile, buildOpts);
10082-
docker.push(imageFullName, buildOpts.tags);
10084+
docker.push(imageFullName, buildOpts.tags, buildOpts.skipPush);
1008310085

1008410086
// Capture outputs
1008510087
core.setOutput('imageFullName', imageFullName);
@@ -10197,7 +10199,12 @@ const isEcr = registry => registry && registry.includes('amazonaws');
1019710199
const getRegion = registry => registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
1019810200

1019910201
// Log in to provided Docker registry
10200-
const login = (username, password, registry) => {
10202+
const login = (username, password, registry, skipPush) => {
10203+
if (skipPush) {
10204+
core.info('Input skipPush is set to true, skipping Docker log in step...');
10205+
return;
10206+
}
10207+
1020110208
// If using ECR, use the AWS CLI login command in favor of docker login
1020210209
if (isEcr(registry)) {
1020310210
const region = getRegion(registry);
@@ -10210,11 +10217,18 @@ const login = (username, password, registry) => {
1021010217
cp.execSync(`docker login -u ${username} --password-stdin ${registry}`, {
1021110218
input: password
1021210219
});
10220+
} else {
10221+
core.setFailed('Must supply Docker registry credentials to push image!');
1021310222
}
1021410223
};
1021510224

1021610225
// Push Docker image & all tags
10217-
const push = (imageName, tags) => {
10226+
const push = (imageName, tags, skipPush) => {
10227+
if (skipPush) {
10228+
core.info('Input skipPush is set to true, skipping Docker push step...');
10229+
return;
10230+
}
10231+
1021810232
core.info(`Pushing tags ${tags} for Docker image ${imageName}...`);
1021910233
cp.execSync(`docker push ${imageName} --all-tags`, cpOptions);
1022010234
};

src/docker-build-push.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const buildOpts = {
1010
target: undefined,
1111
buildDir: undefined,
1212
enableBuildKit: false,
13-
platform: undefined
13+
platform: undefined,
14+
skipPush: false
1415
};
1516

1617
const run = () => {
@@ -31,15 +32,16 @@ const run = () => {
3132
buildOpts.buildDir = core.getInput('directory') || '.';
3233
buildOpts.enableBuildKit = core.getInput('enableBuildKit') === 'true';
3334
buildOpts.platform = core.getInput('platform');
35+
buildOpts.skipPush = core.getInput('pushImage') === 'false';
3436

3537
// Create the Docker image name
3638
const imageFullName = docker.createFullImageName(registry, image, githubOwner);
3739
core.info(`Docker image name used for this build: ${imageFullName}`);
3840

3941
// Log in, build & push the Docker image
40-
docker.login(username, password, registry);
42+
docker.login(username, password, registry, buildOpts.skipPush);
4143
docker.build(imageFullName, dockerfile, buildOpts);
42-
docker.push(imageFullName, buildOpts.tags);
44+
docker.push(imageFullName, buildOpts.tags, buildOpts.skipPush);
4345

4446
// Capture outputs
4547
core.setOutput('imageFullName', imageFullName);

src/docker.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ const isEcr = registry => registry && registry.includes('amazonaws');
9797
const getRegion = registry => registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
9898

9999
// Log in to provided Docker registry
100-
const login = (username, password, registry) => {
100+
const login = (username, password, registry, skipPush) => {
101+
if (skipPush) {
102+
core.info('Input skipPush is set to true, skipping Docker log in step...');
103+
return;
104+
}
105+
101106
// If using ECR, use the AWS CLI login command in favor of docker login
102107
if (isEcr(registry)) {
103108
const region = getRegion(registry);
@@ -110,11 +115,18 @@ const login = (username, password, registry) => {
110115
cp.execSync(`docker login -u ${username} --password-stdin ${registry}`, {
111116
input: password
112117
});
118+
} else {
119+
core.setFailed('Must supply Docker registry credentials to push image!');
113120
}
114121
};
115122

116123
// Push Docker image & all tags
117-
const push = (imageName, tags) => {
124+
const push = (imageName, tags, skipPush) => {
125+
if (skipPush) {
126+
core.info('Input skipPush is set to true, skipping Docker push step...');
127+
return;
128+
}
129+
118130
core.info(`Pushing tags ${tags} for Docker image ${imageName}...`);
119131
cp.execSync(`docker push ${imageName} --all-tags`, cpOptions);
120132
};

tests/docker-build-push.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const mockRepoName = 'some-repo';
2424

2525
const runAssertions = (imageFullName, inputs, tagOverrides) => {
2626
// Inputs
27-
expect(core.getInput).toHaveBeenCalledTimes(15);
27+
expect(core.getInput).toHaveBeenCalledTimes(16);
2828

2929
// Outputs
3030
const tags = tagOverrides || parseArray(inputs.tags);

tests/docker.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,21 @@ describe('Docker build, login & push commands', () => {
296296
);
297297
});
298298

299-
test("returns undefined if empty login and doesn't execute command", () => {
299+
test('Skip login command if skipPush is set to true', () => {
300+
const skipPush = true;
301+
docker.login(username, password, registry, skipPush);
302+
303+
expect(cp.execSync.mock.calls.length).toEqual(0);
304+
});
305+
306+
test('Missing username or password should throw an error', () => {
307+
docker.login(undefined, undefined, registry);
308+
309+
expect(cp.execSync.mock.calls.length).toEqual(0);
310+
expect(core.setFailed).toHaveBeenCalled();
311+
});
312+
313+
test('returns undefined if empty login and does not execute command', () => {
300314
docker.login(username, password, registry);
301315

302316
expect(cp.execSync.mock.calls.length).toEqual(0);
@@ -312,5 +326,12 @@ describe('Docker build, login & push commands', () => {
312326

313327
expect(cp.execSync).toHaveBeenCalledWith(`docker push ${imageName} --all-tags`, cpOptions);
314328
});
329+
330+
test('Skip push command if skipPush is set to true', () => {
331+
const skipPush = true;
332+
docker.push('my-org/my-image', 'latest', skipPush);
333+
334+
expect(cp.execSync.mock.calls.length).toEqual(0);
335+
});
315336
});
316337
});

0 commit comments

Comments
 (0)