Does groovy script support PowerShell syntax? - jenkins

Does groovy script support PowerShell syntax?
I have a PowerShell script that I would like to use in Jenkins pipeline. I was not able to find any PowerShell module under "Generate Pipeline Script".

Of course not. PowerShell is a completely different scripting language than Groovy. You can exeucte your PowerShell script through Groovy though if you have a PowerShell interpretor available and Jenkins Pipeline Scripts allow to call it.

It all depends what you want to do. If you have an existing build script you want to translate into Jenkins pipeline, you would normally want to translate to Jenkinsfile-style groovy. However if you have a script to want to call, there is a standard step to do that to do that. It is important to realise that this comes up as a single step wrt jenkins, but it might be what you want to do. As said, all depends.

These answers are slightly confusing based on the question.
Can you run PowerShell in a Jenkins pipeline? Absolutely
Are their caveats and weird catches to get Groovy to interpolate you script correctly? Yes!
I think you can only run PowerShell on a Windows based Jenkins host or build node (even though you can run pwsh on all *nix machines). To do this is you just call powershell instead of sh in the pipeline step.
powershell """
Write-Host "PowerShell stuff in the pipeline"
Write-Host "Because escaping vars can get gnarly I do this..."
Set-Variable -Name "LonghandVar" -Value "Some Value"
"""

Related

Passing env/params values to the Shell Service plugin of Jenkins

Shell Service of Jenkins plugin is like in the picture that is below. I need to pass params/env variable to it but couldn't make it.
Is there any way to pass values into "Execute shell script on remote host using ssh"?
If this plugin works as a usual ssh command, you can escape $DOCKER_REGISTRY quotes
ssh jenkins#192.168.13.156:22 "echo \"$DOCKER_REGISTRY\""
IMHO: Get rid of the ssh plugins in Jenkins, just use shell, is way more flexible.

Can Jenkins run some code or it is only scheduling external programs?

I'm new to Jenkins and I heard it is really good for continues integration.
My flow is not complicated: I need to get a list from SQL by some query, parse it line by line, send each line to some virtual machines (which will run this line and create some file as result), and then analyze the results.
Where in Jenkins can I program my code?
Is Jenkins' purpose is only to schedule external programs one by one and not to run the code in Jenkins itself?
Is there a way to write code in jenkins that is not a bunch of CMD commands?
you can do the following ( syntaxic pipline ) :
- stage 1 : execute command over ssh (jenkins plugin) to execute sql query
- stage 2 : send each line to dedicated VM with Ansible playbook
- stage 3 : analyze depend on the tech you are using there are plenty of jenkins plugin to connect to monitoring and analyzing tools like grafana or zabbix .. that can ease the process
I think what you are asking for is scripted pipeline: https://jenkins.io/doc/book/pipeline/
This allows you to write Groovy code to execute on the Jenkins master. You can do almost anything you want from there, just like any programming language.

How to set a task variable from a remote powershell script?

From a release definition,
When I execute the following powershell script with the powershell task, it works
successfully and I can retrieve the value from the following tasks in the release definition :
Write-Host "##vso[task.setvariable variable=sslThumbprint]toto"
When I execute the exact same script thanks to the "Powershell on target machine task", I don't get any value.
How can I set a variable from a powershell script that do not execute on the agent machine ?
Regards,
This can only be done in case of hosted agents. If you are using hosted agents then only you will be able to run the commands on the agent's machine.
Microsoft as of now provides no feature for doing this.

Use IDE to generate pipline script for jenkins

I need to overload jenkins functions to debug pipeline script in IDE.
I'm new to java/groovy etc. I'm going to write a several hundred lines scripted pipeline. Groovy is based on java. As I'm new I prefer the function name completion/suggestion and a debugging feature would be awesome where I can walk through the lines step by step and see what is in the vars.
I set up a eclipse Luna with the groovy-plugin. Which is actually working =)! (for newest eclipse the plugin is not yet ready). Also debugging is quiet cool!
But special jenkins expressions will still throw errors.
node(MasterName){ ... }
sh
...
Is there a chance to overload those functions?
Just killing the error, not performing any actions. Maybe converting it to a print like "I'm executing script XYZ" or "Switching to node BLUBB"?
The outcome should be a copy paste script, for checking in and running with jenkins without major changes.
Is there any better way?
In the end it turned out, that even if you omit the pipeline specific expressions, you will still have trouble. In some more complex situations e.g. calling a constructor with super in a extended class and executing a function afterwards, it does not behave the same then in my local python interpreter.
So what I did was an error to assume, my Ubuntu system default groovy interpreter would work the same like the jenkins interpreter. It would be nice to run a debugger inside the jenkins environment, or going step by step through the pipeline script and see how it is actually working without a print in every second line.

Jenkinsfile Pipeline dynamic environment modification at runtime

I need to get GitVersion.exe variables in my Jenkins pipeline.
The GitVersion documentation gives a hint on how to do that. Essentially call gitversion /output buildserver.
This call does add the variables to the current step and they are lost once the step completes. I can show this call executes when combining a set command in the same bat execution. The second set shows the variables are gone from the environment.
bat 'nuget install GitVersion.CommandLine -OutputDirectory c:/packages -Version 3.6.5'
bat 'c:/packages/GitVersion.CommandLine.3.6.5/tools/GitVersion.exe /output buildserver && set'
bat 'set'
The documentation of GitVersion is aware of that and suggests to use EnvInject.
Installing the plugin and executing the same pipeline did not change the result. I read that the Plugin is not made for pipelines so that may have something to do with it.
Pipelines support a syntax for environment.
Following that syntax I can set static variables at the top of my pipeline like this:
environment {
ASuperVariable = 'MySuperVariable'
}
What I need is combining those calls so that I can add run time variables to the Jenkinsfile pupeline.
environment {
bat 'gitversion /output buildserver'
}
Now obviously the above call is not even syntax correct. Is there a way to mark a section so that the contained environment changes are available for other steps?
EDIT:
This is still unsolved. At the moment I need to create a batch script and pass the tool into it as an argument. Inside the batch I can call the tool to add to the environment of the batch script and use that wile the batch is running. A Multi line batch in the Jenkins file could be a solution if the process remains the same over all the multiple lines.
Not sure whether you would be able to use scripted pipeline or at least a script block inside declarative. It'd be quite easy doing so:
withEnv(['ASuperVariable=MySuperVariable']) {
echo env.ASuperVariable
}
Or when calling a windows cmd script:
node('win') {
withEnv(['ASuperVariable=MySuperVariable']) {
bat 'echo %ASuperVariable%'
}
}

Resources