Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Saturday, 28 December 2019

Adding Environment Variables In Jenkins Using Groovy Script

https://medium.com/@mukeshsingal/access-jenkins-global-environment-variables-using-groovy-or-java-b5c1e6b53685
import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;public createGlobalEnvironmentVariables(String key, String value){

        Jenkins instance = Jenkins.getInstance();

        DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
        List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);

        EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
        EnvVars envVars = null;

        if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
            newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
            globalNodeProperties.add(newEnvVarsNodeProperty);
            envVars = newEnvVarsNodeProperty.getEnvVars();
        } else {
            envVars = envVarsNodePropertyList.get(0).getEnvVars();
        }
        envVars.put(key, value)
        instance.save()
}createGlobalEnvironmentVariables('Var1','DummyValue')

Thursday, 22 February 2018

Friday, 21 October 2016

MSTest Jenkins Integration


Plugins Required :
1. Active Choice Parameter
2. Html Report Publisher
3. File System SCM
4. To publish Extent report, you have do some setting in jenkins. The setting details have been covered as other topic.







Monday, 17 October 2016

Where Jenkins Login Password is saved

Jenkins places admin login password in file - ${Home Path}\.jenkins\secrets\initialAdminPassword file.
It's different than where Jenkins is installed.
Normally, Home location = C:\Users\{User}


Monday, 19 September 2016

Why Sometimes Visual Studio IDE compiles Code But MSBuild Throws Compilation Error Because Of Assembly Reference Not Found


1. I had a CodedUI Test which used CodedUI library such as -
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;

I was successfully able to compile the entire solution using Visual Stduio IDE 2013 Ultimate.
But, while I was trying to compile the same using MSBuild tool, I was getting errors because of not able to find assemblies
Following errors were thrown:
05:44:41 FunctionalTests\CodedUITest1.cs(7,40): error CS0234: The type or namespace name 'UITesting' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?) [C:\Users\Administrator\.jenkins\workspace\Bodycote\AutomatedTest\AutomatedTest.csproj]
05:44:41 FunctionalTests\CodedUITest1.cs(9,40): error CS0234: The type or namespace name 'UITest' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?)

Solution - 
This error appears, when MSBuild does not find references added to project of specific version. 
Visual studio facilitates to go with any version of references. 
To go with any available reference, not to be very much strict about particular version, we have to set the property - "Specific Version" to false for used references. By default its true.

By setting Specific Version for all references to false, I solved the issue. MSBuild tool started compiling.








Friday, 16 September 2016

Running Groovy From Jenkins And Reading/Updating Jenkins Parameters Set Using Active Choice Parameters Plugin

Running Groovy In Jenins -

1. Need to install Groovy plugin.
2. Update Global Tool Configuration For Groovy to set path -
in my case, it is - C:\Program Files\groovy-2.4.7
Note - its not upto bin
3. add jars to groovy lib folder - jenkins-core-2.7.4.jar,stapler-1.243.jar,commons-lang-2.6.jar
jenkins-core-2.7.4.jar,stapler-1.243.jar are available in jenkins under
{User folder}\.jenkins\war\WEB-INF\lib

commons-lang-2.6.jar can be downloaded from maven.

Run groovy script as build step - Execute System Groovy Script

Reading/Updating Jenkins Parameters
Reference -
http://stackoverflow.com/questions/10882515/how-to-retrieve-jenkins-build-parameters-using-the-groovy-api
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script

My problem statement -
1. I had active choices parameters for MSTest TestCategory to execute. Like - Sanity,Smoke,Regression
2. Active Choices parameters sets parameter as - Sanity,Smoke,Regression.
3. MSTest TestCategory switch needs in Sanity|Smoke if one needs to execute tests from Sanity and Smoke Category on.
4. So, I need to reset TestCategory Parameter used for

Following was my solution -

import hudson.model.*

def testCategory = build.buildVariableResolver.resolve("TestCategory")
testCategory = testCategory.replaceAll(',','|')

def pa = new ParametersAction([
  new StringParameterValue("TestCategory", testCategory)
])

// add variable to current job
Thread.currentThread().executable.addAction(pa)