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