AlexD
AlexD

Reputation: 307

Develop jenkins groovy script in Eclipse

Is it possible to develop a groovy script (pipeline) for Jenkins in Eclipse? I want to connect to a running Jenkins instance and create / start some jobs. How can i achieve this?

Upvotes: 5

Views: 3533

Answers (2)

David
David

Reputation: 1296

To work with Jenkins Pipeline, I have setup Eclipse like this:

  1. Download and extract Jenkins.war Distribution (it's just a zip file with *.war file extension) from jenkins.io, currently version 2.361.1 LTS.

  2. Run the jenkins war file.

    • 2.1. Open a terminal and run java -jar jenkins.war.You will see that Jenkins will ask for the initial configuration and it will provide an initial password.

    • 2.2. Copy the initial password showed in the terminal.

    • 2.3 Open http://localhost:8080 in a WebBrowser and paste the initial password you have copied from the terminal.

    • 2.4. Follow the steps to install the default plugins. All of these files will be saved in $HOME/.jenkins folder.

  3. Install Eclipse Java IDE Version 2022-03 (4.23.0), I chose flavor: "Eclipse IDE for Java Developers"

  4. Install Eclipse Groovy Plugin 4.5.0 (via Help->Marketplace search for groovy)

  5. Create an Eclipse User Library via Window -> Preferences:

    Then go to Java -> Build Path -> User Libraries, add new User Library with name "Jenkins Pipeline". Then add the following "External Jars..." to this library.

    Uncompress jenkins.war (tar xvf jenkins.war) file and add the following files:

    • 5.1. jenkins/WEB-INF/lib/*.jar Add these libraries from the plugins directory of jenkins home:

    • 5.2. ~/.jenkins/plugins/workflow-cps-global-lib/WEB-INF/lib/*.jar

      If you cannot find this directory it means you are running a new version of Jenkins. In this case, you should use: ~/.jenkins/plugins/pipeline-groovy-lib/WEB-INF/lib/*.jar

    • 5.3. ~/.jenkins/plugins/workflow-cps/WEB-INF/lib/*.jar

    • 5.4. I also add junit to the library because it is often used:

      ~/.jenkins/plugins/junit/WEB-INF/lib/*.jar

  6. Create your groovy pipeline project:

    File -> New -> Project... -> Groovy -> Groovy Project.

    Then add the User Library "Jenkins Pipeline" to the Build Path:

    Right click the groovy project -> Build Path -> Add Libraries -> User Library

  7. Finally add more libraries from the plugins folder to your project according to your needs

Upvotes: 1

TheEllis
TheEllis

Reputation: 1736

Jenkins does not have strong IDE support in any IDE. However, Eclipse (as well as most major java IDE's) does have a groovy plugin and you can import the core jenkins jars to get some auto-completion. At the very least, the IDE gives you autoformatting, with is of some help. Once you've developed your script, you will have to copy it out to jenkins to test.

Upvotes: 6

Related Questions