0

I need to source 2 scripts in different locations and then run docker-compose but I am facing error that the scripts must be sourced first.

I found this How to use source command within Jenkins pipeline script question and wrote my jenkins commnad as below:

. ../env/scriptA.sh arg-1 ../env/scriptB.sh ../compose/build.yml arg-2

But still facing that error. So how I can source all these scripts and build file in the jenkins?

AVarf
  • 4,481
  • 9
  • 47
  • 74

2 Answers2

1

from bash manual

. (a period)

. filename [arguments]

Read and execute commands ...

the syntax is one filename and then positional parameters, it can't accept multiple files. Concatenating files doesn't allow to change paramters between calls, maybe a command sequence could be used if allowed

{ . file1 args ; . file2 args;}

Note the space after the first opening brace and semicolon before closing brace are important.

Community
  • 1
  • 1
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • You are right this is just one line and the script and yml files are other arguments but I can't use . (period) because there are many special combinations of characters that are creating errors (“[[: not found”) and I also found this https://stackoverflow.com/questions/3401183/bash-syntax-error-not-found and when I use "bash" instead of "." it can source the script but obviously in a new environment because in the next line docker-compose can't find the compose file to start running containers. Any idea? – AVarf Feb 06 '19 at 14:05
  • Are you explicitly using a bash shebang? `#! /bin/bash` or `#! /bin/env bash` as the very first line, something like that? Because `sh` is *not* bash, and won't know what `[[` is, though `bash` can read Bourne `sh` syntax just fine. – Paul Hodges Feb 06 '19 at 14:17
  • this has nothing todo with shebang when sourcing files the shell `sh` executes directly the commands which means you can't source specific `bash` syntax file in other shell, the sourced files must be for the calling shell, and here it seems jenkins uses a posix shell `sh` not `bash` – Nahuel Fouilleul Feb 06 '19 at 14:24
  • @Avarf because when using `bash` a new process is created and when it terminates environment of caller shel `sh` hasn't change because each process has its own environment, sourcing is different because it doesn't create a new process – Nahuel Fouilleul Feb 06 '19 at 14:29
  • Thank you for your helps. Yes I found that and I described the solution in my answer. – AVarf Feb 06 '19 at 14:31
0

As Nahuel Fouilleul mentioned in his answer this is just one line and the script and yml files are other arguments and as I mentioned in the comment to Nahuel Fouilleul the problem is because of "[[" I can't source that script in shell environment of jenkins (even if the script has its own shebang) so I added the shebang to the shell block in jenkins as below and now it works.

sh '''#!/bin/bash -xe
. ../env/scriptA.sh arg-1 ../env/scriptB.sh ../compose/build.yml arg-2
echo "other commands"
'''
AVarf
  • 4,481
  • 9
  • 47
  • 74