Parameterized Jenkinsfile from SCM + Bitbucket webhook

Hi,
I have a bit of complicated setup I try to make,
I got a Jenkinsfile in my BitBucket that takes in parameters, screenshot below:


The jenkinsfile itself looks like this, its a proof of concept so its simple logic

node('kubeagent') {
    // Define parameters
    properties([
        parameters([
            choice(choices: ['daniel','dmytro'], description: 'Specify the component', name: 'COMPONENT'),
        ])
    ])
    stage('Print Hello') {
        echo "hello ${COMPONENT}"
    }
}

So this works, I can manually trigger a job and the jenkinsfile is used etc.
Now imagine that the parameter (daniel/ dmytro …) is used to define a different BitBucket repository .

I want to set a webhook that will connect to the current Jenkinsfile setup;

Example workflow:

  1. Go to daniel bitbucket repository and commit a change
  2. The change in repo is detected by webhook and jenkins fetches the repo name as a parameter and feeds it as choice parameter in the next step in the Jenkinsfile job build.
  3. The Jenkinsfile is run with the parameter being repo name.

Is this even possible?
Any leads are much appreciated!

Hello @Danielstankw,

I think you’ll need to modify your Jenkins pipeline to handle webhook triggers and pass repository information as parameters.
Here is an untested pipeline:

def getRepoName() {
    // Extract repository name from webhook payload
    def payload = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.github.webhook.GitHubWebHookCause)?.githubEvent
    if (payload) {
        return payload.repository.name
    }
    // Fallback to parameter if not triggered by webhook
    return params.COMPONENT ?: 'daniel'
}

pipeline {
    agent { label 'kubeagent' }
    
    parameters {
        // Default parameters for manual triggers
        choice(name: 'COMPONENT', choices: ['daniel', 'dmytro'], description: 'Specify the component')
    }
    
    triggers {
        // Add webhook trigger
        GenericTrigger(
            genericVariables: [
                [key: 'REPOSITORY_NAME', value: '$.repository.name']
            ],
            causeString: 'Triggered by BitBucket push',
            token: 'your-webhook-token',  // Use this token in BitBucket webhook URL
            printContributedVariables: true,
            printPostContent: true
        )
    }
    
    stages {
        stage('Setup') {
            steps {
                script {
                    // Set COMPONENT based on webhook or parameter
                    env.COMPONENT = getRepoName()
                    echo "Processing repository: ${env.COMPONENT}"
                }
            }
        }
        
        stage('Print Hello') {
            steps {
                echo "hello ${env.COMPONENT}"
            }
        }
    }
}

To implement this solution, you would have to:

  1. Install required Jenkins plugins:
  1. Configure BitBucket webhook:
  • Go to your BitBucket repository settings
  • Add webhook with URL: http://your-jenkins-url/generic-webhook-trigger/invoke?token=your-webhook-token
  • Select “Push” events
  1. The pipeline should from now on:
  • Accept manual triggers with parameter selection
  • Automatically trigger on BitBucket pushes
  • Extract repository name from webhook payload
  • Use the repository name as the COMPONENT parameter

Remember to:

  • Replace your-webhook-token with a secure token
  • Make sure Jenkins has network access to BitBucket :person_shrugging:
  • Configure appropriate security settings in Jenkins :closed_lock_with_key:

This setup should hopefully automatically trigger the pipeline when changes are pushed to any of the monitored repositories, using the repository name as the component parameter. :crossed_fingers: