At work I recently helped a team to transition from TFS to using Git as their source control management. After introducing the members to Git, we also established a common workflow on how we wanted to have Git integrate with Jenkins and GitLab. Below is our current implementation. You might be interested in my latest "Implementing the git flow" article as well. Git branching strategiesGit branching strategies are guidelines on how to use git's branching mechanism for your development. Establishing such a shared convention is especially useful for having a common vision in the team, in order to have everyone steer in the right direction. There are a couple of git branching strategies around which I'd not like to go into the very details right now. The interested among you might want to consider some of these links Making git-flow work on Jenkins and GitLabAt work, one of our teams recently switched from TFS to Git where we decided to adopt a "git-flow" similar style of working, having
These branches get published onto our internal Git server powered by GitLab. What we obviously wanted as well is to integrate this strategy with our Jenkins build server, having
Based on the above assumptions we currently create one Jenkins job per environment, one for master, one for the user story branches and one for production (although I have the feeling Jenkin's build parameterization could help out here to avoid redundancy between the build configurations...but didn't look into that, yet). Let's take a look at the 1. Source Code Management - Git ConfigurationBefore starting, make sure you have the Jenkins Git plugin. Then create a new Jenkins job, and in the "Source Code Management" section configure your Git repository like in the img example below. In the Branch Specifier field, enter your desired branch, depending on which one you're going to build in this current job. For master simply enter 2. GitLab WebhookThe next step is to add a web hook to GitLab. This is needed s.t. GitLab is able to signal to Jenkins about new commits that have been "pushed" to the repository. This is an alternative, but much more efficient way of instead doing a continuous polling. Just go to your repository settings and then to Web Hooks. Enter an url of the form 3. Activate pollingThe final step is to setup polling. H?? Sorry, didn't you just mention previously that we don't need polling 'cause GitLab calls Jenkins in case of new commits?? Yep, that's right, but that's part of a "security" measure the guys creating the Git plugin introduced to make sure you (that you control the Jenkins job) and you (that you added the Web Hook) both consent to execute the build based on new commits.
However, in the polling configuration you don't have to specify any kind of interval, meaning that it won't start by its own. You simply tick the checkbox "Poll SCM" to give your consent (somehow). That's it, now your Jenkins jobs should start based on the branch you configured in your job and based on the branch that gets pushed to GitLab, just as we wanted. Alternative approachesIn case you just need to trigger the build of a branch from GitLab, you can also simply configure Jenkin's remote trigger and add that as a Web Hook to your GitLab repo The downside of this approach is that you're not able to selectively launch builds based on commits on certain branches. Finally, I also found a plugin called GitLab Hook which I didn't try yet as the above described approach was much simpler (and didn't require the installation of a plugin). On their page they write that
I did not experience any of such delays so far, but we'll see. Automatically build all feature branchesNot only with Git flow, but in general when you work with feature branches and Merge/Pull requests it makes a lot of sense to automatically build those on your build server and execute the tests in order to make sure those branches would pass the build pipeline. There are different strategies for doing so. One possibility is to establish a naming convention for feature branches like a common prefix, i.e. The other strategy is to simply build all branches other than master (probably a more naive approach).
This article has been re-published on the following partner sites: |
|
來自: bananarlily > 《IT》