Retrieve all jenkins parameters to iterate in groovy code - groovy

Based on this answer, I would like to iterate over all the properties in jenkins job and replace with their value in the job in build step.
e..g.
file = new File("folder/path/myfile.cs")
fileText = file.text;
fileText = fileText.replaceAll("parameter", "parametervalue");
file.write(fileText);
I saw an example to resolve one param like this:
build.buildVariableResolver.resolve("myparameter")
But I need
To iterate over all of them
To do it in Build step (after pulling from Source Code) so in the current build.
How can I do it?

The following is a sample groovy code that you can use with the scriptler plugin, that I suggest to use. This code can correctly read the Environment variables that are set in any Jenkins job:
import jenkins.model.Jenkins
def thr = Thread.currentThread()
def build = thr?.executable
def env = build.getEnvironment()
def buildNumber = env.get("BUILD_NUMBER")
In case you want to read the Build Parameters (you have a parameterized build and need access to those variables), you need to use the previous code and add the following:
def bparams = build.getBuildVariables()
def myBuildParam = bparams.get("MY_BUILD_PARAMETER")
after you read them, you can write them to your file, in a build step occurring after the git cloning.

Related

pytest- creating a plugin

I would like to create pytest plugin, that includes hook function and fixtures.
I have created it locally on a conftest.py file located at my root folder, and everything works fine. Now I would like to use this code in other projects, by converting the conftest.py code into a pytest- plugin.
In theory I know I simply need to create a package, however I have some really hard time understanding the entry points that I will use for this plugin.
I have an approximation of what I need, where my actual code goes inside the metadata of the test and reports it to a file.
The example code:
conftest.py
#pytest.fixture(scope = 'function')
def upload_manager():
def wrapper(val):
# Do something
return val
return wrapper
def pytest_report_teststatus(report):
if report.when == 'call':
# Do something
if report.when == 'setup':
# Do something
test_foo.py
def test_bar(upload_manager):
my_val = upload_manager(1000)
assert my_val == 1000
How would I go about using this code (fixture and hooks) as a package?
Okay, found an answer- This requires in the setup.py file to add the following key:
entry_points={
"pytest11": [
"name_of_plugin = package.file_name",
],
},
I've used it for a python plugin I wrote to extract data and metadata out of the tests themselves, it can be found here for future reference pytest-data-extractor.
If for some reason, someone needs it as package it can by installed directly from PyPi:
pip install pytest-data-extractor

How to change working directory of Python script during execution?

I am working on a personal project and I have been stuck here.
I want to change the working directory of my program during its execution but it is not working and there is also no error.
def google_search(search_term, api, cse, num = 10):
service = build("customsearch", "v1", developerKey = api)
res = service.cse().list(q = search_term, cx = cse).execute()
#Creating new folder with search term
if not os.path.exists(search_term):
print('Creating project: ' + search_term)
os.makedirs(search_term)
return res['items']
os.chdir("../test 1/%s" %search_term)
When I execute the entire script I get no error, the script executes completely but the directory doesn't change to the directory I have just created using the 'search_term'.
If I individually run the command below in ipython shell(using the search_term value), it executes and takes me to the intended directory.
os.chdir("../test 1/%s" %search_term)
It is working as an individual command when I put in the name of the directory directly but it is not working when I use it in the whole script.
There is return statement before os.chdir, so that line is not executed.

Recommended way of loading config file from jenkins pipeline groovy script

I want to load a config value (something like json, yaml, xml or ini) from a jenkins pipeline script. When I try to use org.yaml.snakeyaml.Yaml I get
Scripts not permitted to use new org.yaml.snakeyaml.Yaml
I know I can unlock org.yaml.snakeyaml.Yam, but the message tells me that this does not seem to be the standard way of loading config files.
Is there a way of loading config files that is already unlocked?
If anyone looking for yaml parser in jenkinsfile, I recommend the following
def yamlData = readYaml file: 'cae.yaml'
Reference : https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#code-readyaml-code-read-yaml-from-files-in-the-workspace-or-text
Try using the JsonSlurper:
def config = new JsonSlurper().parse(new File("config.json"))

Adding WSDL to a project dynamically using groovy

