Вы находитесь на странице: 1из 3

10/4/2019 Jenkins execute script over ssh – Jaehoo Weblog

Jaehoo Weblog

Blog personal de jaehoo

Jenkins execute script over ssh


junio 11, 2019

To execute a shell script with jenkins it’s very easy task but there are some helpful tips to connect to
remote server over ssh, for example:

To avoid login prompt in command line, setup a ssh keys in your jenkins instance and copy them to
remote server.

1. Create a ssh key pair in your jenkins server, log in with the jenkins user than is currently running the
app server and execute ssh-keygen command:

1 su devops
2 ssh-keygen

2. Leave the options by default, a file /your_home/.ssh/id_rsa is generated, take care if you have a
previous file, you will not be able to authenticate using the previous key anymore if you choose
overwrite.
3. Copy the public key to remote server, using the command:

1 ssh-copy-id

If the command it’s not available you can do it manually with this:

1 cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && t

4. Then test your connection

1 ssh username@remote_host

If you need more detail of this steps you can check this post
(https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604).

https://jaehoo.wordpress.com/2019/06/11/jenkins-execute-script-over-ssh/ 1/3
10/4/2019 Jenkins execute script over ssh – Jaehoo Weblog

Then create a new freestyle jenkins job to copy a local file to remote server, you need to indicate the
path to take the file. I’m going to use the built result from another jenkins job.

1. In build section add a build step and select Copy artifacts from another project, set the path to take
the file (can be relative or absolute), and check option Flatten directories

2. Open your job and add a build step and select  Execute shell

3. Add the command, the next commands are explained by self, but basically I’m using this to copy a
zip and unpackage over remote server:

1 scp /path/to/my-bin.zip username@remotehost:/remote_home/path/to/bin &


2 ls -lah --block-size=K /remote_home/path/to/bin/*.jar
3 unzip -o -d path/to/bin  path/to/bin/my-bin.zip
4 rm path/to/bin/my-bin.zip
5 chmod +x  path/to/bin/run.sh
6 ls -lah --block-size=K /remote_home/path/to/bin/*.jar
7 ENDSSH

That’s it, now the jenkins can release my distribution.

References

https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604
(https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604)

Publicado en: General, Java, Linux, Servers, Sistemas Operativos | Etiquetado: Integration, Jenkins,
Server
https://jaehoo.wordpress.com/2019/06/11/jenkins-execute-script-over-ssh/ 2/3
10/4/2019 Jenkins execute script over ssh – Jaehoo Weblog

This site uses Akismet to reduce spam. Learn how your comment data is processed.

BLOG DE WORDPRESS.COM.

https://jaehoo.wordpress.com/2019/06/11/jenkins-execute-script-over-ssh/ 3/3

Вам также может понравиться