Go build failed

Hi,
I’m working on a project about Jenkins pipeline but after last stage i’ve an error "failed to initialize build cache at /.cache/go-build: mkdir /.cache: permission denied"
Please help

My Jenkinsfile

    #!/usr/bin/env groovy
   // The above line is used to trigger correct syntax highlighting.
   pipeline {
   agent any    
    stages {
    stage('Build & Test') {   
        // Use golang.
        agent { docker { image 'golang' } }

        steps {                                           
            // Create our project directory.
            sh 'cd ${GOPATH}/src'
            sh 'mkdir -p ${GOPATH}/src/PROJECT_DEVOPS'

            // Copy all files in our Jenkins workspace to our project directory.                
            sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/PROJECT_DEVOPS'
            // Build the app.
            sh 'go build'     
            
        }            
    }
}

}

My first guess is that the Jenkins worker runs in an environment that has neither XDG_CACHE_DIR, nor HOME set, so as XDG isn’t set, $HOME/.cache is used as fallback, though as HOME isn’t set as well, it expands to /.cache where usually no one’s has write access.

Though this is just an assumption based on XDG spec.

Yes i think the same thing but i don’t know what i can do

Setting those variables to a location that would be writable.

Sincerely ,I’m a junior developper i know that you say but i don’t how get it on Jenkins

I don’t know Jenkins as well, though if you are a junior, you should have an assigned senior who’s job it is to help you with exactly this stuff.

As a quick workaround you could just use export in the shell script you run.

This isn’t an exact answer but hopefully helps… You have at least two options. One is to set GOCACHE="${WORKSPACE}" (the Jenkins name for your per-build writable area). You used to be able to say GOCACHE=off but can’t since 1.12.

You might also benefit from using dir(), something like:

run: {
runSafely ‘mkdir src’
dir(‘src’) {
git(url: ‘git@github.com:your/project.git’, branch: ‘master’,
credentialsId: yourCredentialsId())
sh “./scripts/build”
}
},

Then have your build script do whatever you need, since it can write inside the dir().

hth