I have configured Jenkins for my Rails 3 application to run test suites. While there are many tutorials online about setting up build triggers when code is pushed to GitHub, I need something more specific.
What I’m looking for is a way to automatically build any fresh branch that gets pushed to my GitHub repository.
Here’s my workflow: I have a main repository with origin/master. When I clone it locally, create a feature branch, make some changes, and push it using git push -u origin feature_branch, I want Jenkins to detect this new branch automatically.
My goal is to have Jenkins build this newly pushed branch, and if all tests pass successfully, it should automatically merge the changes back into origin/master.
I’m currently using the GitHub and Git plugins in Jenkins, but they seem to require specifying a particular branch name in the configuration. Instead, I need Jenkins to handle any new branch dynamically without manual intervention.
What’s the best approach to set up this automated workflow?
I had the same problem with a Django project. What worked for me was using Jenkins multibranch pipelines with a Jenkinsfile in your repo’s root. Jenkins will automatically detect and build new branches without any manual setup. First, create a multibranch pipeline job and connect it to your GitHub repo. Set the branch sources to use * so it catches all branches. Then add a Jenkinsfile to define your build steps and test execution. For automatic merging, I added conditional logic to the Jenkinsfile. It checks if tests pass on feature branches, then merges to master using git commands. Be careful with auto-merges though—add extra checks like branch naming rules or specific success criteria. The multibranch setup eliminates manually defining branch names since Jenkins creates jobs for each branch it finds. Also make sure your GitHub webhook triggers branch indexing when you push new branches.
Try GitHub Actions instead of Jenkins for this. I switched from a similar Jenkins setup after hitting reliability issues with webhook timing and merge conflicts. Actions lets you trigger on any branch push with on: push: branches: ['**'] and handle the whole build-test-merge cycle in one workflow file. GitHub automatically detects new branches without extra scanning or webhook setup. For auto-merge, use the GitHub CLI or REST API with branch protection rules. You’ll skip the headache of managing Jenkins credentials for git operations and avoid race conditions when multiple devs push branches at once. Since workflows run on GitHub’s infrastructure, there’s zero delay between push detection and build start.
there’s another approach - use the Generic Webhook Trigger plugin. set it up to listen for github push events and use regex patterns to catch when branches get created. configure your github webhook to send payloads on push events, then jenkins pulls the branch names straight from the json payload. works really well for handling dynamic branches without needing the multibranch setup.
Just use the Jenkins Job DSL plugin with periodic repo scanning. Set up a seed job that checks your GitHub repo every few minutes and auto-creates jobs for new branches. Way easier than webhooks - no GitHub config headaches and you won’t miss triggers when the network acts up.
The Branch API plugin plus GitHub Branch Source plugin work great for this. Set up a multibranch organization folder that scans your GitHub org or specific repos. Configure it to discover all branches and pull requests, then use repository scanning intervals or webhooks to trigger builds. For auto-merging, I’d use the Pipeline stage view plugin with conditional steps that only merge after tests pass. Write pipeline scripts to check branch patterns and run git merge commands using Jenkins credentials. Just make sure your Jenkins service account can read branches and push merge commits back to master. The scanning approach beats other methods for finding branches.