0

I've a bash script that I run on Debian 11 and it worked just fine, I must move it to Ubuntu Server (probably 22.04) and now it stopped working. My script below:

BRANCHES=("master" "develop")
for BRANCH in "${BRANCHES[*]}"
do
   echo Building $BRANCH
   git checkout "$BRANCH"
   #Code doing tasks
done

Tried also "${BRANCHES[@]}"
It gives me output:

Building ("master" "develop")
error: pathspec '("master" "develop")' did not match any file(s) known to git

I don't understand why it works fine on Debian but doesn't work on Ubuntu. My guess was that it's not executed in bash (but I've #!/bin/bash +x on top of the script). AFAIK I cannot easy check it, because I execute this script via Jenkins shell execution and do not have SSH access to the server.

  • 1
    Please [edit] your question and show us how you run the script. Don't you also get another error, something like Syntax error: "(" unexpected? – terdon Jan 27 '23 at 13:36
  • 1
    Try with for BRANCH in "${BRANCHES[@]}"; Use @ instead of *, it works for me in bash in Ubuntu. – sudodus Jan 27 '23 at 13:38
  • 2
    Pasting the code into https://www.shellcheck.net/ shows an error: for BRANCH in "${BRANCHES[*]}" ^-- SC2066 (error): Since you double quoted this, it will not word split, and the loop will only run once. – Bodo Jan 27 '23 at 13:40
  • 1
    On my Ubuntu 22.04 , in GNU bash, Version 5.1.16(1)-release, I get one line of output Building master develop. (I removed the git command.) Please [edit] your question and copy&paste the complete script you ran on your system and the complete output. Of course you could (should) use a minimalized version of your script. – Bodo Jan 27 '23 at 13:49
  • @terdon No, this is the first error I got. – Aenye_Cerbin Jan 27 '23 at 13:50
  • Is that the exact code you have? You didn't accidentally use BRANCHES='("master" "develop")' (with the single quotes)? – Kusalananda Jan 27 '23 at 14:42
  • "My guess was that it's not executed in bash (but I've #!/bin/bash +x on top of the script)". You can check this by putting an echo $SHELL into your script. But based on the tags, it sounds like you are running this script via Jenkins. Could you please post your whole Pipeline code or Freestyle job configuration? – jayhendren Jan 27 '23 at 17:08
  • @Kusalananda No the declaration and use of the array are copy-paste of what is in the script. – Aenye_Cerbin Jan 28 '23 at 12:41
  • @jayhendren added echo $SHELL and I got the output: /bin/bash. Yeah I run it via Jenkins, but am probably not supposed to publish the whole pipeline. I am using built-in execute shell and it the array is injected via EnvInject (it was done on previous setup the same way and worked). – Aenye_Cerbin Jan 30 '23 at 07:06
  • 1
    Please [edit] the question and add all information there instead of using comments for this purpose. Probably there was something different in your previous setup. Did it use an array? My guess is that your EnvInject does not create BRANCHES as an array but a simple string. Try to create a minimal Jenkins pipeline, that actually reproduces the problem and copy&paste the pipeline code. – Bodo Jan 30 '23 at 10:32
  • Adding to my previous comment: I am sure that an environment variable cannot be an array. See https://stackoverflow.com/q/5564418/10622916, https://askubuntu.com/q/295515, https://unix.stackexchange.com/q/353235/330217. If your branch names will never contain spaces or any special characters that might get interpreted or replaced by the shell, you could use something like BRANCHES="master develop" and for BRANCH in $BRANCHES, without quotes because word-splitting is intended. Always make sure you can reproduce the problem with exactly the code shown in the question without anything else. – Bodo Jan 30 '23 at 10:53
  • @Bodo I will do simple pipeline and copy paste all the scripts from both old and new setup. – Aenye_Cerbin Jan 30 '23 at 11:07

1 Answers1

0

The problem was caused by EnvInject plugin not properly setting the branches to built. Fixed it by writing simple python script that split the array declaration into string and adding that string to env variables. Then in scripts it's possible to simply iterate over all elements of the string separated by space.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Feb 06 '23 at 09:54