Search code examples
javashelldeploymentjenkinsjobs

Usage of string parameter in "execute shell command" section of Jenkins


I have a string parameter configured for a jenkin job named "Job_Name".

I want to check the value that is passed to the parameter in "execute shell command" section of jenkins.

My current shell command is as below in the given pic.

enter image description here

Shell command:

if [ "${Job_Name}" == "RSProductPreprocessor" ]; then
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook  -i hosts/dev playbook_deployRSProductPreProcessor.yml -v --extra-vars "RSProductPreProcessorVersion=${Number_Of_RSProductPreProcessor_Build_To_Deploy}"
fi

But I am getting this response as below when I run the job.

+ '[' '' == RSProductPreprocessor ']'
Finished: SUCCESS

What should be done for the if condition to work properly. Please advise.


Solution

  • You have a couple of things to fix

    Click the link under your shell to see available environment variables

    This is some of mine

    BUILD_NUMBER
    The current build number, such as "153"
    BUILD_ID
    The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
    BUILD_DISPLAY_NAME
    The display name of the current build, which is something like "#153" by default.
    JOB_NAME
    Name of the project of this build, such as "foo" or "foo/bar". (To strip off folder paths from a Bourne shell script, try: ${JOB_NAME##*/})
    

    So you need ${JOB_NAME} all in capitals

    And a comparison in shell is

    if [ "${JOB_NAME}"="xxxx" ]; then
        ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook  -i hosts/dev playbook_deployRSProductPreProcessor.yml -v --extra-vars "RSProductPreProcessorVersion=${Number_Of_RSProductPreProcessor_Build_To_Deploy}"
    fi 
    

    So only a single =

    Your Jenkins may be using a different shell to mine so you can force bash by adding a shebang to the first line

    #!/bin/bash