Search code examples
restjenkinshttprequest

How to call REST API to external server from Jenkins when finish build


I want to know how to call rest api to external server from jenkins when finish build.

I using HTTP Request plug-in, but there is no HTTP Request on 'Post-build action'

is it impossible?

post-build options dialog


Solution

  • AFAIK you cannot execute additional plugins that are not listed using a Free Style Job. The best option for you, is to convert your Free Style Job to a Declarative Pipeline. You can read more about Declarative Pipelines from here. Once you move to a Declarative Pipeline you can invoke the HTTP Request Plugin like below.

    pipeline {
        agent any
    
        tools {
            // Install the Maven version configured as "M3" and add it to the path.
            maven "M3"
        }
    
        stages {
            stage('Build') {
                steps {
                    // Get some code from a GitHub repository
                    git 'https://github.com/jglick/simple-maven-project-with-tests.git'
    
                    // Run Maven on a Unix agent.
                    sh "mvn -Dmaven.test.failure.ignore=true clean package"
    
                }
    
                post {
                    success {
                       script {
                           echo "This will execute if the Job is Successful"
                           def response = httpRequest 'http://localhost:8080/jenkins/api/json?pretty=true'
                        }
                    }
                }
            }
        }
    }