Search code examples
jenkinsjenkins-pipelineaws-event-bridge

How to send an event from Jenkins to AWS EventBridge?


I am looking to achieve something like this.

Jenkins --(json event)--> AWS eventbride event---> multiple targets(cross account EventBridge event bus)

Then every consumer account for this event will be triggering an API Gateway for further processing.

So is there any way to send an event from Jenkins to AWS EventBridge?


Solution

  • You can send an event from Jenkins to AWS EventBridge with AWS CLI or an SDK.

    • create an EventBridge rule for Jenkins

    • Install AWS CLI on Jenkins server, or add the AWS SDK to your Jenkins build environment

    • use script concept that sends an event to the EventBridge rule - below

    • use the AWS CLI or SDK to authenticate with AWS using, send the event to the EventBridge using the put-events API - below

      #!/bin/bash
      
      # Set AWS credentials
      export AWS_ACCESS_KEY_ID=<your-access-key-id>
      export AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
      export AWS_DEFAULT_REGION=<your-region>
      
          # Set the EventBridge rule name and JSON event data
          RULE_NAME=<your-rule-name>
          EVENT_DATA='{
            "source": "jenkins",
            "detail-type": "jenkins-build",
            "detail": {
              "project": "your-project",
              "branch": "master",
              "status": "success"
            }
          }'
      
      # Send the event to EventBridge using the AWS CLI
      

      aws events put-events --entries "{\"Source\": \"jenkins\", \"DetailType\": \"jenkins-build\", \"Detail\": \"$EVENT_DATA\", \"EventBusName\": \"$RULE_NAME\"}"