Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins job builder gitlab trigger is ignored

I have a following Jenkins Job definition:

- scm:
  name: some-project
  scm:
    - git:
      url: git@gitlab.****/some-project.git
      credentials-id: some-ssh-username-with-private-key
      branches:
        - origin/master
- project:
    name: some-project
    jobs:
      - '{name}':
        triggers:
          gitlab:
            trigger-push: true
            trigger-merge-request: false
            trigger-open-merge-request-push: never

Now this job can be uploaded to Jenkins without error but if I go to Configure page of some-project in Jenkins Web UI, I can see that Build when a change is pushed to GitLab in Triggers section is not enabled. GitLab repo gets configured correctly - Source Code Management section of this job has git@gitlab.****/some-project.git with some-ssh-username-with-private-key credentials and branch origin/master configured. But without working triggers this is useless.

What am I doing wrong?

Please note that I'm not asking how to configure GitLab WebHooks to trigger Jenkins job. I can do this manually and it works fine. But we want to manage our Jenkins jobs with Jenkins Job builder to avoid error prone process of configuring them via Web UI and to keep track of changes in job configuration - we are creating git repository with Jenkins job definitions.

like image 908
ElmoVanKielmo Avatar asked Nov 09 '17 12:11

ElmoVanKielmo


3 Answers

I suspect you may need to put the triggers in the job itself, rather than under /project/jobs.

With the triggers under the project key, I experience the same symptom you do. None of my triggers get created in Jenkins. Moving the triggers section into a job fixes the problem for me.

This yaml will build a jenkins job called builder-test with the "Build when a change is pushed to GitLab" box checked:

- scm:
    name: gitlab
    scm:
      - git:
          url: https://gitlab.com/user/repo-name.git
          branches:
            - origin/master
- job:
    name: builder-test
    project-type: freestyle
    description: "builder-test desc"
    scm:
      - gitlab            
    triggers:
      - gitlab: 
          trigger-push: true
- project:
    name: builder-test-project
    jobs:
      - 'builder-test'

Tested on Jenkins 2.32.3 using GitLab plugin 1.5.1 and JJB 1.6.2

like image 178
Mike Patrick Avatar answered Oct 25 '22 08:10

Mike Patrick


In your question you have said that the Build when a change is pushed to GitLab option is "not enabled". On the configure page Jenkins will disable options in drop down lists when those options are already selected (i.e. being used) in the current job configuration.

The trigger-push: true option is probably what is driving this in your case.

Are you saying that your job is not actually being built when changes are pushed to the GitLab repository?

After your clarification:

There is a full example here for versions 1.1.26 and later.

triggers:
  - gitlab:
      trigger-push: false
      trigger-merge-request: false
      trigger-open-merge-request-push: both
      ci-skip: false
      set-build-description: false
      add-note-merge-request: false
      add-vote-merge-request: false
      add-ci-message: true
      allow-all-branches: true
      include-branches:
        - 'master'
        - 'master2'
        - 'local-test'
      exclude-branches:
        - 'broken-test'
        - 'master-foo'

Does your code incorporate the branches to include?

like image 37
Andrew Gray Avatar answered Oct 25 '22 09:10

Andrew Gray


Job configuration should be provided in the "job" or "job-template" sections. Triggers are a job configuration.

"project" configuration in JJB is for describing "which" job and/or job-templates to pull in and to set variables needed by those job configurations.

The problem in the question is that "triggers" which is a job configuration was placed inside a "project" section instead of a "job" and / or "job-template" section.

It may help to think of job and job-templates as "classes" in a programming language, and "project" sections as instantiating and passing parameters to the class constructor.

- job-template:
    name: '{project-name}-verify'

    ######################
    # Default parameters #
    ######################

    branches:
      - origin/master

    gitlab-trigger-push: true
    gitlab-trigger-merge-request: false
    gitlab-trigger-open-merge-request-push: never

    #####################
    # Job configuration #
    #####################

    project-type: freestyle

    scm:
      - git:
          url: '{git-url}'
          branches: '{obj:branches}'

    triggers:
      - gitlab: 
          trigger-push: '{gitlab-trigger-push}'
          trigger-merge-request: '{gitlab-trigger-merge-request}'
          trigger-open-merge-request-push: '{gitlab-trigger-open-merge-request-push}'

- project:
    name: abc123
    jobs:
      - '{project-name}-verify'
    project-name: 'my-project'

What's nice about this method is now all the options in curly braces can optionally override the defaults defined in the job-template.

like image 23
zxiiro Avatar answered Oct 25 '22 10:10

zxiiro