Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Jenkins Groovy scripts directly from Intellij or Eclipse

I have a Groovy repository which contains my Jenkins pipeline's Groovy code.

Currently, I am making changes in an IDE, commiting them to the repository, going to the Jenkins instance, manually triggering a Jenkins job, and checking to see if all of the changes all working. This is taking a lot of time.

Is there a way to do all of this from the IDE itself?

like image 796
Mukesh Singal Avatar asked Nov 23 '17 18:11

Mukesh Singal


People also ask

How do I run a Groovy script in IntelliJ?

To run a Groovy script, from the context menu in the editor, select Run 'name' Ctrl+Shift+F10 .

How do I run a Jenkins Groovy script?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.

How do I open Groovy in IntelliJ?

In the Project tool window, right-click the src directory and select New | Groovy Script. IntelliJ IDEA opens the file in the editor.


2 Answers

I would suggest to treat your pipeline code like some other code in IT. What are you doing now could be called "manual integration tests" because you are making your code changes and check how that code integrate with other components (like shell commands, jenkins plugins, etc.) on jenkins - this development loop is long and not efficient. So my proposition for you is to write simple unit tests using this framework: https://github.com/jenkinsci/JenkinsPipelineUnit So you can test your pipelines on your machine without any interaction with jenkins.

If you think that it's not proper way for you I would suggest to mix using this plugin for running jobs directly from IntelliJ: https://github.com/programisci/jenkins-control-plugin/ and of course IntelliJ git integration to commit your changes to repository.

like image 132
Jakub Bujny Avatar answered Nov 15 '22 04:11

Jakub Bujny


For executing from the IDE, an option is to create some automation around using the Jenkins CLI. You should be able to see the CLI commands at http://your-jenkins-url/cli.

java -jar jenkins-cli.jar -s https://jenkins.physiq.zone/ replay-pipeline JOB [-n (--number) BUILD#] [-s (--script) SCRIPT]

Replay a Pipeline build with edited script taken from standard input

JOB : Name of the job to replay.

-n (--number) BUILD# : Build to replay, if not the last.

-s (--script) SCRIPT : Name of script to edit, such as Script3, if not the main Jenkinsfile.

For example, in IntelliJ you could use a Run Configuration that:

  • Downloads the CLI JAR
  • Executes it with the path to the local file with certain parameters

You can also write a script, Gradle build, or something else that wires into the IDE to pull the CLI JAR and execute a job with your local pipeline code.

For testing you may want to use https://github.com/jenkinsci/JenkinsPipelineUnit as already brought up, or a Gradle plugin that I maintain at https://github.com/mkobit/jenkins-pipeline-shared-libraries-gradle-plugin which uses the previously mentioned library for unit testing and the jenkinsci/jenkins-test-harness for integration testing.

like image 29
mkobit Avatar answered Nov 15 '22 04:11

mkobit