static void Main(string[] args)
        {
            using (var log = File.AppendText(LogFilename))
            {
                try
                {
                    _log           = log;
                    _log.AutoFlush = true;
                    WriteLog       = _writeLog;
                    WriteError     = _writeError;
                    _writeLog($"Beginning New Publishing Run with args: {string.Join(", ", args)}");

                    var storageKey             = args[0];
                    var targetFolder           = new DirectoryInfo(args[1]);
                    var includeAntecendents    = TryParseAsBool(args[2]);
                    var includeDescendents     = TryParseAsBool(args[3]);
                    var gitCommitSha           = args[4];
                    var buildImages            = TryParseAsBool(args[5]);
                    var pushImages             = TryParseAsBool(args[6]);
                    var outputFullMetadataFile = TryParseAsBool(args[7]);

                    var blobContainer = _buildBlobClient(buildImages, StorageAccountName, storageKey, StoragContainerName); //NOTE blobContainer will be null if !buildImages

                    var containerImagePayload = DirectoryTraversal.BuildFullPayloadFromDirectoryTree(targetFolder, includeAntecendents, includeDescendents);

                    _writePrePublishLog(containerImagePayload);
                    var imageNumber      = 1;
                    var latestImages     = new List <string>();
                    var imagesWithShaTag = new List <string>();

                    if (buildImages)
                    {
                        foreach (var imageDef in containerImagePayload.Select(x => x.ContainerImageDefinition))
                        {
                            _writeLog($"Publishing #{imageNumber++} of {containerImagePayload.Count} - {imageDef.ContainerImage}");

                            dynamic blobProperties =
                                _getBlobUriWithSasTokenAndMD5(imageDef.InstallerFileBlob, blobContainer);

                            var localImageId = _buildImage(imageDef, blobProperties.blobSasToken);

                            string tag = ImageTagging._fetchImageTag(blobProperties.blobMD5, gitCommitSha);

                            foreach (var imageTag in new [] { tag, "latest" })
                            {
                                DockerCommands._runDockerTag(imageDef, localImageId, imageTag);
                            }

                            var imageWithGeneratedTag = $"{imageDef.ContainerImage}:{tag}";
                            imagesWithShaTag.Add(imageWithGeneratedTag);
                            _writeLog($"Successfully built and tagged {imageWithGeneratedTag}");

                            if (pushImages)
                            {
                                DockerCommands._runDockerPush(imageDef, tag);
                                _writeLog($"Successfully published {imageWithGeneratedTag}\n");
                            }

                            latestImages.Add($"{imageDef.ContainerImage}:latest");
                        }
                        OutputFileWriter._outputTestFiles(containerImagePayload, imagesWithShaTag);
                        OutputFileWriter._outputTaggedImagesFile(imagesWithShaTag);
                        OutputFileWriter._outputLatestImagesFile(latestImages);
                        OutputFileWriter._outputContainerImageMetadataFile(
                            containerImagePayload.Select(x => x.ContainerImageDefinition).ToList(),
                            outputFullMetadataFile);
                        _writeLog($"Completed Publishing Successfully!\n\n");
                    }
                    else
                    {
                        latestImages = containerImagePayload.Select(x => $"{x.ContainerImageDefinition.ContainerImage}:latest").ToList();
                        OutputFileWriter._outputTestFiles(containerImagePayload, latestImages);
                        _writeLog($"Completed Generating Test Files!\n\n");
                    }
                }

                catch (Exception ex)
                {
                    _writeError("Fatal Exception: " + ex);
                    throw ex;
                }
            }
        }
