Forum Discussion

harmancheema's avatar
harmancheema
Occasional Contributor
6 years ago
Solved

Set variable in groovy based on parameter from Jenkins

I have a groovy script in my tests which takes the user input to set the environment and project variables. I'm trying to integrate the tests with jenkins, and want to create a parameterized build fo...
  • TNeuschwanger's avatar
    6 years ago

    Hello,

    -----------------------
    In Jenkins...

    ...Fill out desired Jenkins parameters
    =========== Jenkins "This project is parameterized" ================
    Choice Parameter

     

    Name ACCOUNT
    Choices development
                  integration
                  production


    Choice Parameter

     

    Name PROJECT
    Choices prjX
                  prjY
                  prjZ


    ...Use parameters in your command line interface for launching SoapUI/ReadyAPI within Jenkins
    =========== Jenkins Build Execute shell ================
    # clean up temporary script dir
    rm -rf bin

     

    # create a temporary script dir
    mkdir bin
    cd bin

     

    # create a temporary script
    touch tmp-test-runner.sh
    chmod 755 tmp-test-runner.sh

     

    SOAPUI_TESTRUNNER=/var/lib/jenkins/SmartBear/SoapUI-5.3.0/bin/testrunner.sh
    SOAPUI_PROJECT=soapui/{your full project file name goes here. (i.e. readyapi-or-soapui-project.xml)}

     

    echo "echo Running tests against the $ACCOUNT account" > tmp-test-runner.sh
    echo "echo Running tests against the $PROJECT project" >> tmp-test-runner.sh
    echo "$SOAPUI_TESTRUNNER -a -I -j -f output -Pact=$ACCOUNT -Pprj=$PROJECT $SOAPUI_PROJECT" >> tmp-test-runner.sh
    export DEBUG=false

     

    # return to base directory
    cd ../

     

    # execute tmp script somehow or however you are doing it now
    bin/tmp-test-runner.sh

     

    -----------------------
    In SoapUI/ReadyAPI...
    SoapUI can be launched from Jenkins. Whatever '-P' properties you supply to the command line can be used during the run of your project. They override whatever you have defined in your project.
    Above the -P "act" and -P "prj" properties are set to whatever was picked in Jenkins build request.

     

    Review the command line options from Smartbear website. I have had success with Jenkins using -P command line option for project level properties. You could also use -D for system properties or -G for global properties if you like to use those instead.

     

    In any groovy script within your project, you can reference what was passed in that property using code such as:
       def accountNameEnvironment = context.expand( '${#Project#act}' ).toLowerCase();
       def projectName = context.expand( '${#Project#prj}' ).toLowerCase();

     

    Then use as needed with groovy code inside your project:
       if (accountNameEnvironment == 'development') {
           endPoint = 'https:\\something a';
       };
       if (accountNameEnvironment == 'integration') {
          endPoint = 'https:\\something b';
       };
       if (accountNameEnvironment == 'production') {
          endPoint = 'https:\\something c';
       };


       log.info 'project from Jenkins=' + projectName;

     

    You will have to code your projects with command line parameters input in mind. Jenkins will execute everything that is not disabled top down in functionality testing. You can do all of this locally on your computer to simulate what Jenkins will do by running SoapUI or ReadyAPI from command line interface from a shell script of your operating system. If it works from your command line, it is easy to port to Jenkins.