Working with Jenkinsfile in Intellij IDEA

This post describes how I write/debug Jenkins Pipelines on different projects (Android, Groovy). There should be definitely other ways/environments of doing this. It’s just what’s convenient for me.

Intellij IDEA IDE

Let’s say you want to work on Jenkins Pipeline configuration on a project using IntelliJ IDEA.

  • Open a project in IntelliJ IDEA
  • Create Jenkinsfile in project root
    • it will be associated with Groovy language by default
    • or you can associate the file with Groovy using Associate with file type action from IDE
  • Install standalone Groovy
  • Add Groovy to PATH:
    • export GROOVY_HOME={path_to_groovy_folder}
    • export PATH=$PATH:$GROOVY_HOME/bin
  • Configure Groovy SDK to enable autocompletion in Jenkinsfile
  • Use pipeline.gdsl to have Pipeline syntax autocompletion in Jenkinsfile
    • create pipeline.gdsl file from http://{YOUR_JENKINS_ADDRESS}/job/{YOUR_PIPELINE_JOB}/pipeline-syntax/gdsl

      gdsl sidepane

      //The global script scope
      def ctx = context(scope: scriptScope())
      contributor(ctx) {
      method(name: 'addToClasspath', type: 'Object', params: [path:'java.lang.String'], doc: 'Add file path to Groovy classPath')
      method(name: 'ansiColor', type: 'Object', params: [colorMapName:java.lang.String, body:'Closure'], doc: 'Color ANSI Console Output')
      ...
      
    • place pipeline.gdsl somewhere in src folder in your project so that it’s recognized properly
    • add pipeline.gdsl to .gitignore to reduce noise in the repo

Once you’ve done all these steps you’ll have autocompletion in Jenkinsfile which will help with the development of Jenkins pipelines.

Groovy only autocompletion

jenkinsfile no autocompletion

Groovy + pipeline.gdsl autocompletion

jenkinsfile with autocompletion

If you’re curious how pipeline.gdsl is generated, please refer to Jenkins workflow-cps-plugin Snippetizer/gdsl.groovy.

Local testing

We need a playground where we can configure Jenkinsfile step-by-step and test our changes.
Options available:

  • Create a Jenkins Pipeline job and edit Jenkinsfile from Jenkins UI
  • Use remote repository and work in a separate branch on Jenkinsfile configuration

Things are getting more complicated if you have a project with Jenkinsfile and steps implementation from Jenkins Shared Library.
In this case, there’s no easy way to edit both Jenkinsfile and Shared Library and test changes on the fly.
To simplify this process a bit, we can install Jenkins instance locally and use local Git repo (see below).

Local testing of Pipeline project

  1. Install Jenkins (I suggest as a separate jenkins user)
  2. Clone Git project being in a default user
  3. Get project relative path by running cd GIT_PROJECT_FOLDER && pwd
  4. Login to jenkins user from command-line using sudo su - jenkins command
  5. Create a symlink to Git project ln -s SOURCE DESTINATION, where SOURCE is the path from #3 step and DESTINATION is any location from jenkins user
  6. Use the symlink as Git repository in Jenkins job (the symlink was created in Desktop folder in my case)
    file:///Users/Shared/Jenkins/Desktop/git-project-symlink

    git repository symlink

  7. Configure Jenkins Shared Library and point it to your work-in-progress branch

    shared library configuration

In case Jenkins Shared Library is shared with other teams, you will need to work in a fork where you can modify it.

Having this setup we can:

  • modify Jenkinsfile in original repository, make a commit
  • modify Jenkins Shared Library code, make a commit
  • trigger build manually in Jenkins which will fetch repository from local machine.
    or trigger build automatically on commit to the local repository. For that, we need to configure Trigger builds remotely and trigger build from git post-commit hook using curl command.

I find myself really productive having this setup and it speeds up significantly the process of testing Jenkinsfile + Shared Library configurations on the local machine.

Eclipse IDE

There’s also Jenkins Editor Eclipse IDE plugin but I never tried it myself since I’m not using Eclipse for a long time already.


Resources

comments powered by Disqus