예제 #2
0
        public static int RunPublish(PublishOptions options)
        {
            try
            {
                var optionValues       = PrettyStringObjectProperties(options);
                var targetRelativeDirs = ParseTargetRelativeDirs(options.TargetRelativeDirs);

                _writeLog($"Beginning Publishing Run with environment:\n{string.Join("\n", optionValues)}");

                DockerInstallScriptsRootDir = options.DockerInstallScriptsRootDir;

                BatchExtensionTemplatesRootDir = options.BatchExtensionTemplatesRootDir;

                StorageAccountName = options.StorageAccountName;

                StorageContainerName = options.StorageContainerName;

                var testRepoRegistry = options.TestRepoBase.Split('/')[0];

                var blobContainer =
                    _buildBlobClient(StorageAccountName, options.StorageKey,
                                     StorageContainerName); //NOTE blobContainer will be null if !buildImages

                var containerImagePayload = DirectoryTraversal.BuildFullPayloadFromDirectoryTree(
                    options.AcrRepoBase,
                    options.TestRepoBase,
                    options.ProdRepoBase,
                    DockerInstallScriptsRootDir,
                    targetRelativeDirs,
                    options.IncludeAntecendentsBool,
                    options.IncludeDescendentsBool,
                    options.BuildVersion,
                    true);

                _writePrePublishLog(containerImagePayload);
                var imageNumber          = 1;
                var prodImages           = new List <string>();
                var testImages           = new List <string>();
                var builtContainerImages = new List <OutputContainerImage>();

                foreach (var payload in containerImagePayload)
                {
                    var     imageDef       = payload.ContainerImageDefinition;
                    dynamic blobProperties = _getBlobUriWithSasTokenAndMD5(imageDef.InstallerFileBlob, blobContainer);

                    string devTag = ImageTagging.BuildDevImageTag(options.ReleaseName, blobProperties.blobMD5, options.GitCommitSha, imageDef.VersionTag);

                    _writeLog($"Building #{imageNumber++} of {containerImagePayload.Count} \n" +
                              $" - Prod-ImageName: {imageDef.ProdRepoName}:{imageDef.VersionTag}\n" +
                              $" - PreProd-ImageName: {imageDef.AcrRepoName}:{imageDef.VersionTag}\n" +
                              $" - Test-RepoName: {imageDef.TestRepoName}:{devTag}\n");

                    var localImageId = _buildImage(options.AcrRepoBase, DockerInstallScriptsRootDir, imageDef,
                                                   blobProperties.blobSasToken);

                    var testImage = $"{imageDef.TestRepoName}:{devTag}";
                    DockerCommands._runDockerTag(localImageId, testImage);
                    testImages.Add(testImage);
                    _writeLog($"Successfully built and tagged {testImage}");

                    var prodImage = $"{imageDef.AcrRepoName}:{imageDef.VersionTag}";
                    DockerCommands._runDockerTag(localImageId, prodImage);
                    prodImages.Add(prodImage);
                    _writeLog($"Successfully built and tagged {prodImage}");

                    var outputImage = new OutputContainerImage
                    {
                        AcrRepoName        = imageDef.AcrRepoName,
                        TestRepoName       = imageDef.TestRepoName,
                        ProdRepoName       = imageDef.ProdRepoName,
                        Metadata           = imageDef.Metadata,
                        DevTag             = devTag,
                        VersionTag         = imageDef.VersionTag,
                        PathToDockerfile   = imageDef.PathToDockerFile,
                        Payload            = payload,
                        TestContainerImage = $"{imageDef.TestRepoName}:{devTag}"
                    };
                    builtContainerImages.Add(outputImage);

                    if (options.PushTestImagesBool)
                    {
                        DockerCommands._runDockerPush(outputImage.TestContainerImage);
                        _writeLog($"Successfully pushed {testImage}\n");
                    }
                }

                OutputFileWriter._outputTestFiles(options.TestRepoUsername, options.TestRepoPassword, testRepoRegistry,
                                                  BatchExtensionTemplatesRootDir, builtContainerImages, testImages);
                OutputFileWriter._outputTestImagesFile(testImages);
                OutputFileWriter._outputProdImagesFile(prodImages);
                OutputFileWriter._outputContainerImageMetadataFile(builtContainerImages,
                                                                   options.OutputFullMetadataFileBool);
                OutputFileWriter.UpdateVersionFiles(DockerInstallScriptsRootDir, builtContainerImages);

                _writeLog($"Completed Publishing Successfully!\n\n");
                return(0);
            }
            catch (Exception ex)
            {
                _writeError("Fatal Exception: " + ex);
                return(1);
            }
        }