0

I am facing some challenges to write my bash script code inside the Jenkinsfile. UserCase: I am keeping multiple tools code in the folder structure in one GIT repo. I want whenever any developer makes the change to the individual folder, a build gets triggered for specific folder related code.
My Logic: Pick the last two commit, find the difference. On the basis of difference, make the build as per changed files. I wrote the bash script and it works fine on linux machine. After moving it inside the Jenkinsfile, i am facing some error. This might be groovy specific. Need Help in these

#!/bin/bash
git log -2 --no-merges | grep commit > git_commit.txt
cat git_commit.txt
echo 'hi'
i=0;
while read -r line;
do
    commit[$i]=`echo "$line" | awk '{print \$2}'`
    i=$((i+1))
done < git_commit.txt

This is not the complete code. I am getting error in assigning value:

commit[$i]=`echo "$line" | awk '{print \$2}'`

Let me know if you know further explanation to help.

yong
  • 13,357
  • 1
  • 16
  • 27
i_am_beginner
  • 119
  • 3
  • 11
  • Hi @jvipul shouldn't you just store that as .sh file and execute this from jenkins? What does your jenkins pipeline code look like? – hakamairi Mar 05 '19 at 08:02
  • @Jvipul : "getting error" is not a very helpful problem description. Does Jenkins say "I don't love you anymore", or what does the error look like? Also, are you sure that Jenkins really runs your code under bash? – user1934428 Mar 05 '19 at 08:10

1 Answers1

0

I just caught it. This was a silly mistake of assigning var in bash. I add eval in assigning the output of awk command. So my code change is:

eval commit[$i]=`echo "$line" | awk '{print \$2}'`

Not sure how eval helping me is here but it resolved now.

i_am_beginner
  • 119
  • 3
  • 11