1

I have the below shell script:

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

How can I run it thought Execute shell step in a Jenkins freestyle job? Execute shell

I need also to add an ssh command to the environment. This should be executed using passwordless authentication.

Note: I don't want to connect to the environmnet and run this shell, but want to execute it directly from the box.

4
  • The screenshot seems to show that you are using a freestyle job, not a pipeline job. Is this the case? Commented Dec 17, 2019 at 17:36
  • You are correct
    – arielma
    Commented Dec 17, 2019 at 17:59
  • I'm a bit confused by your note regarding SSH. Are you saying that you want the script to be executed over SSH on an agent? Commented Dec 17, 2019 at 18:08
  • Yes. On bitbucket remote server
    – arielma
    Commented Dec 17, 2019 at 18:37

2 Answers 2

1

For executing the command in the Jenkins Freestyle Job.

#!/bin/bash

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

For ssh

ssh -o StrictHostKeyChecking=no user@host '
   command_1 to execute
   command_2 to execute
   ./script.sh to execute
   command_3 to execute
'
2
  • Is there an option with the 'Jenkins Freestyle Job' to add ssh to a remote machine and then run the logic?
    – arielma
    Commented Dec 18, 2019 at 9:18
  • Let me try to understand the question you are talking about. You want to install ssh into some instance and then execute commands?
    – Manish_
    Commented Dec 20, 2019 at 4:42
1

You can specify the script in the "execute shell" step of the build. Where it gets executed will depend on what labels the job needs, and which nodes those labels are associated with.

It sounds like you want to execute this script on an agent. As long as the agent is available, you can request it via a label in the job configuration. It's a bit difficult to determine this without having access to your setup, but if e.g. you have a set of agents on Debian boxes, you could attach the label debian to them, and specify that the job should run on debian nodes in the job configuration.

If on the other hand it should be run on master (where Jenkins itself is running), you can specify the label master.

You thus need to put your script into the "Execute Shell" box that Jenkins provides you, perhaps declaring what shell and options you want to use:

/bin/bash -xe
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

I need also to add an ssh command to the environment. This should be executed using passwordless authentication.

For this to happen, you need ssh keys to be exchanged with the host onto which you want to ssh into.

3
  • do you mind put my syntax example on top of my do I'll understand how it should look?
    – arielma
    Commented Dec 18, 2019 at 5:12
  • I've added your script verbatim. Does this help you answer the question? Commented Dec 18, 2019 at 7:45
  • and I want to add ssh to some remote machine? for example: ssh -o StrictHostKeyChecking=no -n {user}@{bitbucket_server}
    – arielma
    Commented Dec 18, 2019 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.