microsoft / azure-pipelines-agent

Azure Pipelines Agent 🚀
MIT License
1.72k stars 865 forks source link

[enhancement]: Environment variable pointing to the host root path for docker agents #4810

Open bdovaz opened 4 months ago

bdovaz commented 4 months ago

Describe your feature request here

I have set up a self-hosted agent in a Docker container.

And since I have pipelines using MegaLinter, I am running into this problem.

I miss more information in that section as no concrete examples come out. I mean, to solve this problem in the best possible way and that the pipeline works in any agent, I don't want to put an absolute hardcoded path to the host obviously...

I have the problem that among the available environment variables there is not one that solves the root path of the agent in the host to be able to build the relative path as for example: $AZP_HOST_ROOT/_work/1/s.

So I had to create that AZP_HOST_ROOT variable on my own pointing to the corresponding path and then do all this scripting in powershell:

$originalWorkDirectoryPath = '$(Agent.WorkFolder)'
$originalArtifactStagingDirectoryPath = '$(Build.ArtifactStagingDirectory)'
$originalSourcesDirectoryPath = '$(Build.SourcesDirectory)'

$relativeArtifactStagingDirectoryPath = $originalArtifactStagingDirectoryPath.Replace($originalWorkDirectoryPath, '')
$relativeSourcesDirectoryPath = $originalSourcesDirectoryPath.Replace($originalWorkDirectoryPath, '')

$hostDirectoryPath = $Env:AZP_HOST_ROOT
$workDirectoryPath = [System.IO.Path]::Combine($hostDirectoryPath , $Env:AZP_WORK)
$artifactsStagingDirectoryPath = [System.IO.Path]::Combine($workDirectoryPath, $relativeArtifactStagingDirectoryPath)
$sourcesDirectoryPath = [System.IO.Path]::Combine($workDirectoryPath, $relativeSourcesDirectoryPath)

Would it be possible for you to expose such an environment variable or is it something that you will not want to support in any way? Because even if so it would be nice to at least have an example of how to solve this case? As I say, in this section I see that more useful information is missing...

It may also make more sense to create an AZP_HOST_WORK variable because when you mount volumes it makes more sense to do it from the work folder which makes more sense to persist.

DmitriiBobreshev commented 4 months ago

Hi @bdovaz, thank you for the good idea! For now, we have more prioritized issues but we'll try to implement it in the feature!