diff --git a/lib/cli/README.md b/lib/cli/README.md index db3afaff31..7b6e85a7eb 100644 --- a/lib/cli/README.md +++ b/lib/cli/README.md @@ -47,7 +47,7 @@ In develop mode, the CLI takes the prebuilt scripts from the dist folder. |check-plugin-env |Check plugin status | |artifact-from-s3 |Get artifact from S3 | |artifact-to-s3 |Get artifact to S3 | -|docker-publish |publish docker image| +|docker |Build and publish a docker image or create additional tag link | |init-aae-env |Init env| |init-aps-env |Init aps| |kubectl-delete |delete kubectl | @@ -81,19 +81,29 @@ npm install adf-cli audit ``` -### Docker publish +### Docker +The command provides 2 targets 'Publish' (default value) and 'Link' +Publish target Move in the folder where you have your `Dockerfile` and run the command: ```bash -adf-cli docker-publish --dockerRepo "${docker_repository}" --dockerTags "${TAGS}" --pathProject "$(pwd)" +adf-cli docker --target "publish" --dockerRepo "${docker_repository}" --dockerTags "${TAGS}" ``` If you want to specify a different docker registry you can run ```bash ---loginCheck --loginUsername "username" --loginPassword "password" --loginRepo "quay.io"--dockerRepo "${docker_repository}" --dockerTags "${TAGS}" --pathProject "$(pwd)" +--loginCheck --loginUsername "username" --loginPassword "password" --loginRepo "quay.io"--dockerRepo "${docker_repository}" --dockerTags "${TAGS}" ``` +Link target +In case you don't need to publish a new image but you would like to create a link to an already existing image (sourceTag) you can use the link target. + +```bash +adf-cli docker --target "link" --dockerRepo "${docker_repository}" --dockerTags "${TAGS}" --sourceTag "develop" +``` + + ### Kubectl update pod image This command allows you to update a specific service on the rancher env with a specific tag diff --git a/lib/cli/scripts/docker-publish.ts b/lib/cli/scripts/docker-publish.ts index 37511cd48d..6d1224f6ab 100644 --- a/lib/cli/scripts/docker-publish.ts +++ b/lib/cli/scripts/docker-publish.ts @@ -17,102 +17,10 @@ * limitations under the License. */ -import { exec } from './exec'; -import * as program from 'commander'; -import { logger } from './logger'; -import { resolve } from 'path'; -export interface PublishArgs { - tag?: string; - loginCheck?: boolean; - loginUsername?: string; - loginPassword?: string; - loginRepo?: string; - dockerRepo?: string; - buildArgs?: string; - dockerTags?: string; - pathProject: string; +import * as docker from './docker'; + +export default function (args: any) { + docker.default(args); } -function loginPerform(args: PublishArgs) { - logger.info(`Perform docker login...${args.loginRepo}`); - const loginDockerRes = exec('docker', ['login', `-u=${args.loginUsername}`, `-p=${args.loginPassword}`, `${args.loginRepo}`], {}); - logger.info(loginDockerRes); -} - -function buildImagePerform(args: PublishArgs, tag: string) { - logger.info(`Perform docker build...${args.dockerRepo}:${tag}`); - const response = exec('docker', ['build', `-t=${args.dockerRepo}:${tag}`, `--build-arg=${args.buildArgs}`, args.pathProject], {}); - logger.info(response); -} - -function tagImagePerform(args: PublishArgs, tagImage: string, newTag: string) { - logger.info(`Perform docker tag... ${args.dockerRepo}:${tagImage} on ${args.dockerRepo}:${newTag}`); - const response = exec('docker', ['tag', `${args.dockerRepo}:${tagImage}`, `${args.dockerRepo}:${newTag}`], {}); - logger.info(response); -} - -function pushImagePerform(args: PublishArgs, tag: string) { - logger.info(`Perform docker push... ${args.dockerRepo}:${tag}`); - const response = exec('docker', ['push', `${args.dockerRepo}:${tag}`], {}); - logger.info(response); -} - -function cleanImagePerform(args: PublishArgs, tag: string) { - logger.info('Perform docker clean...'); - const response = exec('docker', ['rmi', `-f`, `${args.dockerRepo}:${tag}`], {}); - logger.info(response); -} - -export default function (args: PublishArgs) { - main(args); -} - -function main(args) { - program - .version('0.1.0') - .description('Move in the folder where you have your Dockerfile and run the command:\n\n' + - 'adf-cli docker-publish --dockerRepo "${docker_repository}" --dockerTags "${TAGS}"') - .option('--loginRepo [type]', 'URL registry') - .option('--loginPassword [type]', ' password') - .option('--loginUsername [type]', ' username') - .option('--loginCheck [type]', 'perform login') - .option('--pathProject [type]', 'the path build context') - .requiredOption('--dockerRepo [type]', 'docker repo') - .requiredOption('--dockerTags [type]', ' tags') - .requiredOption('--buildArgs [type]', ' buildArgs') - .parse(process.argv); - - if (process.argv.includes('-h') || process.argv.includes('--help')) { - program.outputHelp(); - return; - } - - if (args.loginCheck === true) { - loginPerform(args); - } - - if(args.pathProject === undefined) { - args.pathProject = resolve('./') - } - - let mainTag; - if (args.dockerTags !== '') { - args.dockerTags.split(',').forEach( (tag, index) => { - if (tag) { - logger.info(`Analyzing tag:${tag} ...`); - if (index === 0) { - logger.info(`Build only once`); - mainTag = tag; - buildImagePerform(args, mainTag); - } - tagImagePerform(args, mainTag, tag); - pushImagePerform(args, tag); - } - }); - logger.info(`Clean the image with tag:${mainTag} ...`); - cleanImagePerform(args, mainTag); - } else { - logger.error(`dockerTags cannot be empty ...`); - } -} diff --git a/lib/cli/scripts/docker.ts b/lib/cli/scripts/docker.ts new file mode 100644 index 0000000000..25563747a4 --- /dev/null +++ b/lib/cli/scripts/docker.ts @@ -0,0 +1,149 @@ +#!/usr/bin/env node + +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exec } from './exec'; +import * as program from 'commander'; +import { logger } from './logger'; +import { resolve } from 'path'; + +enum TARGETS { + Publish = 'publish', + Link = 'link' +} +export interface PublishArgs { + tag?: string; + loginCheck?: boolean; + loginUsername?: string; + loginPassword?: string; + loginRepo?: string; + dockerRepo?: string; + buildArgs?: string; + dockerTags?: string; + pathProject: string; +} + +function loginPerform(args: PublishArgs) { + logger.info(`Perform docker login...${args.loginRepo}`); + const loginDockerRes = exec('docker', ['login', `-u=${args.loginUsername}`, `-p=${args.loginPassword}`, `${args.loginRepo}`], {}); + logger.info(loginDockerRes); +} + +function buildImagePerform(args: PublishArgs, tag: string) { + logger.info(`Perform docker build...${args.dockerRepo}:${tag}`); + const response = exec('docker', ['build', `-t=${args.dockerRepo}:${tag}`, `--build-arg=${args.buildArgs}`, args.pathProject], {}); + logger.info(response); +} + +function tagImagePerform(args: PublishArgs, tagImage: string, newTag: string) { + logger.info(`Perform docker tag... ${args.dockerRepo}:${tagImage} on ${args.dockerRepo}:${newTag}`); + const response = exec('docker', ['tag', `${args.dockerRepo}:${tagImage}`, `${args.dockerRepo}:${newTag}`], {}); + logger.info(response); +} + +function pullImagePerform(dockerRepo: string, sourceTag: string) { + logger.info(`Perform docker pull... ${dockerRepo}:${sourceTag}`); + const response = exec('docker', ['pull', `${dockerRepo}:${sourceTag}`], {}); + logger.info(response); +} + +function pushImagePerform(args: PublishArgs, tag: string) { + logger.info(`Perform docker push... ${args.dockerRepo}:${tag}`); + const response = exec('docker', ['push', `${args.dockerRepo}:${tag}`], {}); + logger.info(response); +} + +function cleanImagePerform(args: PublishArgs, tag: string) { + logger.info(`Perform docker clean on tag:${tag}...`); + const response = exec('docker', ['rmi', `-f`, `${args.dockerRepo}:${tag}`], {}); + logger.info(response); +} + +export default function (args: PublishArgs) { + main(args); +} + +function main(args) { + program + .version('0.1.0') + .description('Move in the folder where you have your Dockerfile and run the command:\n\n' + + 'adf-cli docker-publish --dockerRepo "${docker_repository}" --dockerTags "${TAGS}"') + .option('--loginRepo [type]', 'URL registry') + .option('--loginPassword [type]', ' password') + .option('--loginUsername [type]', ' username') + .option('--loginCheck [type]', 'perform login') + .option('--pathProject [type]', 'the path build context') + .option('--sourceTag [type]', 'sourceTag') + .option('--buildArgs [type]', 'buildArgs') + .option('--target [type]', 'target: publish or link', TARGETS.Publish) + .requiredOption('--dockerRepo [type]', 'docker repo') + .requiredOption('--dockerTags [type]', ' tags') + .parse(process.argv); + + if (process.argv.includes('-h') || process.argv.includes('--help')) { + program.outputHelp(); + return; + } + + if (!Object.values(TARGETS).includes(program.opts().target)) { + logger.error(`error: invalid --target value. It can be ${Object.values(TARGETS)}`); + process.exit(1); + } + + if (program.opts().target === TARGETS.Publish && args.buildArgs === undefined) { + logger.error(`error: required option --buildArgs [type] in case the target is ${TARGETS.Publish}`); + process.exit(1); + } + + if (program.opts().target === TARGETS.Link && args.sourceTag === undefined) { + logger.error(`error: required option --sourceTag [type] in case the target is ${TARGETS.Link}`); + process.exit(1); + } + + if(args.pathProject === undefined) { + args.pathProject = resolve('./') + } + + if (args.loginCheck === true) { + loginPerform(args); + } + + let mainTag; + if (args.dockerTags !== '') { + args.dockerTags.split(',').forEach( (tag, index) => { + if (tag) { + logger.info(`Analyzing tag:${tag} ... for target ${program.opts().target}`); + if (program.opts().target === TARGETS.Publish) { + if (index === 0) { + logger.info(`Build only once`); + mainTag = tag; + buildImagePerform(args, mainTag); + } + } else { + mainTag = args.sourceTag; + pullImagePerform(args.dockerRepo, mainTag); + } + tagImagePerform(args, mainTag, tag); + pushImagePerform(args, tag); + } + }); + cleanImagePerform(args, mainTag); + } else { + logger.error(`dockerTags cannot be empty ...`); + } +} diff --git a/scripts/travis/release/release-docker.sh b/scripts/travis/release/release-docker.sh index e50f65bf22..c6a90d36fa 100755 --- a/scripts/travis/release/release-docker.sh +++ b/scripts/travis/release/release-docker.sh @@ -36,7 +36,7 @@ then DOCKER_PROJECT_ARGS="PROJECT_NAME=demo-shell" # Publish Image to docker - ./node_modules/@alfresco/adf-cli/bin/adf-cli docker-publish --loginCheck --loginUsername "$DOCKER_REPOSITORY_USER" --loginPassword "$DOCKER_REPOSITORY_PASSWORD" --loginRepo "$DOCKER_REPOSITORY_DOMAIN" --dockerRepo "$DOCKER_REPOSITORY" --buildArgs "$DOCKER_PROJECT_ARGS" --dockerTags "$TAGS" --pathProject "$(pwd)" + ./node_modules/@alfresco/adf-cli/bin/adf-cli docker --loginCheck --loginUsername "$DOCKER_REPOSITORY_USER" --loginPassword "$DOCKER_REPOSITORY_PASSWORD" --loginRepo "$DOCKER_REPOSITORY_DOMAIN" --dockerRepo "$DOCKER_REPOSITORY" --buildArgs "$DOCKER_PROJECT_ARGS" --dockerTags "$TAGS" --pathProject "$(pwd)" fi; else