// Copyright Epic Games, Inc. All Rights Reserved. using AutomationTool; using EpicGames.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Xml; namespace BuildGraph.Tasks { /// /// Parameters for a Docker-Build task /// public class DockerPushTaskParameters { /// /// Repository /// [TaskParameter] public string Repository; /// /// Source image to push /// [TaskParameter] public string Image; /// /// Name of the target image /// [TaskParameter(Optional = true)] public string TargetImage; /// /// Additional environment variables /// [TaskParameter(Optional = true)] public string Environment; /// /// File to read environment from /// [TaskParameter(Optional = true)] public string EnvironmentFile; /// /// Whether to login to AWS ECR /// [TaskParameter(Optional = true)] public bool AwsEcr; } /// /// Spawns Docker and waits for it to complete. /// [TaskElement("Docker-Push", typeof(DockerPushTaskParameters))] public class DockerPushTask : SpawnTaskBase { /// /// Parameters for this task /// DockerPushTaskParameters Parameters; /// /// Construct a Docker task /// /// Parameters for the task public DockerPushTask(DockerPushTaskParameters InParameters) { Parameters = InParameters; } /// /// Execute the task. /// /// Information about the current job /// Set of build products produced by this node. /// Mapping from tag names to the set of files they include public override void Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { Log.TraceInformation("Pushing Docker image"); using (LogIndentScope Scope = new LogIndentScope(" ")) { Dictionary Environment = ParseEnvVars(Parameters.Environment, Parameters.EnvironmentFile); if (Parameters.AwsEcr) { IProcessResult Result = SpawnTaskBase.Execute("aws", "ecr get-login-password", EnvVars: Environment, LogOutput: false); Execute("docker", $"login {Parameters.Repository} --username AWS --password-stdin", Input: Result.Output); } string TargetImage = Parameters.TargetImage ?? Parameters.Image; Execute("docker", $"tag {Parameters.Image} {Parameters.Repository}/{TargetImage}", EnvVars: Environment); Execute("docker", $"push {Parameters.Repository}/{TargetImage}", EnvVars: Environment); } } /// /// Output this task out to an XML writer. /// public override void Write(XmlWriter Writer) { Write(Writer, Parameters); } /// /// Find all the tags which are used as inputs to this task /// /// The tag names which are read by this task public override IEnumerable FindConsumedTagNames() { yield break; } /// /// Find all the tags which are modified by this task /// /// The tag names which are modified by this task public override IEnumerable FindProducedTagNames() { yield break; } } }