Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Create Pipeline Role

The create_pipeline role creates a new Pipeline by cloning an existing one. The play creates a new pipeline, pulls down the base pipeline’s configs, modifies it, then applies it to the pipeline.

main.yml

---
# tasks file for create_pipeline

- name: Add vault to vars
 include_vars: ../vars/vault.yml
 tags:
   - create_pipeline

- import_tasks: create_pipeline.yml
 tags:
   - create_pipeline

...

vault.yml

You need to supply the credentials required to log into the Jenkins server.

svc_user: foo
svc_token: foo-pass.

create_pipeline.yml

---

- name: Creating Jenkins pipeline
  uri:
    url: "{{ jenkinsurl }}/view/all/createItem"
    method: POST
    timeout: 120
    status_code: [200, 302]
    user: "{{ svc_user }}"
    password: "{{ svc_token }}"
    force_basic_auth: True
    validate_certs: False
    # Cookbook pipelines are named the same as their bitbucket repos. 
    # Update this line with specific pipelines to model after.
    body: "name={{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}&mode=copy&from={{ (pipeline_type == 'cookbook') | ternary(base_pipeline_1, '') }}{{ (pipeline_type == 'role') | ternary(base_pipeline_2, '') }}"


- name: Download Jenkins pipeline config file
  uri:
    url: "{{ jenkinsurl }}/job/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}/config.xml"
    method: GET
    timeout: 120
    status_code: [200, 302]
    user: "{{ svc_user }}"
    password: "{{ svc_token }}"
    force_basic_auth: True
    validate_certs: False
    dest: "/tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml"

# Need to disable at the the config.xml layer because Jenkins won't enable the pipeline otherwise.
- name: Update the config file to disable the pipeline
  replace:
    path: "/tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml"
    after: '<disabled>'
    before: '</disabled>'
    regexp: '^(.+)$'
    replace: 'true'

- name: Update the config file to the correct repo_id
  replace:
    path: "/tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml"
    after: '<authToken>'
    before: '</authToken>'
    regexp: '^(.+)$'
    replace: "{{ repo_id }}"

- name: Reconfigure Jenkins pipeline
  uri:
    url: "{{ jenkinsurl }}/job/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}/config.xml"
    method: POST
    timeout: 120
    status_code: [200, 302]
    user: "{{ svc_user }}"
    password: "{{ svc_token }}"
    force_basic_auth: True
    validate_certs: False
    headers:
      Content-Type: application/xml
    src: "/tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml"

# This only works when the config.xml file was disabled originally.
- name: Enable the pipeline
  uri:
    url: "{{ jenkinsurl }}/job/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}/enable"
    method: POST
    timeout: 120
    status_code: [200, 302]
    user: "{{ svc_user }}"
    password: "{{ svc_token }}"
    force_basic_auth: True
    validate_certs: False

- name: "Cleanup /tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml file"
  file:
    path: "/tmp/{{ (pipeline_type == 'cookbook') | ternary(repo_name, '') }}{{ (pipeline_type == 'role') | ternary('role_' + repo_name, '') }}_config.xml"
    state: absent

...