Permission denied creating /go directory in Jenkins when building a project that uses go modules

9.2k views Asked by At

When I try to build a go Jenkins project that uses go modules, I see this error:

go: writing go.mod cache: mkdir /go: permission denied

I also see a similar error for every import:

project-name/cmd imports
    github.com/antonfisher/nested-logrus-formatter: mkdir /go: permission denied
project-name/cmd imports
    github.com/gorilla/mux: mkdir /go: permission denied
project-name/cmd imports
    github.com/pkg/errors: mkdir /go: permission denied
project-name/cmd imports
    github.com/sirupsen/logrus: mkdir /go: permission denied
project-name/cmd imports
    github.com/spf13/cobra: mkdir /go: permission denied
project-name/cmd imports
    github.com/spf13/viper: mkdir /go: permission denied
project-name/cmd imports
    k8s.io/api/core/v1: mkdir /go: permission denied
project-name/cmd/cli/backup imports
    github.com/vmware-tanzu/velero/pkg/apis/velero/v1: mkdir /go: permission denied
project-name/cmd/cli/backup imports
    github.com/vmware-tanzu/velero/pkg/backup: mkdir /go: permission denied
project-name/cmd/cli/backup imports
    github.com/vmware-tanzu/velero/pkg/builder: mkdir /go: permission denied
project-name/cmd/cli/backup imports
    k8s.io/apimachinery/pkg/apis/meta/v1: mkdir /go: permission denied
project-name/cmd/cli/csisnaprestore imports
    github.com/gosuri/uitable: mkdir /go: permission denied
project-name/cmd/cli/csisnaprestore imports
    github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1: mkdir /go: permission denied
project-name/cmd/cli/csisnaprestore imports
    k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset: mkdir /go: permission denied
project-name/cmd/cli/csisnapshot imports
    k8s.io/apimachinery/pkg/apis/meta/v1/unstructured: mkdir /go: permission denied
project-name/cmd/cli/csisnapshot imports
    k8s.io/apimachinery/pkg/util/wait: mkdir /go: permission denied
project-name/cmd/cli/initz imports
    k8s.io/api/apps/v1: mkdir /go: permission denied
project-name/cmd/cli/px/appbackup imports
    github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1: mkdir /go: permission denied
project-name/cmd/cli/px/credentials imports
    github.com/libopenstorage/openstorage/api: mkdir /go: permission denied
project-name/cmd/cli/repository imports
    gocloud.dev/blob: mkdir /go: permission denied
project-name/cmd/cli/snaprestore imports
    k8s.io/apimachinery/pkg/labels: mkdir /go: permission denied
project-name/cmd/cli/snapshot imports
    k8s.io/apimachinery/pkg/runtime/schema: mkdir /go: permission denied
project-name/cmd/cli/snapshot imports
    k8s.io/client-go/dynamic: mkdir /go: permission denied
project-name/cmd/cli/veleroutil imports
    k8s.io/apimachinery/pkg/api/errors: mkdir /go: permission denied
project-name/cmd/cli/version imports
    k8s.io/apimachinery/pkg/util/intstr: mkdir /go: permission denied
project-name/pkg/cli imports
    github.com/go-openapi/swag: mkdir /go: permission denied
project-name/pkg/cli imports
    github.com/spf13/pflag: mkdir /go: permission denied
project-name/pkg/cli imports
    k8s.io/client-go/kubernetes: mkdir /go: permission denied
project-name/pkg/cli imports
    k8s.io/client-go/rest: mkdir /go: permission denied
project-name/pkg/cli imports
    k8s.io/client-go/tools/clientcmd: mkdir /go: permission denied
project-name/pkg/cli imports
    k8s.io/client-go/tools/clientcmd/api: mkdir /go: permission denied
project-name/pkg/core/framework imports
    github.com/hashicorp/go-version: mkdir /go: permission denied
project-name/pkg/core/framework imports
    k8s.io/api/batch/v1: mkdir /go: permission denied
project-name/pkg/core/framework imports
    k8s.io/api/batch/v1beta1: mkdir /go: permission denied
