UNPKG

@facets-cloud/facetsctlv3

Version:
93 lines (92 loc) 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const artifact_service_1 = require("../../services/artifact-service"); const config_service_1 = require("../../services/config-service"); const facets_api_1 = require("../../services/facets-api"); const child_process_1 = require("child_process"); class Push extends core_1.Command { static description = 'Push a Docker image to the configured artifact repository'; static examples = [ `$ facetsctl artifact push --docker-image my-docker-image:latest Successfully pushed my-docker-image:latest `, ]; static flags = { 'docker-image': core_1.Flags.string({ char: 'd', description: 'URL of the Docker image to push', required: true, }), }; async run() { const { flags } = await this.parse(Push); const dockerImage = flags['docker-image']; // Extract the tag from the Docker image const tag = dockerImage.split(':')[1] || 'latest'; // Read the artifact configuration let artifactInfo; try { artifactInfo = artifact_service_1.ArtifactService.readArtifactInfo(); } catch (error) { this.error('Artifact configuration file not found. Please run `facetsctl artifact init` first.'); return; } // Read the main configuration const configFilePath = config_service_1.ConfigService.findConfigFile(); if (!configFilePath) { this.error('Configuration file not found. Please ensure you are logged in.'); return; } const config = config_service_1.ConfigService.readConfig(configFilePath); this.log('Logged in to: ' + config.ControlPlaneURL + ' with user: ' + config.Username); // Get credentials from the API core_1.ux.action.start('Getting credentials'); if (!artifactInfo.serviceName && !artifactInfo.ciIntegrationName) { core_1.ux.action.stop('Failed'); this.error(`Failed to get credentials: Either service or ci-integration is required`); } let credentials; try { credentials = await (0, facets_api_1.getCredentials)(config.ControlPlaneURL, config.Username, config.AccessToken, artifactInfo.projectName, artifactInfo.serviceName || artifactInfo.ciIntegrationName || '', artifactInfo.artifactoryName); } catch (error) { core_1.ux.action.stop('Failed'); if (error instanceof Error) { this.error(`Failed to get credentials: ${error.message}`); } return; } core_1.ux.action.stop('Done'); // Construct the Docker push command const repositoryUri = credentials.repositoryUri.replace('https://', ''); const repositoryUriWithTag = `${repositoryUri}:${tag}`; const dockerLoginCommand = `docker login ${credentials.artifactoryUri} -u ${credentials.username} -p ${credentials.password}`; const dockerPushCommand = `docker push ${repositoryUriWithTag}`; const dockerTagCommand = `docker tag ${dockerImage} ${repositoryUriWithTag}`; // Update the artifact info with the repository URI artifactInfo.repositoryUrl = repositoryUri; artifact_service_1.ArtifactService.writeArtifactInfo(artifactInfo); // Execute the Docker commands try { core_1.ux.action.start('Executing Docker tag'); (0, child_process_1.execSync)(dockerTagCommand, { stdio: 'inherit' }); core_1.ux.action.stop('Done'); core_1.ux.action.start('Executing Docker login'); (0, child_process_1.execSync)(dockerLoginCommand, { stdio: 'inherit' }); core_1.ux.action.stop('Done'); core_1.ux.action.start('Executing Docker push'); (0, child_process_1.execSync)(dockerPushCommand, { stdio: 'inherit' }); core_1.ux.action.stop('Done'); this.log(`Successfully pushed ${repositoryUriWithTag}`); } catch (error) { if (error instanceof Error) { this.log(error.stack); this.error(`${error.message}`); } } } } exports.default = Push;