Jenkins Freestyle Integration with Ansible and Hivel Webhook

This guide walks you through integrating Jenkins Freestyle pipelines with Hivel using an Ansible playbook and webhook reporting. You’ll learn how to trigger a playbook, capture build and stage metadata, and send it to Hivel’s API endpoint for monitoring and analytics.

🧰 Prerequisites

Make sure the following are set up on your Jenkins server (typically an EC2 instance):

  • ✅ Jenkins installed and accessible

  • ✅ Admin access to Jenkins UI

  • ✅ Ansible installed and available in PATH

  • ✅ Git configured (for Git metadata collection)

📦 Required Plugin

Install the following plugin:

Plugin Name
Description

Allows running Groovy scripts after a build

Required Credentials for Integration

During the integration, you'll need:

  • Organization ID (Generated in the Hivel application)

  • API Key (Generated in the Hivel application)

Retrieving Your Organization ID and API Key

  1. Open the Hivel application and navigate to Integrations under the Settings.

  2. Locate the Jenkins Integration card and click Connect.

  3. Follow the given steps to start your integration.

  4. By clicking on generate your Organization ID and API Key will be generated.

  5. Use these fields while integrating with Jenkins. The steps for where to apply them are provided on the following page.

  6. If you find it difficult, don’t worry! You can always reach out to us for assistance at support@hivel.ai.

Configuring Credentials in Jenkins

  1. Log in to your Jenkins application.

  2. From the side navigation, select Manage Jenkins.

  3. Click on Credentials.

  4. Click on global under Stores scoped to Jenkins

  5. Choose Add Credentials - you will need to do this twice (once for the Organization ID and once for the API Key).

  6. From the dropdown, select Secret text.

  7. After selecting Secret text from the dropdown, fill in the details twice as follows:

    1. Adding the API Key

      1. ID: hivel-jenkins-api-key

      2. Secret: {your API key} (copied from Hivel)

      3. Description: This is the Jenkins API key from Hivel

      4. Click Save

    2. Repeat the process again for Adding the Organization ID

      1. ID: hivel-org-id

      2. Secret: {your Organization ID} (copied from Hivel)

      3. Description: This is the Jenkins Organization ID from Hivel

      4. Click Save

  8. Once you have completed the above steps, your Jenkins Global Credentials should display the following entries:

Step 2: Bind Secrets in Job Configuration

  1. Open your Freestyle job(pipeline in your dashboards)

  2. Go to the Environment section

  3. Check Use secret text(s) or file(s)

  4. Click Add and bind the credentials like this:

Variable Name
Credentials ID

HIVEL_ORG_ID

hivel-org-id

HIVEL_JENKINS_API_KEY

hivel-jenkins-api-key

Step 3: Install Ansible on Jenkins Server

Ensure Ansible is installed on the EC2/server where Jenkins is hosted.

For Amazon Linux 2:

sudo amazon-linux-extras enable ansible2
sudo yum install -y ansible

Run ansible --version on the Jenkins server to confirm installation.

Step 4: Add Build Step – Execute Ansible Playbook

Under Build Steps → Execute shell, add:

# Run Ansible playbook with JSON output
ANSIBLE_STDOUT_CALLBACK=json \
ansible-playbook ansible/dummy-stages.yml \
-i "localhost," --connection=local \
> ansible-output.json 2>&1 || true

This ensures that:

  • The playbook runs locally

  • Output is saved in JSON format to ansible-output.json

  • Even on errors, the script doesn’t fail the job

Note : replace the path of your ansible file with “ansible/dummy-stages.yml”. Invoking ansible like this will only help us to get insights from ansible playbook.

Step 5: Add Groovy Postbuild Script for Webhook Reporting

Under Post-build Actions:

  1. Add → Groovy Postbuild

  2. Paste the Groovy webhook script (Full Version) (provided below)

  3. Uncheck the box labeled Use Groovy Sandbox

  4. Approve script (open will appear once you enter script in groovy text box)

  5. Save the job

Add groovy postbuild

{
  "org_id": 12345,
  "job_name": "freestyle-ansible-job",
  "build_number": 12,
  "status": "SUCCESS",
  "build_url": "https://jenkins.example.com/job/freestyle-ansible-job/12/",
  "start_time": "2025-04-08T10:12:00Z",
  "end_time": "2025-04-08T10:12:45Z",
  "duration": 45.25,
  "git": {
    "commit": "abc123...",
    "branch": "main",
    "author": "John Doe",
    "author_email": "john@example.com",
    "commit_message": "Add API integration",
    "repository": "git@example.com:project/repo.git"
  },
  "triggered_by": "john.doe",
  "stages": [
    {
      "stage_name": "Ensure packages installed",
      "start_time": "2025-04-08T10:12:05.000000Z",
      "end_time": "2025-04-08T10:12:10.000000Z",
      "duration": 5.0,
      "status": "SUCCESS",
      "message": "All packages present"
    }
  ]
}

Last updated

Was this helpful?