private async Task PushToECR(DockerCLIWrapper dockerCli, string sourceDockerImageTag, string destinationDockerImageTag) { var sourceRepoInfo = SplitImageTag(sourceDockerImageTag); await InitiateDockerLogin(dockerCli); var splitDestinationTag = SplitImageTag(destinationDockerImageTag); Repository repository = await SetupECRRepository(splitDestinationTag.RepositoryName); var targetTag = repository.RepositoryUri + ":" + splitDestinationTag.Tag; this.Logger?.WriteLine($"Taging image {sourceRepoInfo.FullTagName} with {destinationDockerImageTag}"); if (dockerCli.Tag(sourceRepoInfo.FullTagName, targetTag) != 0) { throw new ToolsException("Error executing \"docker tag\"", ToolsException.CommonErrorCode.DockerTagFail); } this.Logger?.WriteLine("Pushing image to ECR repository"); if (dockerCli.Push(targetTag) != 0) { throw new ToolsException("Error executing \"docker push\"", ToolsException.CommonErrorCode.DockerPushFail); } this.PushedImageUri = targetTag; this.Logger?.WriteLine($"Image {this.PushedImageUri} Push Complete. "); }
public override async Task <bool> ExecuteAsync() { try { string configuration = this.GetStringValueOrDefault(this.PushDockerImageProperties.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false) ?? "Release"; string targetFramework = this.GetStringValueOrDefault(this.PushDockerImageProperties.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false); this.PushDockerImageProperties.DockerImageTag = this.GetStringValueOrDefault(this.PushDockerImageProperties.DockerImageTag, ECSDefinedCommandOptions.ARGUMENT_DOCKER_TAG, true).ToLower(); if (!this.PushDockerImageProperties.DockerImageTag.Contains(":")) { this.PushDockerImageProperties.DockerImageTag += ":latest"; } var projectLocation = Utilities.DetermineProjectLocation(this.WorkingDirectory, this.ProjectLocation); var dockerDetails = InspectDockerFile(projectLocation); if (!dockerDetails.SkipDotnetBuild) { var dotnetCli = new DotNetCLIWrapper(this.Logger, projectLocation); this.Logger?.WriteLine("Executing publish command"); if (dotnetCli.Publish(projectLocation, dockerDetails.ExpectedPublishLocation, targetFramework, configuration) != 0) { throw new DockerToolsException("Error executing \"dotnet publish\"", DockerToolsException.CommonErrorCode.DotnetPublishFailed); } } var dockerCli = new DockerCLIWrapper(this.Logger, projectLocation); this.Logger?.WriteLine("Executing docker build"); string dockerBuildWorkingDirectory = dockerDetails.BuildFromSolutionDirectory ? DetermineSolutionDirectory(projectLocation) : projectLocation; if (dockerCli.Build(this.DefaultConfig, dockerBuildWorkingDirectory, Path.Combine(projectLocation, "Dockerfile"), this.PushDockerImageProperties.DockerImageTag) != 0) { throw new DockerToolsException("Error executing \"docker build\"", DockerToolsException.ECSErrorCode.DockerBuildFailed); } await InitiateDockerLogin(dockerCli); Repository repository = await SetupECRRepository(this.PushDockerImageProperties.DockerImageTag.Substring(0, this.PushDockerImageProperties.DockerImageTag.IndexOf(':'))); var targetTag = repository.RepositoryUri + this.PushDockerImageProperties.DockerImageTag.Substring(this.PushDockerImageProperties.DockerImageTag.IndexOf(':')); this.Logger?.WriteLine($"Taging image {this.PushDockerImageProperties.DockerImageTag} with {targetTag}"); if (dockerCli.Tag(this.PushDockerImageProperties.DockerImageTag, targetTag) != 0) { throw new DockerToolsException("Error executing \"docker tag\"", DockerToolsException.ECSErrorCode.DockerTagFail); } this.Logger?.WriteLine("Pushing image to ECR repository"); if (dockerCli.Push(targetTag) != 0) { throw new DockerToolsException("Error executing \"docker push\"", DockerToolsException.ECSErrorCode.DockerPushFail); } this.PushedImageUri = targetTag; this.Logger?.WriteLine($"Image {this.PushedImageUri} Push Complete. "); if (this.GetBoolValueOrDefault(this.PersistConfigFile, CommonDefinedCommandOptions.ARGUMENT_PERSIST_CONFIG_FILE, false).GetValueOrDefault()) { this.SaveConfigFile(); } } catch (DockerToolsException e) { this.Logger?.WriteLine(e.Message); this.LastToolsException = e; return(false); } catch (Exception e) { this.Logger?.WriteLine($"Unknown error executing docker push to Amazon Elastic Container Registry: {e.Message}"); this.Logger?.WriteLine(e.StackTrace); return(false); } return(true); }
protected override async Task <bool> PerformActionAsync() { var configuration = this.GetStringValueOrDefault(this.PushDockerImageProperties.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false) ?? "Release"; var targetFramework = this.GetStringValueOrDefault(this.PushDockerImageProperties.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false); string publishOptions = this.GetStringValueOrDefault(this.PushDockerImageProperties.PublishOptions, CommonDefinedCommandOptions.ARGUMENT_PUBLISH_OPTIONS, false); this.PushDockerImageProperties.DockerImageTag = this.GetStringValueOrDefault(this.PushDockerImageProperties.DockerImageTag, ECSDefinedCommandOptions.ARGUMENT_DOCKER_TAG, true).ToLower(); if (!this.PushDockerImageProperties.DockerImageTag.Contains(":")) { this.PushDockerImageProperties.DockerImageTag += ":latest"; } var projectLocation = Utilities.DetermineProjectLocation(this.WorkingDirectory, this.ProjectLocation); var dockerDetails = InspectDockerFile(this.Logger, projectLocation); if (!dockerDetails.SkipDotnetBuild) { this.EnsureInProjectDirectory(); var dotnetCli = new DotNetCLIWrapper(this.Logger, projectLocation); this.Logger?.WriteLine("Executing publish command"); if (dotnetCli.Publish(projectLocation, dockerDetails.ExpectedPublishLocation, targetFramework, configuration, publishOptions) != 0) { throw new DockerToolsException("Error executing \"dotnet publish\"", DockerToolsException.CommonErrorCode.DotnetPublishFailed); } } var dockerCli = new DockerCLIWrapper(this.Logger, projectLocation); this.Logger?.WriteLine("Executing docker build"); var dockerBuildWorkingDirectory = this.GetStringValueOrDefault(this.PushDockerImageProperties.DockerBuildWorkingDirectory, ECSDefinedCommandOptions.ARGUMENT_DOCKER_BUILD_WORKING_DIRECTORY, false); if (string.IsNullOrEmpty(dockerBuildWorkingDirectory)) { dockerBuildWorkingDirectory = dockerDetails.BuildFromSolutionDirectory ? DetermineSolutionDirectory(projectLocation) : projectLocation; } else { if (!Path.IsPathRooted(dockerBuildWorkingDirectory)) { dockerBuildWorkingDirectory = Path.GetFullPath(Path.Combine(projectLocation, dockerBuildWorkingDirectory)); } this.PushDockerImageProperties.DockerBuildWorkingDirectory = Utilities.RelativePathTo(projectLocation, dockerBuildWorkingDirectory); } var dockerBuildOptions = this.GetStringValueOrDefault(this.PushDockerImageProperties.DockerBuildOptions, ECSDefinedCommandOptions.ARGUMENT_DOCKER_BUILD_OPTIONS, false); if (dockerCli.Build(this.DefaultConfig, dockerBuildWorkingDirectory, Path.Combine(projectLocation, "Dockerfile"), this.PushDockerImageProperties.DockerImageTag, dockerBuildOptions) != 0) { throw new DockerToolsException("Error executing \"docker build\"", DockerToolsException.ECSErrorCode.DockerBuildFailed); } await InitiateDockerLogin(dockerCli); Repository repository = await SetupECRRepository(this.PushDockerImageProperties.DockerImageTag.Substring(0, this.PushDockerImageProperties.DockerImageTag.IndexOf(':'))); var targetTag = repository.RepositoryUri + this.PushDockerImageProperties.DockerImageTag.Substring(this.PushDockerImageProperties.DockerImageTag.IndexOf(':')); this.Logger?.WriteLine($"Taging image {this.PushDockerImageProperties.DockerImageTag} with {targetTag}"); if (dockerCli.Tag(this.PushDockerImageProperties.DockerImageTag, targetTag) != 0) { throw new DockerToolsException("Error executing \"docker tag\"", DockerToolsException.ECSErrorCode.DockerTagFail); } this.Logger?.WriteLine("Pushing image to ECR repository"); if (dockerCli.Push(targetTag) != 0) { throw new DockerToolsException("Error executing \"docker push\"", DockerToolsException.ECSErrorCode.DockerPushFail); } this.PushedImageUri = targetTag; this.Logger?.WriteLine($"Image {this.PushedImageUri} Push Complete. "); return(true); }