public static void PurgeBuildArtifactsIfNecessary(string sitePackagesPath, BuildArtifactType fileExtension, ITracer tracer, int totalAllowedFiles) { string extension = fileExtension.ToString().ToLowerInvariant(); IEnumerable <string> fileNames = FileSystemHelpers.GetFiles(sitePackagesPath, $"*.{extension}"); if (fileNames.Count() > totalAllowedFiles) { // Order the files in descending order of the modified date and remove the last (N - allowed zip files). var fileNamesToDelete = fileNames.OrderByDescending(fileName => FileSystemHelpers.GetLastWriteTimeUtc(fileName)).Skip(totalAllowedFiles); foreach (var fileName in fileNamesToDelete) { using (tracer.Step("Deleting outdated zip file {0}", fileName)) { try { File.Delete(fileName); } catch (Exception ex) { tracer.TraceError(ex, "Unable to delete zip file {0}", fileName); } } } } }
/// <summary> /// Package every files and sub directories from a source folder /// </summary> /// <param name="context">The deployment context in current scope</param> /// <param name="srcDirectory">The source directory to be packed</param> /// <param name="artifactDirectory">The destination directory to eject the build artifact</param> /// <param name="artifactFilename">The filename of the build artifact</param> /// <param name="artifactType">The method for packing the artifact</param> /// <param name="numBuildArtifacts">The number of temporary artifacts should be hold in the destination directory</param> /// <returns></returns> private string PackageArtifactFromFolder(DeploymentContext context, string srcDirectory, string artifactDirectory, string artifactFilename, BuildArtifactType artifactType, int numBuildArtifacts = 0) { context.Logger.Log($"Writing the artifacts to a {artifactType.ToString()} file"); string file = Path.Combine(artifactDirectory, artifactFilename); var exe = ExternalCommandFactory.BuildExternalCommandExecutable(srcDirectory, artifactDirectory, context.Logger); try { switch (artifactType) { case BuildArtifactType.Zip: exe.ExecuteWithProgressWriter(context.Logger, context.Tracer, $"zip -r -0 -q {file} ."); break; case BuildArtifactType.Squashfs: exe.ExecuteWithProgressWriter(context.Logger, context.Tracer, $"mksquashfs . {file} -noappend"); break; default: throw new ArgumentException($"Received unknown file extension {artifactType.ToString()}"); } } catch (Exception) { context.GlobalLogger.LogError(); throw; } // Just to be sure that we don't keep adding build artifacts here if (numBuildArtifacts > 0) { DeploymentHelper.PurgeBuildArtifactsIfNecessary(artifactDirectory, artifactType, context.Tracer, numBuildArtifacts); } return(file); }