This guide demonstrates executing remote scripts through Jenkins Pipelines using SSH authentication and the sshPublisher plugin, detailing configuration and execution steps.
Published on October 13, 2024 by Roman Surkoff
ssh post jenkins publish pipeline
7 min READ
This article explains how to use the Jenkins Pipeline with the sshPublisher plugin to run a bash script on a remote server. It covers key steps, such as setting up server authentication by generating and copying SSH keys, configuring Jenkins with the necessary plugin, and running the pipeline to transfer and execute files remotely. The final step is testing and verifying the pipeline output on the remote server.
# using the service user sudo su -s /bin/bash jenkins # generate key pair ssh-keygen -f ~/.ssh/id_pipessh -t rsa
ssh-copy-id -i ~/.ssh/id_pipessh.pub user@host
cat ~/.ssh/id_pipessh.pub | ssh user@host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys $$ chmod -R go= ~/.ssh && cat >> /.ssh/authorized_keys"
chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh/
sudo systemctl restart sshd
# using verbose mode -v ssh -i ~/.ssh/id_pipessh user@host -v
pipeline { agent any stages { stage('ssh') { steps { script{ cleanWs() sh "echo 'hello' >> file1.txt" sh "echo 'hello' >> file2.txt" sh "zip -r oneFile.zip file1.txt file2.txt" echo 'Local files.....' sh 'ls -l' command=''' unzip -o -d ./ oneFile.zip ls -l date cat /etc/os-release ''' } // Copy file to remote server sshPublisher(publishers: [sshPublisherDesc(configName: 'dummy-server', transfers: [ sshTransfer(flatten: false, remoteDirectory: './', sourceFiles: 'oneFile.zip' )]) ]) } } } }
[Pipeline] { [Pipeline] stage [Pipeline] { (ssh) [Pipeline] script [Pipeline] { [Pipeline] cleanWs [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Deferred wipeout is used... [WS-CLEANUP] done [Pipeline] sh + echo hello [Pipeline] sh + echo hello [Pipeline] sh + zip -r oneFile.zip file1.txt file2.txt adding: file1.txt (stored 0%) adding: file2.txt (stored 0%) [Pipeline] echo Local files..... [Pipeline] sh + ls -l total 12 -rw-r--r--. 1 jenkins jenkins 6 jun 22 19:36 file1.txt -rw-r--r--. 1 jenkins jenkins 6 jun 22 19:36 file2.txt -rw-r--r--. 1 jenkins jenkins 326 jun 22 19:36 oneFile.zip [Pipeline] sshPublisher SSH: Connecting from host [dummy] SSH: Connecting with configuration [dummy-server] ... SSH: Disconnecting configuration [dummy-server] ... SSH: Transferred 1 file(s) [Pipeline] sshPublisher SSH: Connecting from host [dummy] SSH: Connecting with configuration [dummy-server] ... SSH: EXEC: STDOUT/STDERR from command [ unzip -o -d ./ oneFile.zip ls -l date cat /etc/os-release ] ... Archive: oneFile.zip extracting: ./file1.txt extracting: ./file2.txt total 16 -rw-r--r--. 1 asanchez asanchez 6 jun 22 19:36 file1.txt -rw-r--r--. 1 asanchez asanchez 6 jun 22 19:36 file2.txt -rw-rw-r--. 1 asanchez asanchez 326 jun 22 19:36 oneFile.zip drwxrwxr-x. 2 asanchez asanchez 4096 jun 20 23:31 out mié jun 22 19:36:22 CDT 2022 NAME="Red Hat Enterprise Linux Server" VERSION="7.4 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.4" PRETTY_NAME="Red Hat Enterprise Linux" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.4:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/" REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.4 REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.4" SSH: EXEC: completed after 200 ms SSH: Disconnecting configuration [dummy-server] ... SSH: Transferred 0 file(s) [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS