|
| 1 | +#! /usr/bin/bash |
| 2 | +# PY_COLORS: '1' |
| 3 | +# ANSIBLE_FORCE_COLOR: '1' |
| 4 | + |
| 5 | +export PY_COLORS=1 |
| 6 | +export ANSIBLE_FORCE_COLOR=1 |
| 7 | + |
| 8 | +echo "AAP - Automation controller Github Action" |
| 9 | + |
| 10 | +if [ -z "$CONTROLLER_HOST" ]; then |
| 11 | + echo "Automation controller host is not set. Exiting." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +if [ -z "$CONTROLLER_USERNAME" ]; then |
| 16 | + echo "Automation controller username is not set. Exiting." |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +if [ -z "$CONTROLLER_PASSWORD" ]; then |
| 21 | + echo "Automation controller password is not set. Exiting." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +echo "CONTROLLER_HOST is $CONTROLLER_HOST" |
| 26 | +echo "JOB_TEMPLATE being executed $JOB_TEMPLATE" |
| 27 | +echo "EXTRA_VARS is set to $EXTRA_VARS" |
| 28 | +echo "CONTROLLER_VERIFY_SSL is set to $CONTROLLER_VERIFY_SSL" |
| 29 | +echo "CONTROLLER_PROJECT is set to $CONTROLLER_PROJECT" |
| 30 | +echo "----------------" |
| 31 | +echo "GITHUB_BASE_REF is $GITHUB_BASE_REF" |
| 32 | +echo "GITHUB_HEAD_REF is $GITHUB_HEAD_REF" |
| 33 | +echo "GITHUB_REF_NAME is $GITHUB_REF_NAME" |
| 34 | +echo "GITHUB_EVENT_NAME is $GITHUB_EVENT_NAME" |
| 35 | +echo "GITHUB_JOB is $GITHUB_JOB" |
| 36 | +echo "GITHUB_REF is $GITHUB_REF" |
| 37 | +echo "GITHUB_REPOSITORY is $GITHUB_REPOSITORY" |
| 38 | +echo "pull_request_event is $pull_request_event" |
| 39 | +echo "---------------" |
| 40 | + |
| 41 | +pull/1/head |
| 42 | + |
| 43 | +# scm_url e.g. https://github.com/ansible-cloud/aap_controller_action |
| 44 | +# scm_branch e.g. pull/1/head (for PR #1) |
| 45 | +# scm_refspec e.g. refs/pull/1/head:refs/remotes/origin/pull/1/head |
| 46 | + |
| 47 | +scm_url="https://github.com/$GITHUB_REPOSITORY" |
| 48 | +echo "scm_url is $scm_url" |
| 49 | + |
| 50 | +scm_branch="pull/$pull_request_event/head" |
| 51 | +echo "scm_branch is $scm_branch" |
| 52 | + |
| 53 | +scm_refspec="refs/pull/$pull_request_event/head:refs/remotes/origin/pull/$pull_request_event/head" |
| 54 | +echo "scm_refspec is $scm_refspec" |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +tee ansible.cfg << EOF |
| 59 | +[defaults] |
| 60 | +COLLECTIONS_PATHS = /root/.ansible/collections |
| 61 | +stdout_callback=community.general.yaml |
| 62 | +callbacks_enabled=ansible.posix.profile_tasks |
| 63 | +
|
| 64 | +EOF |
| 65 | + |
| 66 | + |
| 67 | +tee playbook.yml << EOF |
| 68 | +--- |
| 69 | +- name: execute autmation job |
| 70 | + hosts: localhost |
| 71 | + gather_facts: no |
| 72 | +
|
| 73 | + tasks: |
| 74 | +
|
| 75 | + - name: grab github action input |
| 76 | + set_fact: |
| 77 | + job_template_var: "$JOB_TEMPLATE" |
| 78 | + workflow_template_var: "$WORKFLOW_TEMPLATE" |
| 79 | + project_var: "$CONTROLLER_PROJECT" |
| 80 | + extra_vars: "$EXTRA_VARS" |
| 81 | + scm_url: "$scm_url" |
| 82 | + scm_branch: "$scm_branch" |
| 83 | + scm_refspec: "$scm_refspec" |
| 84 | +
|
| 85 | + - name: print out extra_vars |
| 86 | + debug: |
| 87 | + msg: |
| 88 | + - "extra vars are {{ extra_vars }}" |
| 89 | +
|
| 90 | + - name: project update and sync |
| 91 | + block: |
| 92 | + - name: retrieve info on existing specified project in Automation controller |
| 93 | + set_fact: |
| 94 | + project_info: "{{ query('awx.awx.controller_api', 'projects', verify_ssl=$CONTROLLER_VERIFY_SSL, query_params={ 'name': 'test project' }) }}" |
| 95 | +
|
| 96 | + - name: print out existing project settings to terminal |
| 97 | + debug: |
| 98 | + msg: |
| 99 | + - description: "{{ project_info[0].description }}" |
| 100 | + - organization: "{{ project_info[0].organization }}" |
| 101 | + - default_environment: "{{ project_info[0].default_environment }}" |
| 102 | + - scm_type: "{{ project_info[0].scm_type }}" |
| 103 | + - scm_url: "{{ project_info[0].scm_url }}" |
| 104 | + - scm_branch: "{{ project_info[0].scm_branch }}" |
| 105 | + - scm_refspec: "{{ project_info[0].scm_refspec }}" |
| 106 | + - credential: "{{ project_info[0].credential }}" |
| 107 | + - scm_clean: "{{ project_info[0].scm_clean }}" |
| 108 | + - scm_delete_on_update: "{{ project_info[0].scm_delete_on_update }}" |
| 109 | + - scm_track_submodules: "{{ project_info[0].scm_track_submodules }}" |
| 110 | + - scm_update_on_launch: "{{ project_info[0].scm_update_on_launch }}" |
| 111 | + - allow_override: "{{ project_info[0].allow_override }}" |
| 112 | +
|
| 113 | + - name: update project to point at pull request |
| 114 | + awx.awx.project: |
| 115 | + name: "{{ project_var }}" |
| 116 | + state: present |
| 117 | + description: "{{ project_info[0].description }}" |
| 118 | + organization: "{{ project_info[0].organization }}" |
| 119 | + default_environment: "{{ project_info[0].default_environment }}" |
| 120 | + scm_type: "{{ project_info[0].scm_type }}" |
| 121 | + scm_url: "$scm_url" |
| 122 | + scm_branch: "$scm_branch" |
| 123 | + scm_refspec: "$scm_refspec" |
| 124 | + # credential: "{{ project_info[0].credential }}" |
| 125 | + scm_clean: "{{ project_info[0].scm_clean }}" |
| 126 | + scm_delete_on_update: "{{ project_info[0].scm_delete_on_update }}" |
| 127 | + scm_track_submodules: "{{ project_info[0].scm_track_submodules }}" |
| 128 | + scm_update_on_launch: "{{ project_info[0].scm_update_on_launch }}" |
| 129 | + allow_override: "{{ project_info[0].allow_override }}" |
| 130 | +
|
| 131 | + - name: sync project update |
| 132 | + awx.awx.project_update: |
| 133 | + project: "{{ project_var }}" |
| 134 | + wait: true |
| 135 | + when: project_var|length > 0 |
| 136 | +
|
| 137 | + - name: Launch a job template with extra_vars on remote controller instance |
| 138 | + when: job_template_var|length > 0 |
| 139 | + awx.awx.job_launch: |
| 140 | + job_template: "{{ job_template_var }}" |
| 141 | + extra_vars: "$EXTRA_VARS" |
| 142 | + validate_certs: "$CONTROLLER_VERIFY_SSL" |
| 143 | + controller_username: "$CONTROLLER_USERNAME" |
| 144 | + controller_password: "$CONTROLLER_PASSWORD" |
| 145 | + controller_host: "$CONTROLLER_HOST" |
| 146 | + register: job_output |
| 147 | +
|
| 148 | + - name: Wait for job |
| 149 | + when: job_template_var|length > 0 or workflow_template_var|length > 0 |
| 150 | + awx.awx.job_wait: |
| 151 | + job_id: "{{ job_output.id }}" |
| 152 | + timeout: 3600 |
| 153 | + validate_certs: "$CONTROLLER_VERIFY_SSL" |
| 154 | + controller_username: "$CONTROLLER_USERNAME" |
| 155 | + controller_password: "$CONTROLLER_PASSWORD" |
| 156 | + controller_host: "$CONTROLLER_HOST" |
| 157 | +
|
| 158 | + - name: retrieve job info |
| 159 | + when: job_template_var|length > 0 or workflow_template_var|length > 0 |
| 160 | + uri: |
| 161 | + url: https://$CONTROLLER_HOST/api/v2/jobs/{{ job_output.id }}/stdout/?format=json |
| 162 | + method: GET |
| 163 | + user: "$CONTROLLER_USERNAME" |
| 164 | + password: "$CONTROLLER_PASSWORD" |
| 165 | + validate_certs: "$CONTROLLER_VERIFY_SSL" |
| 166 | + force_basic_auth: yes |
| 167 | + register: playbook_output |
| 168 | +
|
| 169 | + - name: display Automation controller job output |
| 170 | + when: job_template_var|length > 0 or workflow_template_var|length > 0 |
| 171 | + debug: |
| 172 | + var: playbook_output.json.content |
| 173 | +
|
| 174 | + - name: copy playbook output from Automation controller to file |
| 175 | + when: job_template_var|length > 0 or workflow_template_var|length > 0 |
| 176 | + ansible.builtin.copy: |
| 177 | + content: "{{ playbook_output.json.content }}" |
| 178 | + dest: job_output.txt |
| 179 | +
|
| 180 | + - name: revert project settings back to original |
| 181 | + awx.awx.project: |
| 182 | + name: "{{ project_var }}" |
| 183 | + state: present |
| 184 | + description: "{{ project_info[0].description }}" |
| 185 | + organization: "{{ project_info[0].organization }}" |
| 186 | + default_environment: "{{ project_info[0].default_environment }}" |
| 187 | + scm_type: "{{ project_info[0].scm_type }}" |
| 188 | + scm_url: "{{ project_info[0].scm_url }}" |
| 189 | + scm_branch: "{{ project_info[0].scm_branch }}" |
| 190 | + scm_refspec: "{{ project_info[0].scm_refspec }}" |
| 191 | + # credential: "{{ project_info[0].credential }}" |
| 192 | + scm_clean: "{{ project_info[0].scm_clean }}" |
| 193 | + scm_delete_on_update: "{{ project_info[0].scm_delete_on_update }}" |
| 194 | + scm_track_submodules: "{{ project_info[0].scm_track_submodules }}" |
| 195 | + scm_update_on_launch: "{{ project_info[0].scm_update_on_launch }}" |
| 196 | + allow_override: "{{ project_info[0].allow_override }}" |
| 197 | + when: project_var|length > 0 |
| 198 | +
|
| 199 | +EOF |
| 200 | + |
| 201 | +echo "AAP Github Action - Executing Automation Job on Automation controller" |
| 202 | + |
| 203 | +/usr/local/bin/ansible-playbook playbook.yml |
| 204 | + |
| 205 | +if [ $? -eq 0 ]; then |
| 206 | + echo "Ansible Github Action Job has executed successfully" |
| 207 | +else |
| 208 | + echo "Ansible Github Action Job has failed" |
| 209 | + exit 1 |
| 210 | +fi |
| 211 | + |
| 212 | +echo "END OF AAP - Automation controller Github Action" |
0 commit comments