1

I'm a little bit of a newb, with this CI/CD container stuff so please correct me anywhere I'm wrong.
I can't seem to find out how to send by npm build files created on my jenkins instance (workspace) to a remote server. I have a pipeline that successfully pulls in my github repo, does all my fun npm stuff (npm install, test, build). I see my build dir in my jenkins instance /workspace.
My environment is as follows. We have a server where docker (with Portainer) is installed. Jenkins is running in a container with a volume mounted (my react build dir goes here). No issues with the pipeline or building etc. I just can't figure out how to push my artifacts from my jenkins workspace directory to my 'remote' dev server.

I can successfully open a console in my jenkins container (portainer as the jenkins user) and scp files from the workspace directory using my remote server creds(but password is necessary).

I installed and used "Publish Over SSH" Jenkins plugin and get a successful "Test Configuration" from my setup.
I created my RSA keys on the REMOTE machine (that I'm trying to push my build files to). I then pasted the private key (created without a password) into the plugin at the 'Use password authentication, or use a different key' section. Again, I get a successful test connection.

In my pipeline the last step is deploying and I use this command

sh 'scp -r build myusername@xx.xx.xx.xx:/var/files/react-tester'

I get a 'Permission denied (publickey,password).' error. I have no password associated with the rsa key. I tried both ways, creating the rsa key on the remote machine as my remote user, and the jenkins machine as the jenkins user. I've read examples of people creating the keys both ways, but not sure which user/machine combo to create the keys and paste to which section of the 'Publish Over SSH' plugin.
I'm out of ideas.

benishky
  • 901
  • 1
  • 11
  • 23

1 Answers1

0

First, go to "Manage Jenkins" > "Credentials", add a new SSH credential of type "SSH Username with private key" and fill the "Username" and your private key (generate one if you haven't done it yet) fields (you can also upload one). Don't forget that you have to copy the generated public key to the ${SSH_USERNAME}/.ssh/authorized_keys file on the remote server.

I'm assuming you're using a scripted or DSL pipeline here. In your code, after you've builded your application, you can push it to your server adding this step to your pipeline:

pipeline {

    stages {
        stage("Pushing changes to remote server") {
            steps {
                script {
                    def remote_server = "1.2.3.4"
                    withCredentials([sshUserPrivateKey(credentialsId: 'my-key', keyFileVariable: 'SSH_KEY', passphraseVariable: '', usernameVariable: 'SSH_USERNAME')]) {
                        sh "scp -i \${SSH_KEY} build/ ${SSH_USERNAME}@${remote_server}:/var/files/react-tester/"
                    }
                }
            }
        }
    }

}

Best regards.

Stefano Martins
  • 472
  • 2
  • 7
  • thank you Stefano, but I got it working the 'old fashioned way'. I disabled the plugin then I created they key pair on my jenkins container, then ran this command: ssh-copy-id -i ~/.ssh/id_rsa.pub myusernsme@remoteip. I followed these instructions (although not jenkins/docker specific) if anyone else gets here: https://upcloud.com/community/tutorials/use-ssh-keys-authentication/ – benishky Apr 02 '21 at 13:52