Skip to content

Commit

Permalink
improve reliability of downloading tools (#208)
Browse files Browse the repository at this point in the history
Sometimes extracting failed due to volume mounts and moving the extracted folder between those volumes
  • Loading branch information
ChristopherHX authored Sep 5, 2023
1 parent b0d24dd commit cac948c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Runner.Common/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ public string GetDirectory(WellKnownDirectory directory)
path = Path.Combine(
GetDirectory(WellKnownDirectory.Root),
Constants.Path.ExternalsDirectory);
// No longer create the local externals folder if it doesn't exist
if(!new DirectoryInfo(path).Exists) {
path = Path.Combine(GitHub.Runner.Sdk.GharunUtil.GetLocalStorage());
}
#endif
break;

Expand Down
36 changes: 34 additions & 2 deletions src/Runner.Sdk/Util/IOUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,38 @@ public static void DeleteFile(string path)
}
}

private static bool IsChildPath(string relativeTo, string sourceDir) {
var relativePath = Path.GetRelativePath(relativeTo, sourceDir);
return relativePath != sourceDir && !relativePath.StartsWith("..");
}

private static DriveInfo GetDrive(DriveInfo[] allDrives, string sourceDir)
{
DriveInfo ret = null;
foreach (DriveInfo drive in allDrives)
{
if (IsChildPath(drive.RootDirectory.FullName, sourceDir) && (ret == null || IsChildPath(ret.RootDirectory.FullName, drive.RootDirectory.FullName)))
{
ret = drive;
}
}
return ret;
}

private static void MoveOrCopy(string sourceDir, string targetDir, CancellationToken token)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
if (GetDrive(allDrives, sourceDir) != GetDrive(allDrives, targetDir))
{
CopyDirectory(sourceDir, targetDir, token);
DeleteDirectory(sourceDir, token);
}
else
{
Directory.Move(sourceDir, targetDir);
}
}

public static void MoveDirectory(string sourceDir, string targetDir, string stagingDir, CancellationToken token)
{
ArgUtil.Directory(sourceDir, nameof(sourceDir));
Expand All @@ -217,7 +249,7 @@ public static void MoveDirectory(string sourceDir, string targetDir, string stag
Directory.CreateDirectory(Path.GetDirectoryName(stagingDir));

// move source to staging
Directory.Move(sourceDir, stagingDir);
MoveOrCopy(sourceDir, stagingDir, token);

// delete existing targetDir
DeleteDirectory(targetDir, token);
Expand All @@ -226,7 +258,7 @@ public static void MoveDirectory(string sourceDir, string targetDir, string stag
Directory.CreateDirectory(Path.GetDirectoryName(targetDir));

// move staging to target
Directory.Move(stagingDir, targetDir);
MoveOrCopy(stagingDir, targetDir, token);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.Worker/ExternalToolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public static async Task<string> GetNodeTool(IExecutionContext executionContext,
_tools = new Dictionary<string, Func<string, Task>> {
{ "windows/386", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "win", "x86", "zip"), Path.Combine(dest, "bin"), unwrap: true)},
{ "windows/amd64", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "win", "x64", "zip"), Path.Combine(dest, "bin"), unwrap: true)},
{ "windows/arm64", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, "16.6.2", "win", "arm64", "zip"), Path.Combine(dest, "bin"), unwrap: true)},
{ "windows/arm64", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "win", "arm64", "zip"), Path.Combine(dest, "bin"), unwrap: true)},
{ "linux/amd64", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "linux", "x64", "tar.gz"), dest, tarextraopts, true)},
{ "linux/arm", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "linux", "armv7l", "tar.gz"), dest, tarextraopts, true)},
{ "linux/arm64", dest => DownloadTool(executionContext, NodeOfficialUrl(nodeUrl, nodeVersion, "linux", "arm64", "tar.gz"), dest, tarextraopts, true)},
Expand Down

0 comments on commit cac948c

Please sign in to comment.