Jenkins

Jenkins changeRequest

A Jenkins ChangeRequest or “change request” refers to an action triggered in response to a change in the source code repository connected to a Jenkins server.

This can include building a new software version, running tests, and deploying the updated code to a staging or production environment.

We generally configure change requests as part of a Jenkins pipeline which can be triggered automatically or manually, depending on the project’s specific requirements.

To configure a Jenkins change request feature, we can use the changeRequest() function inside a when block.

Example

To configure a change request in Jenkins, we need to create a Jenkinsfile that defines the Jenkins job.

To set up a change request in the Jenkinsfile, we can use the when block to specify when the change request should be triggered.

Take the example pipeline shown below:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo "Build Stage running…"
      }
    }
    stage('Test') {
      when {
        changeRequest()
      }
      steps {
        echo "Change detected and running the Test stage."
      }
    }
    stage('Deploy') {
    when {
      changeRequest()
    }
    steps {
      echo "Change detected and running the Deployment Stage."
    }
   }
  }
}

In this example, the Test and Deploy stages will only be triggered if a change request is detected. You can also use the changeRequest function to specify additional criteria for triggering the change request, such as the branch or repository in which the change was made.

We can also specify additional criteria for triggering the change request using the changeRequest function with parameters. For example:

when {
  changeRequest targetBranch: 'master', approved: true
}

This will trigger the stage only when a change request is made to the master branch and has been approved.

Other function parameters we can use include:

  1. Id – specifies the ID of the change request
  2. Approved – Specifies a Boolean value indicating whether the change request has been approved
  3. Author – The author of the change request.
  4. targetBranch – The target branch of the change request.
  5. Title – The title of the change request.
  6. URL – The URL of the change request.

We can use these parameters to specify the conditions under which the change request should be triggered. For example, we might want to trigger a stage only when a change request made by a specific user has been approved or when a change request targeting a particular branch is made.

Conclusion

This is on how you could use the changeRequest() function in Jenkins to perform a given stage when a change is detected on a repository. Check our tutorial on Jenkins when to explore further.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list