Skip to content

Commit e6750b6

Browse files
committed
testing workflow mode
1 parent 95abd0e commit e6750b6

File tree

4 files changed

+98
-43
lines changed

4 files changed

+98
-43
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
steps:
2424
- name: Load the ansible-cloud action
2525
id: controller_job
26-
uses: ansible-cloud/[email protected].6
26+
uses: ansible-cloud/[email protected].7
2727
with:
2828
controller_host: ${{ secrets.CONTROLLER_HOST }}
2929
controller_username: ${{ secrets.CONTROLLER_USERNAME }}

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ For the Github Pull Request method to work, you need to make sure your specified
7272
* **Source Control Branch** (which requires the **PROJECT** to have Allow Branch Override checked)
7373
* **Variables**
7474

75+
### Example of working with a Pull Request Methodology WORKFLOW
76+
77+
This is also known as Roger Lopez mode. In this example we will only initiate the Github Action if there is a Pull Request on our Github repository. We need to have a `workflow_template` and `controller_project` specified. This will make sure that any in-bound pull requests (PRs) are tested on a workflow rather than your existing project. This will allow people contributing to your Github repo to test their changes before you merge into your downstream repoistory.
78+
79+
Example of `.github/workflows/main.yml`
80+
81+
```
82+
on:
83+
pull_request_target:
84+
85+
jobs:
86+
automation_controller_job:
87+
runs-on: ubuntu-latest
88+
name: Kick off Automation controller job
89+
steps:
90+
- name: Load the ansible-cloud action
91+
id: controller_job
92+
uses: ansible-cloud/[email protected]
93+
with:
94+
controller_host: ${{ secrets.CONTROLLER_HOST }}
95+
controller_username: ${{ secrets.CONTROLLER_USERNAME }}
96+
controller_password: ${{ secrets.CONTROLLER_PASSWORD }}
97+
workflow_template: "test workflow"
98+
controller_project: "test project"
99+
extra_vars: "your_region=us-west-1"
100+
validate_certs: false
101+
env:
102+
pull_request_event: ${{ github.event.pull_request.number }}
103+
```
104+
105+
75106
## Setting up your repo to work with this action
76107

77108
You need to setup 3 (three) secrets:

action.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ inputs:
2121
required: false
2222
default: "true"
2323
job_template:
24-
description: "Name or ID of project, job_template or workflow_job_template."
25-
required: true
24+
description: "Name or ID of job_template"
25+
required: false
26+
workflow_template:
27+
description: "Name or ID of workflow_template"
28+
required: false
2629
extra_vars:
2730
description: "Pass extra command line variables to the playbook."
2831
required: false
@@ -36,4 +39,5 @@ runs:
3639
CONTROLLER_VERIFY_SSL: ${{ inputs.validate_certs }}
3740
CONTROLLER_PROJECT: ${{ inputs.controller_project }}
3841
JOB_TEMPLATE: ${{ inputs.job_template }}
42+
WORKFLOW_TEMPLATE: ${{ inputs.job_template }}
3943
EXTRA_VARS: ${{ inputs.extra_vars }}

entrypoint.sh

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ echo "GITHUB_REF is $GITHUB_REF"
3737
echo "GITHUB_REPOSITORY is $GITHUB_REPOSITORY"
3838
echo "pull_request_event is $pull_request_event"
3939
echo "---------------"
40-
41-
pull/1/head
40+
echo "WORKFLOW_TEMPLATE is $WORKFLOW_TEMPLATE"
41+
echo "---------------"
4242

4343
# scm_url e.g. https://github.com/ansible-cloud/aap_controller_action
4444
# scm_branch e.g. pull/1/head (for PR #1)
@@ -66,7 +66,7 @@ EOF
6666

6767
tee playbook.yml << EOF
6868
---
69-
- name: execute autmation job
69+
- name: execute automation job
7070
hosts: localhost
7171
gather_facts: no
7272
@@ -85,6 +85,7 @@ tee playbook.yml << EOF
8585
- "extra vars are {{ extra_vars }}"
8686
8787
- name: project update and sync
88+
when: workflow_template_var|length == 0
8889
block:
8990
- name: retrieve info on existing specified project in Automation controller
9091
set_fact:
@@ -225,43 +226,62 @@ tee playbook.yml << EOF
225226
226227
when: project_var|length > 0
227228
228-
- name: Launch a job template with extra_vars on remote controller instance when project is set
229-
when: job_template_var|length > 0
230-
awx.awx.job_launch:
231-
job_template: "{{ job_template_var }}"
232-
extra_vars: "$EXTRA_VARS"
233-
validate_certs: "$CONTROLLER_VERIFY_SSL"
234-
scm_branch: "{{ scm_branch | default(omit, true) }}"
235-
register: job_output
236-
237-
- name: Wait for job
238-
when: job_template_var|length > 0 or workflow_template_var|length > 0
239-
awx.awx.job_wait:
240-
job_id: "{{ job_output.id }}"
241-
timeout: 3600
242-
validate_certs: "$CONTROLLER_VERIFY_SSL"
243-
244-
- name: retrieve job info
245-
when: job_template_var|length > 0 or workflow_template_var|length > 0
246-
uri:
247-
url: https://$CONTROLLER_HOST/api/v2/jobs/{{ job_output.id }}/stdout/?format=json
248-
method: GET
249-
user: "$CONTROLLER_USERNAME"
250-
password: "$CONTROLLER_PASSWORD"
251-
validate_certs: "$CONTROLLER_VERIFY_SSL"
252-
force_basic_auth: yes
253-
register: playbook_output
254-
255-
- name: display Automation controller job output
256-
when: job_template_var|length > 0 or workflow_template_var|length > 0
257-
debug:
258-
var: playbook_output.json.content
259-
260-
- name: copy playbook output from Automation controller to file
261-
when: job_template_var|length > 0 or workflow_template_var|length > 0
262-
ansible.builtin.copy:
263-
content: "{{ playbook_output.json.content }}"
264-
dest: job_output.txt
229+
- name: launch a job and wait for the job
230+
when: job_template_var | length > 0
231+
block:
232+
- name: Launch a job template with extra_vars on remote controller instance when project is set
233+
awx.awx.job_launch:
234+
job_template: "{{ job_template_var }}"
235+
extra_vars: "{{ extra_vars | default(omit, true) }}"
236+
validate_certs: "$CONTROLLER_VERIFY_SSL"
237+
scm_branch: "{{ scm_branch | default(omit, true) }}"
238+
register: job_output
239+
240+
- name: Wait for job
241+
awx.awx.job_wait:
242+
job_id: "{{ job_output.id }}"
243+
timeout: 3600
244+
validate_certs: "$CONTROLLER_VERIFY_SSL"
245+
246+
- name: retrieve job info
247+
when: job_template_var|length > 0
248+
uri:
249+
url: https://$CONTROLLER_HOST/api/v2/jobs/{{ job_output.id }}/stdout/?format=json
250+
method: GET
251+
user: "$CONTROLLER_USERNAME"
252+
password: "$CONTROLLER_PASSWORD"
253+
validate_certs: "$CONTROLLER_VERIFY_SSL"
254+
force_basic_auth: yes
255+
register: playbook_output
256+
257+
- name: display Automation controller job output
258+
when: job_template_var|length > 0 or workflow_template_var|length > 0
259+
debug:
260+
var: playbook_output.json.content
261+
262+
- name: copy playbook output from Automation controller to file
263+
when: job_template_var|length > 0 or workflow_template_var|length > 0
264+
ansible.builtin.copy:
265+
content: "{{ playbook_output.json.content }}"
266+
dest: job_output.txt
267+
268+
- name: launch a workflow and wait for the workflow to finish
269+
when: workflow_template_var | length > 0
270+
block:
271+
- name: Launch a job template with extra_vars on remote controller instance when project is set
272+
awx.awx.workflow_launch:
273+
workflow_template: "{{ job_template_var }}"
274+
extra_vars: "{{ extra_vars | default(omit, true) }}"
275+
validate_certs: "$CONTROLLER_VERIFY_SSL"
276+
scm_branch: "{{ scm_branch | default(omit, true) }}"
277+
register: job_output
278+
279+
- name: Wait for workflow
280+
awx.awx.job_wait:
281+
job_id: "{{ job_output.id }}"
282+
job_type: workflow_jobs
283+
timeout: 3600
284+
validate_certs: "$CONTROLLER_VERIFY_SSL"
265285
266286
EOF
267287

0 commit comments

Comments
 (0)