|
| 1 | +--- |
| 2 | +# @TODO - for AWS ECR we'll need certain policies attaching to the deploy IAM user |
| 3 | +- name: Create Dockerfile from template. |
| 4 | + local_action: |
| 5 | + module: ansible.builtin.template |
| 6 | + src: Dockerfile.j2 |
| 7 | + dest: "{{ deploy_container.docker_build_dir }}/Dockerfile" |
| 8 | + |
| 9 | +- name: Set Docker registry username and password. |
| 10 | + ansible.builtin.set_fact: |
| 11 | + _docker_registry_username: "{{ deploy_container.docker_registry_user }}" |
| 12 | + _docker_registry_password: "{{ deploy_container.docker_registry_pass }}" |
| 13 | + |
| 14 | +# Token valid for 12 hours |
| 15 | +- name: Fetch AWS ECR registry login token. |
| 16 | + ansible.builtin.command: |
| 17 | + command: "aws ecr get-login-password --region {{ deploy_container.aws_ecr.region }} --profile {{ deploy_container.aws_ecr.profile }}" |
| 18 | + when: deploy_container.aws_ecr.enabled |
| 19 | + register: _docker_registry_ecr_token |
| 20 | + |
| 21 | +- name: Set AWS ECR registry password. |
| 22 | + ansible.builtin.set_fact: |
| 23 | + _docker_registry_password: "{{ _docker_registry_ecr_token.stdout }}" |
| 24 | + when: deploy_container.aws_ecr.enabled |
| 25 | + |
| 26 | +- name: Set AWS ECR registry username. |
| 27 | + ansible.builtin.set_fact: |
| 28 | + _docker_registry_username: "AWS" |
| 29 | + when: deploy_container.aws_ecr.enabled |
| 30 | + |
| 31 | +- name: Log into Docker registry. |
| 32 | + community.docker.docker_login: |
| 33 | + registry_url: "{{ deploy_container.docker_registry_url }}" |
| 34 | + username: "{{ _docker_registry_username }}" |
| 35 | + password: "{{ _docker_registry_password }}" |
| 36 | + reauthorize: true |
| 37 | + delegate_to: localhost |
| 38 | + when: deploy_container.docker_registry_login |
| 39 | + |
| 40 | +- name: Build and push container image. |
| 41 | + community.docker.docker_image: |
| 42 | + build: |
| 43 | + path: "{{ deploy_container.docker_build_dir }}" |
| 44 | + repository: "{{ deploy_container.docker_registry_url }}" |
| 45 | + name: "{{ deploy_container.container_name }}" |
| 46 | + tag: "{{ deploy_container.container_tag | default('latest') }}" |
| 47 | + push: true |
| 48 | + source: build |
| 49 | + delegate_to: localhost |
0 commit comments