参考链接:
https://github.com/jenkinsci/gitlab-plugin#jenkins-job-configuration
https://docs.gitlab.com/ee/integration/jenkins.html
https://github.com/jenkinsci/gitlab-plugin#jenkins-job-configuration
https://docs.gitlab.com/ee/integration/jenkins.html
GitLab-to-Jenkins authentication
By default the plugin will require authentication to be set up for the connection from GitLab to Jenkins, in order to prevent unauthorized persons from being able to trigger jobs.
Configuring global authentication
- Create a user in Jenkins which has, at a minimum, Job/Build permissions
- Log in as that user (this is required even if you are a Jenkins admin user), then click on the user's name in the top right corner of the page
- Click 'Configure,' then 'Show API Token...', and note/copy the User ID and API Token
- In GitLab, when you create webhooks to trigger Jenkins jobs, use this format for the URL and do not enter anything for 'Secret Token':
http://USERID:APITOKEN@JENKINS_URL/project/YOUR_JOB - After you add the webhook, click the 'Test' button, and it should succeed
Job trigger configuration
Webhook URL
When you configure the plugin to trigger your Jenkins job, by following the instructions below depending on job type, it will listen on a dedicated URL for JSON POSTs from GitLab's webhooks. That URL always takes the form
http://JENKINS_URL/project/PROJECT_NAME, or http://JENKINS_URL/project/FOLDER/PROJECT_NAME if the project is inside a folder in Jenkins. You should not be using http://JENKINS_URL/job/PROJECT_NAME/build or http://JENKINS_URL/job/gitlab-plugin/buildWithParameters, as this will bypass the plugin completely.Freestyle and Pipeline jobs
- In the Build Triggers section:
- Select Build when a change is pushed to GitLab
- Copy the GitLab webhook URL shown in the UI (see here for guidance)
- Use the check boxes to trigger builds on Push Events and/or Created Merge Request Events and/or Accepted Merge Request Events and/or Closed Merge Request Events
- Optionally use Rebuild open Merge Requests to enable re-building open merge requests after a push to the source branch
- If you selected Rebuild open Merge Requests other than None, check Comments, and specify the Comment for triggering a build. A new build will be triggered when this phrase appears in a commit comment. In addition to a literal phrase, you can also specify a Java regular expression
- You can use Build on successful pipeline events to trigger on a successful pipeline run in Gitlab. Note that this build trigger will only trigger a build if the commit is not already built and does not set the Gitlab status. Otherwise you might end up in a loop
- Configure any other pre build, build or post build actions as necessary
- Click Save to preserve your changes in Jenkins
- Create a webhook in the relevant GitLab projects (consult the GitLab documentation for instructions on this), and use the URL you copied from the Jenkins job configuration UI. It should look something like
http://JENKINS_URL/project/yourbuildname
Configure a GitLab project
Create a new GitLab project or choose an existing one. Then, go to Integrations -> Jenkins CI.
Check the ‘Active’ box. Select whether you want GitLab to trigger a build on push, Merge Request creation, tag push, or any combination of these. We recommend unchecking ‘Merge Request events’ unless you have a specific use-case that requires re-building a commit when a merge request is created. With ‘Push events’ selected, GitLab will build the latest commit on each push and the build status will be displayed in the merge request.
Enter the Jenkins URL and Project name. The project name should be URL-friendly where spaces are replaced with underscores. To be safe, copy the project name from the URL bar of your browser while viewing the Jenkins project.
Optionally, enter a username and password if your Jenkins server requires authentication.
评论
发表评论