Member-only story
Execute bash script on a remote host using Jenkins Pipeline and Publish Over SSH plugin
I decided to install Jenkins on another server because I don’t want it to mess up with my containers and I will execute different remote scripts from different servers.
Prerequisite: A running Docker and Docker Compose, a DNS record that points to https://jenkins.yourdomain.com, and knowledge of SSH keys.
Let’s install Jenkins first by creating a Docker Compose file.
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:lts
ports:
- 80:8080
container_name: jenkins
restart: always
volumes:
- .:/var/jenkins_home
The file states that it will pull the latest Jenkins image from Docker Hub and assign it to port 80 so that we don’t need to type https://jenkins.yourdomain.com:8080 in the browser.
Make sure to have the restart: always because when you install any Jenkins plugins, the service will automatically restart, and you will lose connection to the container.
From the Jenkins server, copy the public key and save it to the authorized keys of the remote server server.
Jenkins Server: cat ~/.ssh/id_rsa.pub
Go to Dashboard > Manage Jenkins and click on the Plugins section.