Skip to content

Commit

Permalink
Refactor image_build and image_push roles
Browse files Browse the repository at this point in the history
Primary changes are:

- Generalized variable names (remove "docker")
- Add explicit "push" variable rather than checking if the "registry" variable is defined.
- Allow for passing in version as build arg
  • Loading branch information
shanemcd committed Oct 12, 2021
1 parent 84ffa4a commit c9b53cf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 45 deletions.
1 change: 1 addition & 0 deletions .dockerignore
@@ -1,2 +1,3 @@
awx/ui/node_modules
Dockerfile
.git
7 changes: 5 additions & 2 deletions docs/build_awx_image.md
Expand Up @@ -5,7 +5,10 @@
To build a custom awx image to use with the awx-operator, use the `build_image` role:

```
$ ansible-playbook tools/ansible/build.yml -v -e awx_image=registry.example.com/awx -e awx_version=test
$ ansible-playbook tools/ansible/build.yml \
-e registry=registry.example.com \
-e awx_image=ansible/awx \
-e awx_version=test -v
```

> Note: The development image (`make docker-compose-build`) will not work with the awx-operator, the UI is not built in that image, among other things (see Dockerfile.j2 for more info).
Expand All @@ -21,7 +24,7 @@ $ docker push registry.example.com/awx:test
## Using this image with the awx-operator

In the spec section of the `my-awx.yml` file described in the [install docs](./../INSTALL.md#deploy-awx),
specify the new custom image.
specify the new custom image.

```
spec:
Expand Down
25 changes: 21 additions & 4 deletions tools/ansible/build.yml
Expand Up @@ -2,7 +2,24 @@
- name: Build AWX Docker Images
hosts: localhost
gather_facts: true
roles:
- {role: dockerfile}
- {role: image_build}
- {role: image_push, when: "docker_registry is defined"}
tasks:
- name: Get version from SCM if not explicitly provided
shell: |
python setup.py --version | cut -d + -f -1
args:
chdir: '../../'
register: setup_py_version
when: awx_version is not defined

- name: Set awx_version
set_fact:
awx_version: "{{ setup_py_version.stdout }}"
when: awx_version is not defined

- include_role:
name: dockerfile
- include_role:
name: image_build
- include_role:
name: image_push
when: push | default(false) | bool
3 changes: 3 additions & 0 deletions tools/ansible/roles/dockerfile/templates/Dockerfile.j2
Expand Up @@ -54,6 +54,9 @@ ADD requirements/requirements.txt \

RUN cd /tmp && make requirements_awx

ARG VERSION
ARG SETUPTOOLS_SCM_PRETEND_VERSION

{% if (build_dev|bool) or (kube_dev|bool) %}
ADD requirements/requirements_dev.txt /tmp/requirements
RUN cd /tmp && make requirements_awx_dev
Expand Down
5 changes: 4 additions & 1 deletion tools/ansible/roles/image_build/defaults/main.yml
@@ -1,2 +1,5 @@
---
awx_image: quay.io/ansible/awx
awx_image: ansible/awx
awx_image_tag: "{{ awx_version }}"
dockerfile_name: 'Dockerfile'

25 changes: 14 additions & 11 deletions tools/ansible/roles/image_build/tasks/main.yml
@@ -1,9 +1,4 @@
---
- name: Set global version if not provided
set_fact:
awx_version: "{{ lookup('file', playbook_dir + '/../../VERSION') }}"
when: awx_version is not defined

- name: Verify awx-logos directory exists for official install
stat:
path: "../../../awx-logos"
Expand All @@ -17,11 +12,19 @@
dest: "../../awx/ui/public/static/media/"
when: awx_official|default(false)|bool

- set_fact:
command_to_run: |
docker build -t {{ awx_image }}:{{ awx_image_tag }} \
-f {{ dockerfile_name }} \
--build-arg VERSION={{ awx_version }} \
--build-arg SETUPTOOLS_SCM_PRETEND_VERSION={{ awx_version }} \
--build-arg HEADLESS={{ headless }} \
.
# Calling Docker directly because docker-py doesnt support BuildKit
- name: Build AWX image
command: docker build -t {{ awx_image }}:{{ awx_version }} -f ../../{{ dockerfile_name }} ../..

- name: Tag awx images as latest
command: "docker tag {{ item }}:{{ awx_version }} {{ item }}:latest"
with_items:
- "{{ awx_image }}"
shell: "{{ command_to_run }}"
environment:
DOCKER_BUILDKIT: 1
args:
chdir: "{{ playbook_dir }}/../../"
4 changes: 4 additions & 0 deletions tools/ansible/roles/image_push/defaults/main.yml
@@ -0,0 +1,4 @@
---
registry: quay.io
awx_image: ansible/awx
awx_image_tag: "{{ awx_version }}"
43 changes: 16 additions & 27 deletions tools/ansible/roles/image_push/tasks/main.yml
@@ -1,33 +1,22 @@
---
- name: Authenticate with Docker registry if registry password given
docker_login:
registry: "{{ docker_registry }}"
username: "{{ docker_registry_username }}"
password: "{{ docker_registry_password }}"
registry: "{{ registry }}"
username: "{{ registry_username }}"
password: "{{ registry_password }}"
reauthorize: true
when: docker_registry is defined and docker_registry_password is defined

- name: Remove local images to ensure proper push behavior
block:
- name: Remove awx image
docker_image:
name: "{{ docker_registry }}/{{ docker_registry_repository }}/{{ awx_image }}"
tag: "{{ awx_version }}"
state: absent
when:
- registry is defined
- registry_username is defined
- registry_password is defined

- name: Tag and Push Container Images
block:
- name: Tag and push awx image to registry
docker_image:
name: "{{ awx_image }}"
repository: "{{ docker_registry }}/{{ docker_registry_repository }}/{{ awx_image }}"
tag: "{{ item }}"
push: true
with_items:
- "latest"
- "{{ awx_version }}"

- name: Set full image path for Registry
set_fact:
awx_docker_actual_image: >-
{{ docker_registry }}/{{ docker_registry_repository }}/{{ awx_image }}:{{ awx_version }}
docker_image:
name: "{{ awx_image }}:{{ awx_version }}"
repository: "{{ registry }}/{{ awx_image }}:{{ item }}"
force_tag: yes
push: true
source: local
with_items:
- "latest"
- "{{ awx_version }}"

0 comments on commit c9b53cf

Please sign in to comment.