How to config Jenkins to build any branch whenever it committed to GitHub - github

I'd like to config jenkins that builds any branch whenever it committed to GitHub.
I did as below:
At Source Code Management, I chose Git and Branch Specifier was **
At Build Triggers, I chose Build when a change is pushed to GitHub
I tried to commit to branch (ex: fixBugAAA), but jenkins built all branches.
What did I do wrong? I just need to build only one branch I committed (ex: in this case fixBugAAA). How to do that?
Thanks.

Related

Azure DevOps CI/CD pipeline: Revert commit on failure

I am currently building an Azure DevOps CI/CD pipeline and if it fails, I don't want to keep the code that lead to the fail in my repository. So, if the pipeline fails, I want the repo to be reverted to the last version before that commit. I can't find any help on that. How does this option look like and how can I add this option to my .yaml file?
Thank you so much.
Normally, you can use the git revert command to revert some existing commits.
In your pipeline, you can check out the git repository and the branch where you made the changes. Then run the git revert command to 'undo' some commits.
For more details, you can reference the following articles:
Git - git-revert Documentation
Git Revert
However, as #GeralexGR has suggested, it is recommended that you'd better create a develop branch based on the default branch (main/master) and make changes on this branch. Then build and test the code on the develop branch. Once everything is good on the develop branch, you can create a Pull Request to merge the changes from the develop branch to the default branch.

Is it possible to set source dynamically in Azure Repos Git?

I'm looking for a solution to dynamically select a branch to build in the Azure pipeline. I have Azure Repos Git where I select project, repository and default branch. I would like to be able to select branch based on a variable.
What I'm trying now is to use the Command Line task and fire a git checkout command (e.g. branch is a variable):
git checkout $(branch)
I can't confirm it working yet but still I confirm it works but I feel that there is a better option than checking out default branch and then switching branch with the command line.
Update:
If you want to have single pipeline that can build different branches (version branches) for different branches, you could just specify them in the trigger of branch filters. This will not build all branches.
The branch you select in build definition is just the default branch used when you Queue New Build manually. OTOH the build trigger establish which branch to download, e.g. if the build has been triggered by a git push on branch develop then that is the one checkout'ed and built. Take a look at this link: Get the sources from the branch that triggered the build in Team Services
Besides, you could disable the default get source step.Then use you own powershell script and git command to get source code manually(just what you want) and check out branch, finally build based on your variable.
For YAML, to disable the default "Get Sources" just specify none in
the checkout:
checkout: none
For UI, please refer my reply in this question:
Is it able to ignore/disable the first step Get source in vNext Build?
Assuming you're choosing the default branch. That doesn't mean that
it's the only branch that can be built by that build definition.
You can choose which branches to monitor for CI (on the Triggers tab,
you can add multiple branch filters to represent the branches you wish
to build on commit), and the appropriate branch will be checked out
when the build is triggered. You can also choose an alternate branch
when manually queuing the build.
Source Link: Get Sources from multiple branches
If you want to dynamically select default branch as below, this is not available at present.
This is the branch that you want to be the default when you manually
queue this build. If you set a scheduled trigger for the build, this
is the branch from which your build will get the latest sources.
The default branch has no bearing when the build is triggered
through continuous integration (CI). Usually you'll set this to be
the same as the default branch of the repository (for example,
"master").
There is a related user voice here: When triggering a build, use the same branch as the trigger build. You could kindly vote up and track the process.

Got error trigger Jenkins builds by pushing to Github

I config Jenkins deploy PHP app by trigger the script deployment whenever developers pushed their code to Github with domain .mydomain.com. If developer create new branch and push some changes to that branch, it works perfectly fine, the strange thing is when developer create new branch and push to server (don't change anything just
create new branch and push) it doesn't trigger the script deployment.
Could you please help?
In Git configuration there is a field Branch Specifier (blank for default): if you put there ** it will build all branches from all remotes.
But if you are creating a new branch and push to server it won't trigger the job. As Github plugin in Jenkins configuration is monitoring the changes in the repo and creating a branch is not a change. So the job won't trigger for that branch until you do a commit in that branch. And I feel this is the correct behavior.
For example when you do a commit xxxx on branch master, your job
gets triggered with commit id xxxx. Now you create a branch say
branch1 from master and you push to server. As you can see there is
not difference between commit xxxx and branch1 so there is no need
to build branch1 in Jenkins.

Jenkins not building new branch on first push

I am using the github plugin in Jenkins and my automatic builds are working for the most part. However, a build only occurs after the second push to a branch. When I create a new branch using git push origin branch_name:branch_name the jenkins build is not kicked off. I have to make another commit to the same branch for that to work. How can I fix this?
It turned out that my new branch was no different than my old branch so it wasn't viewed as a change. As in it had the same commit history with no additions.
I literally just did
git branch -b new_branch
git push origin new_branch:new_branch
The web hook log showed that jenkins recognized the new branch but said --> no changes.

Pulling latest changes from mercurial forked repository in a branch

Consider two repositories, production and stage. I have a branch in production repository called stage-branch. What I am trying to achieve is to merge latest changes from stage repository into that branch.
And everything went well, I cloned my production repository I pulled stage repository and merged under stage-branch.
What is unexpected though is that the default branch in my production repository now has been replaced by the default branch of the stage repository, which was not intended. I have just committed my merge changes under stage-branch in production repository but when I push I get a notification that there is a new head in my default branch.
How can I keep or revert my default branch to the state it was before pulling and merging?
EDIT: Production repository is a fork of stage repository, is it logical that the tip is getting automatically to latest revision of the pulled repository?
When your push or pull something in Mercurial, by default everything (and not just the current active branch, as it is in e.g. Git) will be pushed/pulled. If you would only like to push or pull a specific branch, you'll have to use the -b option.
From hg help pull you'll for instance see:
-b --branch BRANCH [+] a specific branch you would like to push
So if I understand your problem correct, it sounds like you doing a hg pull -b stage-branch should be the right thing to do.