github.com/xgoffin/jenkins-library@v1.154.0/vars/healthExecuteCheck.groovy (about)

     1  import static com.sap.piper.Prerequisites.checkScript
     2  
     3  import com.sap.piper.GenerateDocumentation
     4  import com.sap.piper.ConfigurationHelper
     5  import com.sap.piper.Utils
     6  import groovy.transform.Field
     7  
     8  @Field String STEP_NAME = getClass().getName()
     9  
    10  @Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
    11  
    12  @Field Set STEP_CONFIG_KEYS = [
    13      /** Optionally with `healthEndpoint` the health function is called if endpoint is not the standard url.*/
    14      'healthEndpoint',
    15      /**
    16       * Health check function is called providing full qualified `testServerUrl` to the health check.
    17       *
    18       */
    19      'testServerUrl'
    20  ]
    21  
    22  @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
    23  
    24  /**
    25   * Calls the health endpoint url of the application.
    26   *
    27   * The intention of the check is to verify that a suitable health endpoint is available. Such a health endpoint is required for operation purposes.
    28   *
    29   * This check is used as a real-life test for your productive health endpoints.
    30   *
    31   * !!! note "Check Depth"
    32   *     Typically, tools performing simple health checks are not too smart. Therefore it is important to choose an endpoint for checking wisely.
    33   *
    34   *     This check therefore only checks if the application/service url returns `HTTP 200`.
    35   *
    36   *     This is in line with health check capabilities of platforms which are used for example in load balancing scenarios. Here you can find an [example for Amazon AWS](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-healthchecks.html).
    37   */
    38  @GenerateDocumentation
    39  void call(Map parameters = [:]) {
    40      handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
    41          def script = checkScript(this, parameters) ?: this
    42          String stageName = parameters.stageName ?: env.STAGE_NAME
    43  
    44          // load default & individual configuration
    45          Map config = ConfigurationHelper.newInstance(this)
    46              .loadStepDefaults([:], stageName)
    47              .mixinGeneralConfig(script.commonPipelineEnvironment,  GENERAL_CONFIG_KEYS)
    48              .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
    49              .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
    50              .mixin(parameters, PARAMETER_KEYS)
    51              .withMandatoryProperty('testServerUrl')
    52              .use()
    53  
    54          new Utils().pushToSWA([step: STEP_NAME], config)
    55  
    56          def checkUrl = config.testServerUrl
    57          if(config.healthEndpoint){
    58              if(!checkUrl.endsWith('/'))
    59                  checkUrl += '/'
    60              checkUrl += config.healthEndpoint
    61          }
    62  
    63          def statusCode = curl(checkUrl)
    64          if (statusCode != '200') {
    65              error "Health check failed: ${statusCode}"
    66          } else {
    67              echo "Health check for ${checkUrl} successful"
    68          }
    69      }
    70  }
    71  
    72  def curl(url){
    73      return sh(
    74          returnStdout: true,
    75          script: "curl -so /dev/null -w '%{response_code}' ${url}"
    76      ).trim()
    77  }