It’s very easy to use Azure DevOps Cache Task with Microsoft Hosted agent. For selfhost agent, the document only mentioned the prerequies software in Required software on self-hosted agent.
Archive software / Platform | Windows | Linux | Mac |
---|---|---|---|
GNU Tar | Required | Required | No |
BSD Tar | No | No | Required |
7-Zip | Recommended | No | No |
Description
From above table, it tells us that we must install GNU Tar on Windows and 7-Zip is recommended. Then we will install GNU Tar and 7-Zip in selfhost agent and add the path to PATH environment variable.
In Cache task will use 7-zip (only if 7z is installed and is part of PATH variable) to extract the TAR file on Windows. It will try to launch 7z.exe to check 7z exists. If failed, it will fall back to GNU Tar.
private static ProcessStartInfo GetExtractStartProcessInf(AgentTaskPluginExecutionContext context, stringtargetDirectory)
{
string processFileName, processArguments;
if (isWindows && CheckIf7ZExists())
{
processFileName = "7z";
processArguments = $"x -si -aoa -o\"{targetDirectory}\" -ttar";
if (context.IsSystemDebugTrue())
{
processArguments = "-bb1 " + processArguments;
}
}
else
{
processFileName = GetTar(context);
processArguments = $"-xf - -C ."; // Instead of targetDirectory, we are providing . to tar, because the tar process is being started from targetDirectory.
if (context.IsSystemDebugTrue())
{
processArguments = "-v " + processArguments;
}
}
ProcessStartInfo processStartInfo = new ProcessStartInfo();
CreateProcessStartInfo(processStartInfo, processFileName, processArguments, processWorkingDirectory: targetDirectory);
return processStartInfo;
}
In Post Cache Task, it will use Tar.exe to create Tar archive.
private static ProcessStartInfo GetCreateTarProcessInf(AgentTaskPluginExecutionContext context, stringarchiveFileName, string inputPath)
{
var processFileName = GetTar(context);
inputPath = inputPath.TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar);
var processArguments = $"-cf \"{archiveFileName}\" -C \"{inputPath}\" ."; // If given the absolute path for the '-cf' option, the GNU tar fails. The workaround is to start the tarring process in the temp directory, and simply speficy 'archive.tar' for that option.
if (context.IsSystemDebugTrue())
{
processArguments = "-v " + processArguments;
}
if (isWindows)
{
processArguments = "-h " + processArguments;
}
ProcessStartInfo processStartInfo = new ProcessStartInfo();
CreateProcessStartInfo(processStartInfo, processFileName, processArguments, processWorkingDirectory: Path.GetTempPath()); // We want to create the archiveFile in temp folder, and hence starting the tar process from TEMP to avoid absolute paths in tar cmd line.
return processStartInfo;
}
How to Setup Selfhost Agent for Cache Task
- Install 7-zip and GNU Tar
- Add C:\Program Files\7-Zip and C:\Program Files (x86)\GnuWin32\bin to PATH variable in System Environment Variables.
- Add VSTS_TAR_EXECUTABLE environment variable and set its value to C:\Program Files (x86)\GnuWin32\bin\tar.exe.
Notes:System will add a quote for Windows 10 above OS. We should mannually remove the quote by Edit Text of PATH;
文档信息
- 本文作者:Robin Chen
- 本文链接:https://crushonme.github.io/2022/04/12/How-To-Use-Cache-Task-With-Selfhost-Agent/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享4.0许可证)
Document Information
- Author :Robin Chen
- Link : https://crushonme.github.io/2022/04/12/How-To-Use-Cache-Task-With-Selfhost-Agent/
- Copyright:Attribution-NonCommercial-NoDerivatives-No additional restrictions(CC BY-NC-ND 4.0)