简体   繁体   中英

Execute remote script in Jenkins

I have the following shell script execution for jenkins:

#!/bin/bash
ssh user@perftest03 -f '~/setup_environment.sh load/LoadConfig.xml'

Depending on time of day or what Jenkins want, or the old gods for all I know the script executes part or more of the script, but always exits before it finishes. Here is 2 different logs with the same execution, same script and no changes to anything:

Started by user Andreas Joelsson
[EnvInject] - Loading node environment variables.
Building remotely on linuxdev01 in workspace /home/byggarebob/workspace/Project-Load
[Project-Load] $ /bin/bash /tmp/hudson1526996494849043502.sh
MG_DIRECTORY   ==> /home/user/user
MG_SYMLINK     ==> /home/user/user/RunnerConfig.xml
MGC_DIRECTORY  ==> /home/user/user-config
MGC_REPOSITORY ==> git@git.company.se:test/user-config.git
MGC_BRANCH     ==> develop
MGC_FILE       ==> /home/user/user-config/load/LoadConfig.xml
Changing directory to "/home/user/user-config".
cd /home/user/user-config
git show-ref --verify --quiet refs/heads/develop
Checking if we are in branch develop.
git branch | grep --quiet '* develop'
Finished: SUCCESS

and

Started by user Andreas Joelsson
[EnvInject] - Loading node environment variables.
Building remotely on linuxdev01 in workspace /home/byggarebob/workspace/Project-Load
[Project-Load] $ /bin/bash /tmp/hudson7204317023088063558.sh
MG_DIRECTORY   ==> /home/user/user
MG_SYMLINK     ==> /home/user/user/RunnerConfig.xml
MGC_DIRECTORY  ==> /home/user/user-config
MGC_REPOSITORY ==> git@git.company.se:test/user-config.git
MGC_BRANCH     ==> develop
MGC_FILE       ==> /home/user/user-config/load/LoadConfig.xml
Changing directory to "/home/user/user-config".
cd /home/user/user-config
git show-ref --verify --quiet refs/heads/develop
Finished: SUCCESS

So one manages to run git show-ref --verify --quiet refs/heads/develop and continue, while the other stops at it, I also have stops at the change directory and event printout depending on the mood of the server, I have had it working once but it's clearly some random element which I cannot find.

Here is the parts of the shell script that is stopped executed (it goes on for longer but I don't want to flood the page)

if [ ! -d "$MGC_DIRECTORY" ]; then
    echo "Unable to clone $MGC_REPOSITORY, cannot continue"
    exit 1
else
    echo "Changing directory to \"$MGC_DIRECTORY\"."
    if [ -n $VERBOSE ]; then
        echo "cd $MGC_DIRECTORY"
    fi
    cd $MGC_DIRECTORY
fi

if [ -n $VERBOSE ]; then
    echo "git show-ref --verify --quiet refs/heads/$MGC_BRANCH"
fi
git show-ref --verify --quiet refs/heads/$MGC_BRANCH

if [ $? != 0 ]; then
    echo "No branch found in $MGC_DIRECTORY called $MGC_BRANCH."
    exit 1
fi

echo "Checking if we are in branch $MGC_BRANCH."
if [ -n $VERBOSE ]; then
    echo "git branch | grep --quiet '* $MGC_BRANCH'"
fi
git status | grep --quiet '* $MGC_BRANCH'

I have a theory that the first successful shell command (return 0) breaks the execution, but some of the test cases breaks this theory as we manage to change directory for example.

I have checked the https://wiki.jenkins-ci.org/display/JENKINS/SSH+Agent+Plugin plugin but it seems to be broken / not able to set it up correctly.

#!/bin/bash
ssh user@perftest03 '~/setup_environment.sh load/LoadConfig.xml'

Solved it, for some reason after googling I thought I needed the -f flag to work. Was looking in the wrong direction for the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM