docker command: Create 2 different actions publish and link (#6907)

* Create 2 different action publish and link

* use enum

* Use publish as default action

* Check if action is valid

* Add back compatibility
This commit is contained in:
Maurizio Vitale
2021-04-09 15:27:27 +01:00
committed by GitHub
parent 833ca30440
commit a1ae3ae8c8
4 changed files with 168 additions and 101 deletions

View File

@@ -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 ...`);
}
}