Home > OS >  Jenkins scp permission denied for file on Jenkins server
Jenkins scp permission denied for file on Jenkins server

Time:05-13

I'm trying to copy a file (created during SCM Checkout) from my Jenkins agent to a remote server. I have set up SSH to the remote server, and I'm able to connect to it. I'm getting a "permission denied" error for the file on the Jenkins agent (not the remote host).

I have checked the ownership of the file, and also tried changing the permissions, but this hasn't worked.

My pipeline:

pipeline {
agent { label projectName }

stages {
    
    stage("SCM: Checkout"){
        steps{
            script{
                def gitURL="https://xxx.xxxxx.net/bitbucket/scm/project/text_preprocessor.git"

                log.outputBanner("Checking out from the SCM")
                scmSteps.cloneRepository(gitURL, env.BRANCH_NAME, credentialsId)
                sh "ls -l"
                sh "chmod  x docker-compose.yml" //changing permissions
                sh "ls -l"
                
            }
        }
    }
    
    stage("Copy files") {
        steps {
            script{
                sshagent (credentials: [prodServerCredentialId]) {
                    sh "ssh -o StrictHostKeyChecking=no -l user0 host.xxx.net 'ls'"
                    sh '''
                    scp -o StrictHostKeyChecking=no docker-compose.yml [email protected]:/
                    '''
                    sh "ssh -o StrictHostKeyChecking=no -l user0 host.xxx.net 'ls'"
                }                   
            }  
        }
    }    
}  

The output of the build shows the following (some info parts removed for clarity):

[Pipeline] checkout
...
[Pipeline] sh
  ls -l
total 20
drwxrwxr-x 2 user1 user1 4096 May  9 13:38 __pycache__
-rw-rw-r-- 1 user1 user1  385 May  9 13:38 docker-compose.prod.yml
-rw-rw-r-- 1 user1 user1  214 May  9 13:38 docker-compose.yml
drwxrwxr-x 3 user1 user1 4096 May  9 13:38 jenkinsfiles
drwxrwxr-x 4 user1 user1 4096 May  9 13:38 services
[Pipeline] sh
...
  chmod  x docker-compose.yml
[Pipeline] sh
  ls -l
total 20
drwxrwxr-x 2 user1 user1 4096 May  9 13:38 __pycache__
-rw-rw-r-- 1 user1 user1  385 May  9 13:38 docker-compose.prod.yml
-rwxrwxr-x 1 user1 user1  214 May  9 13:38 docker-compose.yml
drwxrwxr-x 3 user1 user1 4096 May  9 13:38 jenkinsfiles
drwxrwxr-x 4 user1 user1 4096 May  9 13:38 services
...


[Pipeline] sshagent
[ssh-agent] Using credentials user0 (Prod)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-FQIEn7xpUfkE/agent.608892
SSH_AGENT_PID=608894
Running ssh-add (command line suppressed)
...
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
  ssh -o StrictHostKeyChecking=no -l user0 host.xxx.net ls
bin
docker-compose
[Pipeline] sh
  scp -o StrictHostKeyChecking=no docker-compose.yml [email protected]:/
scp: /docker-compose.yml: Permission denied
[Pipeline] }

CodePudding user response:

It turns out that the permission error was at the remote host. The problem was that I was trying to write to a location on the remote host that I did not have permission to write to. With [email protected]:/ I was trying to write to the root of the remote host. I created a temp folder in the home folder of my user on the remote server (user0) and successfully copied the file there by changing:

scp -o StrictHostKeyChecking=no docker-compose.yml [email protected]:/

to

scp -o StrictHostKeyChecking=no docker-compose.yml [email protected]:/home/user0/temp

  • Related