Skip to content

Commit

Permalink
FEATURE: Add the strip option which allows to removes certain parts o…
Browse files Browse the repository at this point in the history
…f the generated docker tags
  • Loading branch information
Inkdpixels committed Aug 30, 2017
1 parent ea46b7a commit cd533c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ npm i -D @immowelt/docker-publish
--arg <string> The build-arg key to use when forwarding the current iterated tag.
--image <string> The image name for the docker images to create.
--latest <string> An optional 'latest' tag to specify, defaults to 'latest'
--strip <string> An optional string which will be removed from the docker tags to generate. E.g. useful if your repo is named 'foo/java-alpine' and you don't want to repeat the '-alpine' keyword in tags.
```

#### Example usage
Expand Down
22 changes: 19 additions & 3 deletions src/commands/default.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// @flow

type ExecFn = (arg: string, isLogging?: boolean) => Promise<void>;
type Opts = {
tags: Array<string>,
image: string,
arg: string,
latest?: string,
strip?: string
};
type BuildOpts = {
image: string,
arg: string,
version: string,
tag?: string
};

const logger = require('log-fancy')('@immowelt/docker-publish');
const fetch = require('node-fetch').default;
const isUrl = require('is-url');
const semver = require('semver');
const asyncExec = require('async-exec');

async function buildAndPush(opts: {image: string, arg: string, version: string, tag?: string}) {
async function buildAndPush(opts: BuildOpts) {
const exec: ExecFn = asyncExec.default;
const {
image,
Expand All @@ -35,9 +48,10 @@ async function buildAndPush(opts: {image: string, arg: string, version: string,
logger.success(`Successfuly pushed ${dockerImageTag}!`);
}

module.exports = async (opts: {tags: Array<string>, image: string, arg: string, latest?: string}) => {
module.exports = async (opts: Opts) => {
const {
latest = 'latest',
strip = false,
tags,
image,
arg
Expand Down Expand Up @@ -69,11 +83,13 @@ module.exports = async (opts: {tags: Array<string>, image: string, arg: string,
//
for (var i = 0; i < versionTags.length; i++) {
const version = versionTags[i];
const tag = strip && strip.length ? version.replace(strip, '') : version;

await buildAndPush({ // eslint-disable-line no-await-in-loop
image,
arg,
version
version,
tag
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/commands/default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,21 @@ describe('defaultFlow()', () => {
expect(exec.args[1][0]).toContain('docker push');
expect(exec.args[1][0]).toContain('foo/bar:latest');
});

it('should allow the stripping of parts from the version tags.', async () => {
await defaultFlow({
tags: ['2.2.1-alpine', '3.0.0-alpine'],
image: 'foo/bar',
strip: '-alpine',
arg: 'BAR_VERSION'
});

expect(exec.args[0][0]).toContain('-t foo/bar:2.2.1');
expect(exec.args[0][0]).toContain('--build-arg BAR_VERSION=2.2.1-alpine');
expect(exec.args[1][0]).toContain('foo/bar:2.2.1');

expect(exec.args[2][0]).toContain('-t foo/bar:3.0.0');
expect(exec.args[2][0]).toContain('--build-arg BAR_VERSION=3.0.0-alpine');
expect(exec.args[3][0]).toContain('foo/bar:3.0.0');
});
});
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cli
.option('--image <string>', 'The docker image name to use as a base.')
.option('--arg <string>', 'The build-arg key which will be used to.')
.option('--latest <string>', 'The tag/version to pass into the docker build when building your latest image, defaults to "latest".')
.option('--strip <string>', 'An optional string to strip from the generated/fetched docker tags.')
.description('Uses/Fetches the given tags, filters out release only tags based on semver and builds/pushes the docker images.')
.version(pkg.version)
.parse(process.argv);
Expand All @@ -20,13 +21,14 @@ if (!cli.rawArgs.length) {
}

(async function () {
const {tags, image, arg, latest} = cli;
const {tags, image, arg, latest, strip} = cli;

try {
await defaultFlow({
latest,
tags,
image,
strip,
arg
});
} catch (err) {
Expand Down

0 comments on commit cd533c9

Please sign in to comment.