In that script, output the value of the PATH environment variable. You’ll probably find that when running in the Jenkins agent, it does not include /usr/local/bin (or where ever your copy of jq) is stored.
Extend the script to assign PATH=$PATH:/usr/local/bin and try it again.
+ echo PATH is /usr/bin:/bin:/usr/sbin:/sbin
PATH is /usr/bin:/bin:/usr/sbin:/sbin
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/jq
[Pipeline] sh
+ echo '{ foo: 123, bar: 456 }'
+ jq .foo
/Users/jordankasper/.jenkins/workspace/jq_test@tmp/durable-25b496ab/script.sh: line 1: jq: command not found
Im assuming Im still not setting it right but not sure how maybe.
+ echo PATH before is /usr/bin:/bin:/usr/sbin:/sbin
PATH before is /usr/bin:/bin:/usr/sbin:/sbin
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
+ echo PATH after is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
PATH after is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
[Pipeline] sh
+ echo '{ foo: 123, bar: 456 }'
+ jq .foo
/Users/jordankasper/.jenkins/workspace/jq_test@tmp/durable-2fde5e04/script.sh: line 1: jq: command not found
so on my mac if I do a which jq it gives me
/usr/local/bin/jq
Which means I should have to append with just
/usr/local/bin/
Which per the output above is what I have done. Is there anything else that could be hold it up?
This is the code I am running, syntax seems correct.
pipeline {
agent {
label '!windows'
}
stages {
stage('Example') {
steps {
sh '''
echo PATH before is $PATH
PATH=$PATH:/usr/local/bin
echo PATH after is $PATH
'''
sh 'echo "{ "foo": 123, "bar": 456 }" | jq ".foo"'
}
}
}
}
Each sh statement has its own env. So setting it in one won’t affect other ones.
You could combine them into a single step.
You could also be explicit and just use sh 'echo "{ "foo": 123, "bar": 456 }" | /usr/local/bin/jq ".foo"'
If /usr/local/bin/jq also reports command not found then you may want to check that the file stored at /usr/local/bin/jq is the correct executable type for your computer. I think that I’ve seen command not found when an executable was not what my system expected.