简体   繁体   中英

How to use source command within Jenkins pipeline script

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?

Replace source environment.sh with

. ./environment.sh

Please note there is a space after first dot.

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
    ...
'''

如果有人想仅使用源代码执行脚本,解决方案是在 ->Manage Jenkins->Configure System 中将“Shell executable”更改为 bash

. ./script.sh . ./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

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