public void MarkForGarbageCollection(IExecutionContext executionContext, TrackingConfigBase2 config) { Trace.Entering(); // Convert legacy format to the new format. LegacyTrackingConfig2 legacyConfig = config as LegacyTrackingConfig2; if (legacyConfig != null) { // Convert legacy format to the new format. config = new TrackingConfig2( executionContext, legacyConfig, // The sources folder wasn't stored in the legacy format - only the // build folder was stored. Since the hash key has changed, it is // unknown what the source folder was named. Just set the folder name // to "s" so the property isn't left blank. sourcesDirectoryNameOnly: Constants.Build.Path.SourcesDirectory); } // Write a copy of the tracking config to the GC folder. string gcDirectory = Path.Combine( IOUtil.GetWorkPath(HostContext), Constants.Build.Path.SourceRootMappingDirectory, Constants.Build.Path.GarbageCollectionDirectory); string file = Path.Combine( gcDirectory, StringUtil.Format("{0}.json", Guid.NewGuid())); WriteToFile(file, config); }
public void UpdateJobRunProperties(IExecutionContext executionContext, TrackingConfig2 config, string file) { Trace.Entering(); // Update the info properties and save the file. config.UpdateJobRunProperties(executionContext); WriteToFile(file, config); }
private TrackingConfig2 ConvertToNewFormat( IExecutionContext executionContext, ServiceEndpoint endpoint, TrackingConfigBase2 config) { Trace.Entering(); // If it's already in the new format, return it. TrackingConfig2 newConfig = config as TrackingConfig2; if (newConfig != null) { return(newConfig); } // Delete the legacy artifact/staging directories. LegacyTrackingConfig2 legacyConfig = config as LegacyTrackingConfig2; DeleteDirectory( executionContext, description: "legacy artifacts directory", path: Path.Combine(legacyConfig.BuildDirectory, Constants.Build.Path.LegacyArtifactsDirectory)); DeleteDirectory( executionContext, description: "legacy staging directory", path: Path.Combine(legacyConfig.BuildDirectory, Constants.Build.Path.LegacyStagingDirectory)); // Determine the source directory name. Check if the directory is named "s" already. // Convert the source directory to be named "s" if there is a problem with the old name. string sourcesDirectoryNameOnly = Constants.Build.Path.SourcesDirectory; if (!Directory.Exists(Path.Combine(legacyConfig.BuildDirectory, sourcesDirectoryNameOnly)) && !String.Equals(endpoint.Name, Constants.Build.Path.ArtifactsDirectory, StringComparison.OrdinalIgnoreCase) && !String.Equals(endpoint.Name, Constants.Build.Path.LegacyArtifactsDirectory, StringComparison.OrdinalIgnoreCase) && !String.Equals(endpoint.Name, Constants.Build.Path.LegacyStagingDirectory, StringComparison.OrdinalIgnoreCase) && !String.Equals(endpoint.Name, Constants.Build.Path.TestResultsDirectory, StringComparison.OrdinalIgnoreCase) && !endpoint.Name.Contains("\\") && !endpoint.Name.Contains("/") && Directory.Exists(Path.Combine(legacyConfig.BuildDirectory, endpoint.Name))) { sourcesDirectoryNameOnly = endpoint.Name; } // Convert to the new format. newConfig = new TrackingConfig2( executionContext, legacyConfig, sourcesDirectoryNameOnly, // The legacy artifacts directory has been deleted at this point - see above - so // switch the configuration to using the new naming scheme. useNewArtifactsDirectoryName: true); return(newConfig); }
public TrackingConfig2 Create( IExecutionContext executionContext, string repositoryUrl, string hashKey, string file) { Trace.Entering(); // Get or create the top-level tracking config. TopLevelTrackingConfig topLevelConfig; string topLevelFile = Path.Combine( IOUtil.GetWorkPath(HostContext), Constants.Build.Path.SourceRootMappingDirectory, Constants.Build.Path.TopLevelTrackingConfigFile); Trace.Verbose($"Loading top-level tracking config if exists: {topLevelFile}"); if (!File.Exists(topLevelFile)) { topLevelConfig = new TopLevelTrackingConfig(); } else { topLevelConfig = JsonConvert.DeserializeObject <TopLevelTrackingConfig>( value: File.ReadAllText(topLevelFile)); } // Update the top-level tracking config. topLevelConfig.LastBuildDirectoryCreatedOn = DateTimeOffset.Now; topLevelConfig.LastBuildDirectoryNumber++; WriteToFile(topLevelFile, topLevelConfig); // Create the new tracking config. TrackingConfig2 config = new TrackingConfig2( executionContext, repositoryUrl, topLevelConfig.LastBuildDirectoryNumber, hashKey); WriteToFile(file, config); return(config); }
private void InitializeAgent(IExecutionContext executionContext, out bool skipArtifactsDownload, out Guid teamProjectId, out string artifactsWorkingFolder, out int pipelineInstanceId) { Trace.Entering(); teamProjectId = executionContext.Variables.GetGuid(Constants.Variables.System.TeamProjectId) ?? Guid.Empty; string definitionType = executionContext.Variables.Get("Pipeline.DefinitionType"); if (string.Equals(definitionType, "Build", StringComparison.OrdinalIgnoreCase)) { pipelineInstanceId = executionContext.Variables.GetInt(WellKnownBuildVariables.BuildId) ?? 0; string buildDefinitionName = executionContext.Variables.Get(WellKnownBuildVariables.DefinitionName); executionContext.Output($"BuildId={pipelineInstanceId}, TeamProjectId={teamProjectId}, ReleaseDefinitionName={buildDefinitionName}"); skipArtifactsDownload = executionContext.Variables.GetBoolean(Constants.Variables.Release.SkipArtifactsDownload) ?? false; string repositoryUrl = executionContext.Variables.Get(WellKnownBuildVariables.RepoUri); var directoryManager = HostContext.GetService <IBuildDirectoryManager2>(); TrackingConfig2 trackingConfig = directoryManager.PrepareDirectory(executionContext, repositoryUrl); string workDirectory = IOUtil.GetWorkPath(HostContext); executionContext.Variables.Set(Constants.Variables.Agent.BuildDirectory, Path.Combine(workDirectory, trackingConfig.BuildDirectory)); executionContext.Variables.Set(Constants.Variables.System.ArtifactsDirectory, Path.Combine(workDirectory, trackingConfig.ArtifactsDirectory)); executionContext.Variables.Set(Constants.Variables.System.DefaultWorkingDirectory, Path.Combine(workDirectory, trackingConfig.SourcesDirectory)); executionContext.Variables.Set(Constants.Variables.Common.TestResultsDirectory, Path.Combine(workDirectory, trackingConfig.TestResultsDirectory)); executionContext.Variables.Set(Constants.Variables.Build.BinariesDirectory, Path.Combine(workDirectory, trackingConfig.BuildDirectory, Constants.Build.Path.BinariesDirectory)); executionContext.Variables.Set(Constants.Variables.Build.SourcesDirectory, Path.Combine(workDirectory, trackingConfig.SourcesDirectory)); executionContext.Variables.Set(Constants.Variables.Build.StagingDirectory, Path.Combine(workDirectory, trackingConfig.ArtifactsDirectory)); executionContext.Variables.Set(Constants.Variables.Build.ArtifactStagingDirectory, Path.Combine(workDirectory, trackingConfig.ArtifactsDirectory)); executionContext.Variables.Set(Constants.Variables.Build.RepoLocalPath, Path.Combine(workDirectory, trackingConfig.SourcesDirectory)); artifactsWorkingFolder = executionContext.Variables.Get(Constants.Variables.System.DefaultWorkingDirectory); } else { var directoryManager = HostContext.GetService <IReleaseDirectoryManager>(); pipelineInstanceId = executionContext.Variables.GetInt(Constants.Variables.Release.ReleaseId) ?? 0; skipArtifactsDownload = executionContext.Variables.GetBoolean(Constants.Variables.Release.SkipArtifactsDownload) ?? false; string releaseDefinitionName = executionContext.Variables.Get(Constants.Variables.Release.ReleaseDefinitionName); // TODO: Should we also write to log in executionContext.Output methods? so that we don't have to repeat writing into logs? // Log these values here to debug scenarios where downloading the artifact fails. executionContext.Output($"ReleaseId={pipelineInstanceId}, TeamProjectId={teamProjectId}, ReleaseDefinitionName={releaseDefinitionName}"); var releaseDefinition = executionContext.Variables.Get(Constants.Variables.Release.ReleaseDefinitionId); if (string.IsNullOrEmpty(releaseDefinition)) { string pattern = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); Regex regex = new Regex(string.Format("[{0}]", Regex.Escape(pattern))); releaseDefinition = regex.Replace(releaseDefinitionName, string.Empty); } var releaseDefinitionToFolderMap = directoryManager.PrepareArtifactsDirectory( IOUtil.GetWorkPath(HostContext), executionContext.Variables.System_CollectionId, executionContext.Variables.System_TeamProjectId.ToString(), releaseDefinition); artifactsWorkingFolder = Path.Combine( IOUtil.GetWorkPath(HostContext), releaseDefinitionToFolderMap.ReleaseDirectory, Constants.Release.Path.ArtifactsDirectory); executionContext.Output($"Release folder: {artifactsWorkingFolder}"); SetLocalVariables(executionContext, artifactsWorkingFolder); // Log the environment variables available after populating the variable service with our variables LogEnvironmentVariables(executionContext); if (skipArtifactsDownload) { // If this is the first time the agent is executing a task, we need to create the artifactsFolder // otherwise Process.StartWithCreateProcess() will fail with the error "The directory name is invalid" // because the working folder doesn't exist CreateWorkingFolderIfRequired(executionContext, artifactsWorkingFolder); // log the message that the user chose to skip artifact download and move on executionContext.Output(StringUtil.Loc("RMUserChoseToSkipArtifactDownload")); Trace.Info("Skipping artifact download based on the setting specified."); } } }