SSH and SCP Fail Deploy to Remote Server

I have Jenkins 2.426.2 installed.
I have a problem with the Jenkins pipeline. The problem is that when the deploy with docker-compose stage always fails with a log like this:

[Pipeline] { (Deploy with Docker Compose)
[Pipeline] script
[Pipeline] {
[Pipeline] sshagent
[ssh-agent] Using credentials (ssh-server)
[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-XXXXXXOyEYx9/agent.116515
SSH_AGENT_PID=116518
Running ssh-add (command line suppressed)
Identity added: /var/lib/jenkins/workspace/testbuild@tmp/private_key_1581090813655xxxxxxx.key (user@LAPTOP-82KEP79T)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
sed -i s/latest/60/g deployment/docker-compose.yml
[Pipeline] sh
scp deploy/docker-compose.yml user@ipaddress:/home/user/testbuild
Host key verification failed.
lost connection
[Pipeline] }
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 116518 killed;
[ssh-agent] Stopped.
[Pipeline] // sshagent
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stages
[Pipeline] { (Prune docker system on jenkins server )

I have added SSH credentials with private keys in Jenkins and have also installed plugins such as the SSH plugin and the SCP plugin. I have allowed the ssh port on the firewall, I have also run ssh-keyscan, and of course I have also generated ssh-keygen on my laptop and I have also pasted my laptop’s id_rsa.pub into the.ssh/authorized_keys directory on the remote server. I’ve also tried doing scp from the laptop I use to the remote server, and the results showed no problems. But why is it that when it is applied using the Jenkinsfile pipeline, an error appears as I mentioned above.

This is the contents of my jenkinsfile pipeline which I got from the internet and modified:

stage ('Deploy with Docker Compose') {
    steps {
        script {
                    sshagent(credentials: ['ssh-server']) { 
                    sh "sed -i 's/latest/$BUILD_NUMBER/g' deployment/docker-compose.yml"
                    sh "scp deployment/docker-compose.yml user@ipaddress:/home/user/testbuild"
                    def remoteCommand = "ssh -o StrictHostKeyChecking=no user@ipaddress 'cd /home/user/testbuild && docker compose up -d'"
                    def remoteStatus = sh(script: remoteCommand, returnStatus: true)
                 }
            }
      }
}

for information, my jenkins is running on WSL2 Ubuntu 22.04

Thank you

you might want to add -o StrictHostKeyChecking=no to the scp command as well
sh "scp -o StrictHostKeyChecking=no deployment/docker-compose.yml user@ipaddress:/home/user/testbuild"

Thank you for your advice. The problem has been solved after the 65th build attempt :face_with_spiral_eyes: :man_facepalming:t2: :crossed_fingers:t2:

Have a nice day :beers: