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:
Go to daniel bitbucket repository and commit a change
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.
The Jenkinsfile is run with the parameter being repo name.
Is this even possible?
Any leads are much appreciated!
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}"
}
}
}
}
Add webhook with URL: http://your-jenkins-url/generic-webhook-trigger/invoke?token=your-webhook-token
Select “Push” events
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
Configure appropriate security settings in Jenkins
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.