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."