Trigger a new jenkins job through ansible

2,940 views
Skip to first unread message

Seeram Venkatesh

unread,
Aug 15, 2017, 11:50:00 AM8/15/17
to Ansible Project
Hi Techies, 

I'm looking for a solution where I want to trigger a Jenkins build through Ansible. 
I went through a module called jenkins_job from which I can connect to the jenkins server but not able to trigger a new build. 
Please help me here and TIA. 

J Hawkesworth

unread,
Aug 16, 2017, 11:12:59 AM8/16/17
to Ansible Project
Maybe use the uri module to call jenkins rest api - see https://wiki.jenkins.io/display/JENKINS/Remote+access+API

J Hawkesworth

unread,
Aug 29, 2017, 5:34:37 AM8/29/17
to Ansible Project
Turns out I needed this too.  In the end I wound up with the following role (lightly tested in ansible 2.3.0.0 so far).

$ tree -A jenkins-job/
jenkins-job/
├── defaults
│   └── main.yml
└── tasks
    └── main.yml

2 directories, 2 files
$ cat jenkins-job/defaults/main.yml
---
success_check_max_retries: 10
jenkins_protocol: http
poll_delay_seconds: 4

$ cat jenkins-job/tasks/main.yml
---
# Generic role for running a jenkins job and checking that it completes successfully.
# Note this uses json_query which means you need to install jmespath from pip

# role parameters
# mandatory parameters
#     jenkins_host
#     jenkins_job
#     jenkins_job_token
# Note: remember to set the job token up in the job configuration
# in Build Triggers ->
# Trigger builds remotely (e.g., from scripts)
# Without this you'll get 403 'No valid crumb was included in the request'.
# for a simple description of setting up jenkins jobs so they can be called remotely.
#
#
# optional parameters
#     jenkins_protocol (default value: http)
#     success_check_max_retries: (default value: 10)
#     jenkins_user (no default)
#     jenkins_cred (no default)

- name: ensure role parameters supplied
  fail:
    msg: "jenkins-job role expects {{item}} but it was not defined or empty"
  when:
      - item is undefined
      - item.length < 1
  with_items:
    - jenkins_host
    - jenkins_job
    - jenkins_job_token

- name: post to jenkins job queue to attempt to start a jenkins build job
  block:

    - name: call the jenkins rest api endpoint for starting the jenkins job
      uri:
        force_basic_auth: yes
        headers:
          Content-Type: "application/json"
        method: POST
        password: "{{ jenkins_cred }}"
        return_content: yes
        status_code: 201
        url: "{{jenkins_protocol}}://{{jenkins_host}}/job/{{ jenkins_job }}/build?TOKEN={{jenkins_job_token}}"
        user: "{{ jenkins_user }}"
      register: start_job_result

    # hopefully the job is queued at this point.
    # Now poll the job queue to see if it has finished.

    - name: debug jenkins request result
      debug:
        var: start_job_result
        verbosity: 1
  rescue:
    - name: Fail because we could not queue a new build job in Jenkins successfully
      fail:
        msg: "Error, could not queue a new build for jenkins job [ {{ jenkins_job }} ] on [ {{jenkins_protocol}}://{{jenkins_host}} ].  Is jenkins up and reachable?  Is the job name correct?  Are the credentials correct and current?"

# Now query the job queue and poll until it has started the build, or timed out
- name: query jenkins job queue to see if it has been able to start a build
  block:
    - name: poll jenkins JOB QUEUE every 3 seconds for configured number of attempts to get the number from the job queue
      uri:
        force_basic_auth: yes
        headers:
          Content-Type: "application/json"
        method: GET
        password: "{{ jenkins_cred }}"
        return_content: yes
        timeout: 3
        url: "{{start_job_result.location}}api/json"
        user: "{{ jenkins_user }}"
      register: poll_result
      until: poll_result|json_query('json.executable.number')|default(0)|int > 0
      retries: '{{ success_check_max_retries }}'
      delay: '{{ poll_delay_seconds }}'

  rescue:
    - name: Fail because we could not query the jenkins job queue successfully
      fail:
        msg: "Error, could not query the jenkins job queue at [ {{start_job_result.location}}/api/json ].  Is jenkins up?  Are the credentials correct and current?"

- name: check for jenkins job success
  block:
    - name: show the build job url detected above
      debug:
        msg: "Build job is at [ {{jenkins_protocol}}://{{jenkins_host}}/job/{{ jenkins_job }}/{{poll_result.json.executable.number}}/api/json ]"

    - name: now poll jenkins JOB every 3 seconds for configured number of attempts to check if the job was actually successful
      uri:
        force_basic_auth: yes
        headers:
          Content-Type: "application/json"
        method: GET
        password: "{{ jenkins_cred }}"
        return_content: yes
        timeout: 3
        url: "{{jenkins_protocol}}://{{jenkins_host}}/job/{{ jenkins_job }}/{{poll_result.json.executable.number}}/api/json"
        user: "{{ jenkins_user }}"
      register: build_job_result
      until: build_job_result|json_query('json.result') == 'SUCCESS'
      retries: '{{ success_check_max_retries }}'
      delay: '{{ poll_delay_seconds }}'

    - name: show the build job url detected above
      debug:
        msg: "Build job [ {{jenkins_protocol}}://{{jenkins_host}}/job/{{ jenkins_job }}/{{poll_result.json.executable.number}}/api/json ] reported SUCCESS"

  rescue:

    - name:
      fail:
        msg: "Build job [ {{jenkins_protocol}}://{{jenkins_host}}/job/{{ jenkins_job }}/{{poll_result.json.executable.number}}/api/json ] did not report SUCCESS within the timeout period.  This can happen if the job is taking longer than the polling timeout (approx {{ poll_delay_seconds * success_check_max_retries }} seconds), or the build is stuck for some reason, such as no available build executors."

# End of file jenkins-job/tasks/main.yml

Seeram Venkatesh

unread,
Aug 29, 2017, 9:21:15 AM8/29/17
to ansible...@googlegroups.com
Thank you very much. It really helps me a lot.

Regards,
Venkatesh 

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/WoDO9M0hzss/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/26b28467-81c5-4bbc-b17e-e9536f0de7b6%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chichu sam

unread,
Jul 15, 2018, 8:13:06 AM7/15/18
to Ansible Project

Hi Venkat,

CAn you please share the complete file .

I want to use to do full build and deployemnt

Ansible should call jenkins to do build and deployment

PLease  help
thanks
sam
Reply all
Reply to author
Forward
0 new messages