Jenkins can't access shell alias

15,281

Solution 1

The \n that is showing in the output of your echo commands
suggest this is not running under bash, as you may be expecting.

Please check the setting of your jenkins user
(or any other user that you run Jenkins with) -
especially the setting of the default shell.

To test this, add one of those lines at the beginning of your script:

env

or

env | grep -i shell

Should also consider making sure your scripts run under the correct shell,
by adding the "shebang" line as the first line in each script.
In the case of 'bash', for example, you should add the following first line:

#!/bin/bash

(it is not a comment, despite what the auto-syntax-highlighter may think...)

Solution 2

For anyone else who's having this problem, you may need to set the expand_aliases shell option, which seems to be off by default with Jenkins:

shopt expand_aliases # check if it's on
shopt -s expand_aliases # set expand_aliases option to true
shopt expand_aliases # it should be on now

# test with a simple alias, should print 1
alias x="python -c 'print 1'"
x
Share:
15,281
Daniel Brady
Author by

Daniel Brady

Cognitive Science and Computer Science graduate of Indiana University, class of '14. Employed as a Software Engineer with ProdPerfect, Inc. Familiar with: Scheme, Racket, miniKanren, SML, HTML, CSS, JavaScript, C, C++, Java, Python, English, Go and Ruby. Enjoy metaprogramming and long walks through the stack.

Updated on November 02, 2022

Comments

  • Daniel Brady
    Daniel Brady over 1 year

    I have configured a Jenkins job to source a bash script that sources another bash script which adds an alias to the .bashrc of its user and sources the .bashrc itself, and then original script tries to use that alias (set by the second). However, it cannot seem to find the alias it has just created. I am not using any scripting plugins aside from using a "Send files or execute commands over SSH" build step to source the script.

    The job does this:

    source ./test_script.sh
    

    test_script.sh looks like this:

    echo "In test_script.sh"
    echo $USER
    echo $HOME
    source ./setup_env.sh
    echo "\nBack in test_script.sh"
    alias foo
    foo
    

    And finally, setup_env.sh looks like this:

    echo "\nIn setup_env.sh"
    echo "alias foo=\"echo foobar\"" >> $HOME/.bashrc
    source $HOME/.bashrc 2>/dev/null
    cat $HOME/.bashrc
    

    The output I receive from the Jenkins job looks like this:

    In test_script.sh
    my_user
    /home/my_user
    \nIn setup_env.sh
    ...all of my bashrc...
    alias foo="echo foo"
    \nBack in test_script.sh
    alias foo='echo foo'
    ./test_script.sh: line 7: foo: command not found
    

    I don't understand why this is happening, when I can happily run it myself on the command-line and watch it succeed. Why can't Jenkins use the new alias, when it can obviously find it (as demonstrated by the output of the alias foo command)?

  • Abhinav Thakur
    Abhinav Thakur almost 3 years
    Couldn't have found this solution on my own. Thank you so much!