27

I recently rewrite bash execution command into Jenkins pipeline. The old code is like

...
source environment.sh
//Build
//Test
...

Now I use pipeline script to wrap the command, like this

sh '''
    ...
    source environment.sh
    //Build
    //Test
    ...
'''

However, I got an error, as.../.jenkins/script.sh: line 9: source: environment.sh: file not found. When I try to less environment.sh, it display correctly. So I suspect something wrong with source command within sh wrap.

Before using pipeline, source environment.sh command is working fine in shell execution. So source is install at Jenkins server, it seems pipeline script don't know what is the source command.

How could I do to run source command within sh wrapped block?

Neil
  • 2,714
  • 11
  • 29
  • 45

4 Answers4

30

Replace source environment.sh with

. ./environment.sh

Please note there is a space after first dot.

Steephen
  • 14,645
  • 7
  • 40
  • 47
16

source is a bash/ksh/etc extension, provided as a more "substantial" synonym for ..

In sh, you need to use . in case the underlying shell is one (such as dash) that does not support the command source.

sh '''
    ...
    . ./environment.sh
    //Build
    //Test
    ...
'''
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I suppose triple quotes is the syntax in pipeline script, which identify multiple lines command. And I tried use `.` instead of `source`, however, no luck on that. Similar error throws `/.jenkins/script.sh: line 9: .: environment.sh: file not found` – Neil Jul 29 '16 at 18:06
  • Ah, missed that this wasn't just a shell script. (Although that should have been obvious, sorry.) It looks like the real problem is that Jenkins has a different working directory than your original script. Using an absolute path (instead of just `environment.sh`) should work. – chepner Jul 29 '16 at 18:18
  • Thank you, with absolute path, it works. Still, it's weird that linux cannot find the file – Neil Jul 29 '16 at 22:24
  • @Neil You have to put ' ./' in first: `. ./environment.sh`. It worked for me. – A. Gille Jul 06 '21 at 17:10
  • Fixed; I always forget that `.` uses path lookup if there is no `/` in the file name. – chepner Jul 06 '21 at 17:18
1

If someone want to execute the script with source only solution is to change "Shell executable" to bash in ->Manage Jenkins->Configure System

krupal
  • 403
  • 4
  • 15
1

. ./script.sh it works fine!, but you can also do:

sh '''#!/bin/bash
      source /path/to/script.sh
                    
   '''

IMPORTANT!: Note that #!/bin/bash is the first line in the script

Adán Escobar
  • 1,729
  • 9
  • 15