Cant get jq to work in pipeline script

Hello All,

Trying to do something I hope should be pretty simple. I am running a simple pipeline script and in the build stage trying to run

labId=$(cat slmaven.json |jq -r '.labId')

But I keep getting

/Users/jordankasper/.jenkins/workspace/env_pipeline@tmp/durable-a197f770/script.sh: line 3: jq: command not found

I installed Jenkins on my mac and followed those install instructions but cant find a way for my jenkins instance to see jq on my local machine.

What do I need to do so it can be seen my Jenkins when it runs the pipeline script?

Thank you for your help!

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.

@MarkEWaite this is what I get when I echo out the PATH

PATH=/Users/jordankasper/.jenkins/tools/hudson.model.JDK/1.8/bin:/Users/jordankasper/.jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.8.1/bin:/Users/jordankasper/.jenkins/tools/hudson.model.JDK/1.8/bin:/Users/jordankasper/.jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.8.1/bin:/usr/local/bin/jq:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

If I do a which jq I get

/usr/local/bin/jq

Am I doing something wrong?

If your agent is running directly on the computer, then it should “just work”.

pipeline {
    agent {
        label '!windows'
    }
    stages {
        stage('Example') {
            steps {
                sh 'echo PATH is $PATH && which jq'
            }
        }
    }
}

In my case, I don’t have jq installed, so the output of the which command is empty.

@MarkEWaite I am still not sure what I am doing wrong.

Here is the code

pipeline {
    agent {
        label '!windows'
    }
    stages {
        stage('Example') {
            steps {
                sh 'echo PATH is $PATH && PATH=$PATH:/usr/local/bin/jq'
                sh 'echo "{ "foo": 123, "bar": 456 }" | jq ".foo"'
                
            }
        }
    }
}

Here is the output

+ 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.

That’s very close. You need to add the directory to the path, not the program. You need to handle the arguments to echo with consistent quoting.

@MarkEWaite still getting the same thing

+ 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.

Thank you @halkeye and @MarkEWaite this worked great!