Wednesday, July 20, 2022

Jenkins/ Git/ CVS : Syncing / Pushing a branch from one repo to another repo

 If you want to sync or push a branch from one repo to another on certain git events like merge or push, Jenkin provides a "Git Publisher" step which you can utilize to sync or push your branch code to another repo. Steps are given below:


1. Create a new job in Jenkins

2. Configure "Source Code Management". Configure 2 repos : one for source repo and other for target repo. In my case- Source repo name is - "adobe-git" and Target repo name is - "ams-git"

For each repo define 3 things:

a. Repository URL- Depending on credentials type, you have two options: either enter ssh URL or https URL. For simplicity - I recommend using https URL format of your repo like this- https://myrepo.github.com/myorg/myreponame/

b. Credentials: Select the Add option provided to save repo credentials so that Jenkin can authenticate the repo to perform sync operation. Here you will see multiple option. For simplicity - I recommend using "Username with Password" option. If 2 factor auth is enabled for your git account- Create a Personal Access Token in git account and use that as a password. In password field use your Personal Access Token.

c. Git your each repo a custom name e.g. adobe-git. You can use this name in rest of configuration to point your specific repo.





3. Optionally, you can also setup branches to build if you want to push happen only when source branch build is successful to avoid any breaking build to have been pushed to target repo. In this option set the value in this format: {source repo name}/{source branch name}


4.  Configure "Build Triggers". If you want Jenkin to automatically poll to source repo and push any new commits to target repo, you can configure "Poll SCM" with some schedular cron e.g. H/10 * * * *

This cron expression means job will automatically run every 10 mins. If there is any change (if new PRs were merged in the source branch) in source repo, it will push to target repo.



5. Configure "Build Environment". Enable "Delete workspace before build starts" option.

6. Optionally, Configure "Build". 



7. Configure "Post Steps". Enable "Run only if build succeeds".

8. Configure "Post-build Actions". Add a "Git Publisher" step and configure it to point to target repo and target branch.

In case the target branch does not exist configure the "Branch to push" in this format- "refs/heads/develop".

That's all. Save the Job and test it.


No comments:

Post a Comment

CDN | Clearing Cloudflare cache

In order to clear Cloudflare cache automatically via code, follow below steps: 1. Develop Custom TransportHandler Develop a custom Trans...