Reputation: 7581
We are working in parallel on a number of Git branches.
When pushing a specific Git branch, how can we start a Jenkins item/job that will trigger the build of that specific branch?
As an example: we push a Git branch "feature-abc" ==> this should trigger a Jenkins build job using/pulling that branch "feature-abc".
For each new feature we have a branch, so the number of branches is not fixed.
So, I think it is different than this answer with a manual action.
Upvotes: 3
Views: 4360
Reputation: 7581
A much better alternative for building Jenkins jobs via the GUI is making them as Jenkins pipelines.
The result of the (below) simple demo configuration is that each time you push updates to the Git repo, a Jenkinsfile for that (changed) branch is started. In the Jenkinsfile you can output the branch name.
How? You can easily create such a configuration yourself in a few minutes. In this case I have a Jenkins server running on my local PC. So, this Jenkins server is by default not accessable for a webhook in the Git project. (Notice: you can use Webhook relay to simulate real webhooks). When the Git server can access the Jenkins server, then you should definitely use a webhook.
.
node {
stage('Preparation') {
checkout scm
}
stage('Who am i?') {
echo "This job was triggered by a Git push to branch: ${env.BRANCH_NAME}"
}
}
Upvotes: 1
Reputation: 1401
If the use case is to trigger a build for every git push, you can add a a git pre-receive hook, which will trigger the build, once git-push is completed.
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
#Trigger based on branch name "$branch" or pass this parameter to trigger the build
curl -X POST -u "user" "http://<jenkins_url>/job/buildWithParameters?branch_name=$branch"
done
Upvotes: 1