jenkinsjenkins-generic-webhook-trigger

Jenkins Webhook Header as Argument to Shell script


I'm trying to trigger a Jenkins job through the webhook using the curl command whenever there is EC2 Spot Instance Interruption Warning with below sample event. All this will be done in AWS lambda from CloudWatch Event Trigger.

{
  "version": "0",
  "id": "1e5527d7-bb36-4607-3370-4164db56a40e",
  "detail-type": "EC2 Spot Instance Interruption Warning",
  "source": "aws.ec2",
  "account": "123456789012",
  "time": "1970-01-01T00:00:00Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ec2:us-east-1b:instance/i-0b662ef9931388ba0"
  ],
  "detail": {
    "instance-id": "i-0b662ef9931388ba0",
    "instance-action": "terminate"
  }
}

My aim is to get the instance-id from the event and pass it as a header to the jenkins webhook where the jenkins job gets triggered and this header has to be sent as an argument to the underlying python script in the jenkins job.

I tried the below approach which didn't give me success. And am not sure if this is how it is done. :)

curl -H 'param: instance' https://jenkins.url/generic-webhook-trigger/invoke\?token\=jenkins-job

Generic Jenkins webhook trigger is configured as below.

enter image description here

The final python script in the job is configured as below.

python maintenance.py $.param

I'm trying to get the final script similar to below. Please let me know if you know of any approach how can I get this done. TIA

python maintenance.py i-0b662ef9931388ba

Solution

  • I fixed this by adding the following.

    Add string parameter under This project is parameterized as shown below.

    enter image description here

    Next under Generic Webhook Trigger I have added Header parameters

    enter image description here

    Now, we can directly pass this param in the build command using $param.

    The curl command is now modified to below.

    curl --location --request POST 'https://jenkins.url/generic-webhook-trigger/invoke\?token\=jenkins-job' --header 'Content-Type: application/json' --header "param: $EVENT_DATA" --data-raw ''
    

    What did I learn from this? I was too lazy to read the documentation and finally figured it out only after reading it.