How can I make Jenkins CI with git trigger on pushes to master?

匿名 (未验证) 提交于 2019-12-03 02:02:01

问题:

I'm trying to set up Jenkins-ci for a project using github. I've already set up jenkins with the appropriate plugins. I want jenkins to run build scripts only whenever someone on the project pushes to master. So far I've been able to set it up so that a build will be triggered anytime anyone pushes to anywhere, but that is too broad. I've done this with post-receive service hooks on git.

I've read the jenkins wiki, and a couple of tutorials, but this particular detail is missing... is it something to do with polling maybe? Or should work be done on the git side, so that git only triggers jenkins when Master is changed?

Thanks for your help!

回答1:

As already noted by gezzed in his comment, meanwhile there is a good solution (described by http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/):

  • Set the Jekins job's Build trigger to Poll SCM, but do not specify a schedule

  • Create a github post-receive trigger to notify the URL

    http://yourserver/jenkins/git/notifyCommit?url=?token=
  • This will trigger all builds that poll the specified git repo.

  • However, polling actually checks whether anything has been pushed to the used branch.

It works perfectly.



回答2:

As of version 0.5, the Github plugin for Jenkins can trigger a build when a change is pushed to GitHub



回答3:

Instead of triggering builds remotely, change your Jenkins project config to trigger builds by polling. Jenkins can poll based on a fixed internal, or by a URL. The latter is what you want to skip builds if there are not changes for that branch. The exact details are in the documentation. Essentially you just need to check the "Poll SCM" option, leave the schedule section blank, and set a remote URL to hit JENKINS_URL/job/name/polling.

One gotcha if you have a secured Jenkins environment is unlike /build, the /polling URL requires authentication. The instructions here have details. For example, I have a Github Post-Receive hook going to username:apiToken@JENKIS_URL/job/name/polling.



回答4:

For Gitlab, use these steps:

  1. Go to your project's settings > Web hooks
  2. Enter the "Build Now" URL from your Jenkins project as a Push Event URL

    http://server.com/jenkins/job/project_name/build?delay=0sec for example

  3. Click Add Web Hook and then test hook

Then any time you commit to the repo, the web hook is triggered and a build is created. Be sure to set your Jenkins workspace to delete workspace before each build so you get a fresh copy of the new code.



回答5:

Not related to Git but below I will help with the Jenkins job configuration in detail with Mercurial. May help others with similar problem.

  1. Install URL Trigger Plugin
  2. Go to job configuration page and select Poll SCM option. Set the value to * * * * *
  3. Check the option: [URLTrigger] - Poll with a URL.
    Now you can select some options like modification date change, URL content etc.
  4. Monitor change of content
  5. Save the changes.

Now, trigger some change to the mercurial repository by some test check-ins

See that the Jenkins job now runs by detecting the SCM changes. When the build is run due to mercurial changes, then, you will see text Started by an SCM change. Else, the user who manually started it.



回答6:

You need to specify the branch. By default it listens to anything. See this post here:

http://evgenyg.wordpress.com/2010/03/27/hudson-git-maven/



回答7:

Hope this helps: http://nrecursions.blogspot.in/2014/02/how-to-trigger-jenkins-build-on-git.html

It's just a matter of using curl to trigger a Jenkins job using the git hooks provided by git.
The command curl http://localhost:8080/job/someJob/build?delay=0sec can run a Jenkins job, where someJob is the name of the Jenkins job.

Search for the "hooks" folder in your hidden .git folder. Rename the "post-commit.sample" file to "post-commit". Open it with Notepad, remove the ": Nothing" line and paste the above command into it.

That's it. Whenever you do a commit, Git will trigger the post-commit commands defined in the file.



回答8:

Another possible solution to the problem is to use a recipe similar to the answer of question "How to trigger a build only if changes happen on particular set of files".



回答9:

Use the pull request builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin

It's really straightforward. You can then setup GitHub webhooks to trigger builds.



回答10:

Continues Integration with Jenkins After the code is committed from Git command/ GUI from local system to repository.

1) Create a job in jenkins with only job name and select type of the project freestyle click ok. Next page don't added any thing just click Save.

2) Go to the your local git repository where you have the source code and navigate to .git/hooks folder

3) hooks folder contains the few files check for the "post-commit" if not present create a file "post-commit" with out extension

C:\work\test\.git\hooks\post-commit

4) Edit the "post-commit" with below command make sure it is present in your local source code hooks folder

curl -u : -X POST http://localhost:8080/jenkins/job//build?token= >

Example: curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384

5)
: jenkins user name
: job name of the build
: go to the top right corner click on the username it will open the user page on the left hand side you can find the "Configure" menu. click the configure menu , in this page you can see the API token heading click on the Show API token

"userName" and "API Token" will be present , if the values are not present enter some random value and use in the url.

6) Make changes in your source code and commit the code to repository. 7) you can the job "http://localhost:8080/jenkins/job/Gitcommittest/" dash borad job is building.

It worked for me.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!