I am trying to build a .NET application using Jenkins. The Jenkins instance is running in a Docker container.
My Jenkinsfile is as follows:
pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
registryUrl 'https://index.docker.io/v1/'
}
}
stages {
stage('Build') {
steps {
sh 'dotnet build MyApplication/Application.csproj -c Release -o /app'
}
}
stage('Test') {
steps {
sh 'dotnet test MyApplication/Application.csproj -c Release -r /results'
}
}
}
}
When I attempt the build I am seeing the following error in the Jenkins build output:
System.UnauthorizedAccessException: Access to the path '/.dotnet' is denied. ---> System.IO.IOException: Permission denied
--- End of inner exception stack trace ---
at System.IO.FileSystem.CreateDirectory(String fullPath)
at System.IO.Directory.CreateDirectory(String path)
at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateDirectory(String path)
at Microsoft.DotNet.Configurer.FileSentinel.Create()
at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.Configure()
at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel, IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel, IAspNetCertificateSentinel aspNetCertificateSentinel, IFileSentinel toolPathSentinel, Boolean hasSuperUserAccess, DotnetFirstRunConfiguration dotnetFirstRunConfiguration, IEnvironmentProvider environmentProvider)
at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
at Microsoft.DotNet.Cli.Program.Main(String[] args)
It appears that the '.dotnet' folder is protected in the Docker container. Is there a way of getting read/write permission on this, or a way of changing it's location? I can't seem to find the folder when I bash into the container.
Thanks for any help.
Launch Jenkins in a Docker container. Install additional tools and plugins. Pass Java system properties and Jenkins application arguments. Backup the Docker volume.
You can set the HOME
environment variable as @colmulhall suggested, but then you will set the docker container home directory to /tmp
.
To do it in "dotnet"
way set the environment variable DOTNET_CLI_HOME
:
environment {
DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}
Or before calling dotnet
run:
export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"
A sample Jenkins pipeline code taken from https://www.jenkins.io/doc/pipeline/tour/environment/
Look how DOTNET_CLI_HOME
is defined in the environment
section:
pipeline {
agent {
label '!windows'
}
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}
stages {
stage('Build') {
steps {
echo "Database engine is ${DB_ENGINE}"
echo "DISABLE_AUTH is ${DISABLE_AUTH}"
sh 'printenv'
}
}
}
}
There are many ways to achieve this. If you are using docker, maybe a better way is defining the environment variable DOTNET_CLI_HOME
in the docker image.
The issue appeared to be linked to trying to write data to the top level of the Docker container ('/').
Adding the following to the Jenkinsfile ensures that the home directory is set and the .dotnet folder can be created in a location with correct permissions.
environment {
HOME = '/tmp'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With