project-name/pkg/core/framework imports
    k8s.io/apimachinery/pkg/util/rand: mkdir /go: permission denied
project-name/pkg/core/loader imports
    k8s.io/apimachinery/pkg/util/validation: mkdir /go: permission denied
project-name/pkg/hook imports
    k8s.io/client-go/kubernetes/scheme: mkdir /go: permission denied
project-name/pkg/hook imports
    k8s.io/client-go/tools/remotecommand: mkdir /go: permission denied
project-name/pkg/ioutil imports
    github.com/gofrs/flock: mkdir /go: permission denied
project-name/pkg/ioutil imports
    golang.org/x/sys/unix: mkdir /go: permission denied
project-name/pkg/kube imports
    github.com/ghodss/yaml: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/api/storage/v1: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/apimachinery/pkg/api/meta: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/apimachinery/pkg/api/resource: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/apimachinery/pkg/fields: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/apimachinery/pkg/runtime: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/apimachinery/pkg/types: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/client-go/informers: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/client-go/tools/cache: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/client-go/tools/portforward: mkdir /go: permission denied
project-name/pkg/kube imports
    k8s.io/client-go/transport/spdy: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/aws: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/aws/credentials: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/aws/endpoints: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/aws/session: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/service/s3: mkdir /go: permission denied
project-name/pkg/objectstore imports
    github.com/aws/aws-sdk-go/service/s3/s3manager: mkdir /go: permission denied
project-name/pkg/objectstore imports
    gocloud.dev/blob/fileblob: mkdir /go: permission denied
project-name/pkg/objectstore imports
    gocloud.dev/blob/s3blob: mkdir /go: permission denied
project-name/pkg/px imports
    github.com/libopenstorage/openstorage/api/client: mkdir /go: permission denied
project-name/pkg/px imports
    github.com/libopenstorage/openstorage/api/client/cluster: mkdir /go: permission denied
project-name/pkg/px imports
    github.com/libopenstorage/openstorage/api/client/volume: mkdir /go: permission denied
project-name/pkg/px imports
    github.com/libopenstorage/openstorage/cluster: mkdir /go: permission denied
project-name/pkg/px imports
    github.com/libopenstorage/openstorage/volume: mkdir /go: permission denied
project-name/pkg/px imports
    google.golang.org/grpc: mkdir /go: permission denied
project-name/pkg/px imports
    google.golang.org/grpc/credentials: mkdir /go: permission denied
project-name/pkg/px imports
    google.golang.org/grpc/status: mkdir /go: permission denied
project-name/pkg/quiesce imports
    github.com/deckarep/golang-set: mkdir /go: permission denied
project-name/pkg/quiesce imports
    github.com/drone/envsubst: mkdir /go: permission denied
project-name/pkg/stork imports
    github.com/libopenstorage/stork/pkg/client/clientset/versioned/typed/stork/v1alpha1: mkdir /go: permission denied
project-name/pkg/utils imports
    k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1: mkdir /go: permission denied
project-name/pkg/velero imports
    github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned: mkdir /go: permission denied

I am using the Go Jenkins plugin to change the Go version.

Here is how I am using it in a Jenkins pipeline:

def root = tool name: 'Go 1.14', type: 'go'

withEnv(["GOROOT=${root}", "PATH+GO=${root}/bin", "GOBIN=${root}/bin"]) {
  sh 'go version'
  sh './build.sh'
}

Note: ./build.sh builds the project using modules

What is happening, and how can I get my build to work?

1

There are 1 answers

0
AlexPogue On

The issue was that GOPATH is still defined as /go. The build process tries to create this directory, but Jenkins does not have access to create it.

To resolve this issue, define GOPATH in your withEnv call, so the final withEnv code block would look like:

withEnv(["GOROOT=${root}", "PATH+GO=${root}/bin", "GOBIN=${root}/bin", "GOPATH=${root}/go"]) {
  sh 'go version'
  sh './build.sh'
}

The documentation did not mention defining "GOPATH" here, so it took me a few days to discover this.