Error: Using Bash script inside Declarative Jenkinsfile

376 views Asked by At

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.

1

There are 1 answers

0
i_am_beginner On

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.