I am looking for groovy script to add an wsdl to a project in SOAPUI dynamically at runtime using groovy scripts. i have tried the following code
import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.WsdlInterfaceFactory
project = new WsdlProject()
//wsdl = new WsdlInterfaceFactory()
project.setName("Project1");
WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project, "C:\\Manoj\\BIAScalarReads\\IDSRequestLIB\\IDS_Request_MeterData.wsdl", true)[0];
No new project is getting loaded in the SOAPUI. Can anyone help on this?
You're creating a project, however then you don't add the project to the Workspace. To do so you must use the related method from Workspace class.
It possible to do so in a different ways. For example, create your project, save on disk and then load on the workspace in your groovy script testStep inside a testCase:
import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.WsdlInterfaceFactory
def project = new WsdlProject()
project.setName("Project1")
WsdlInterfaceFactory.importWsdl(project, 'path/to/yourWsdl', true)
// file to save the project
def projectFilePath = 'C:/temp/myProject.xml'
// save the project
project.saveAs(projectFilePath)
// load the project from disc to workspace
testRunner.testCase.testSuite.project.workspace.importProject(projectFilePath)
Another possible and more compact way to do the same is using workspace.createProject this way you avoid to save the disc and then import, to do it this way you can use the follow script:
import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.WsdlInterfaceFactory
def workspace = testRunner.testCase.testSuite.project.workspace
def project = workspace.createProject('Project2',new File('C:/temp/myProject.xml'))
WsdlInterfaceFactory.importWsdl(project, 'path/to/yourWsdl', true)
Hope this helps,

Writing a custom builder that executes external command and python function

I'm looking to write a custom SCons Builder that:
Executes an external command to produce foo.temp
Then executes a python function to manipulate foo.temp and produce the final output file
I've referred to the two following sections, but I'm not sure the correct way to "glue" them together.
18.1. Writing Builders That Execute External Commands
18.4. Builders That Execute Python Functions
I know that Command accepts a list of actions to take. But how do I properly handle that intermediate file? Ideally the intermediate file would be invisible to the user -- the entire Builder would appear to operate atomically.
Here's what I've come up with that seems to be working. However the .bin file isn't being deleted automatically.
from SCons.Action import Action
from SCons.Util import is_List
from SCons.Script import Delete
_objcopy_builder = Builder(
action = 'objcopy -O binary $SOURCE $TARGET',
suffix = '.bin',
single_source = 1
)
def _add_header(target, source, env):
source = str(source[0])
target = str(target[0])
with open(source, 'rb') as src:
with open(target, 'wn') as tgt:
tgt.write('MODULE\x00\x00')
tgt.write(src.read())
return 0
_addheader_builder = Builder(
action = _add_header,
single_source = 1
)
def Elf2Mod(env, target, source, *args, **kw):
def check_one(x, what):
if not is_List(x):
x = [x]
if len(x) != 1:
raise StopError('Only one {0} allowed'.format(what))
return x
target = check_one(target, 'target')
source = check_one(source, 'source')
# objcopy a binary file
binfile = _objcopy_builder.__call__(env, source=source, **kw)
# write the module header
_addheader_builder.__call__(env, target=target, source=binfile, **kw)
# delete the intermediate binary file
# TODO: Not working
Delete(binfile)
return target
def generate(env):
"""Add Builders and construction variables to the Environment."""
env.AddMethod(Elf2Mod, 'Elf2Mod')
print 'Added Elf2Mod to env {0}'.format(env)
def exists(env):
return True
This can indeed be done with the Command builder, by specifying a list of actions, as follows:
Command('foo.temp', 'foo.in',
['your_external_action',
your_python_function])
Notice that foo.in is the source, and you should name it accordingly. But if foo.temp is internal as you mention, then this approach probably isnt the best approach.
Another way, which I feel is much more flexible, would be to use Custom Builder with a Generator and/or Emitter.
The Generator is a Python function where you do the actual work, which in your case would be calling the external command, and also call the Python function.
An Emitter allows you to have a fine-tuned control over the sources and targets. I used a Builder with a Emitter (and Generator) once to do C++ and Java code-generation with Thrift input IDL files. I had to read and process the Thrift input file to know exactly what files would be code-generated (which are the actual targets), and the Emitter is the best/only way to do something like this. If your particular use-case isnt so complicated, you can skip the Emitter and just list your sources/targets in the call to the builder. But if you want foo.temp to be transparent to the end-user, then you'll need an Emitter.
When using a Custom Builder with a Generator and Emitter, the Emitter will be called every time by SCons to calculate the sources and dependencies to know if the Generator needs to be called. The Generator will only be called if one of the targets is considered older with respect to the sources.
There are numerous examples showing how to use a Generator and Emitter in a Custom Builder, so I wont list the code here, but let me know if you need help with the syntax, etc.

Resources