Skip to content

Commit

Permalink
Aws ecr (#61)
Browse files Browse the repository at this point in the history
* Added ability to use ECR as the docker repository
  • Loading branch information
jewnix authored Nov 28, 2023
1 parent 85f3aa2 commit 28064e2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ If all goes well, you should have six new docker images:
* Changes made to the [inventory hosts](examples/organized-environment/inventory.yml) list for:
* docker_nodes

#### Pushing to an ECR repository

To use a ECR repository on AWS, set `is_ecr_registry: true` this will call the `aws_tools` role and install the aws cli on the build host, and grab the password for `docker_login` to use.

The following variables need to be set to use a ECR repository:
* ecr_access_key_id
* ecr_secret_access_key
* ecr_aws_region
* aws_ecr_username (defaults to `AWS`)

#### The push playbook

From the directory containing this set of playbooks, run:
Expand Down
2 changes: 1 addition & 1 deletion group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ host_volumes: []
volumes: "{{ role_volumes + common_volumes + host_volumes }}"

docker_image_name: "{{ image_name | default(inventory_hostname) }}"
repository: "{{ registry }}/{{ repository_path }}"
repository: "{{ registry }}{{ repository_path }}"
# multiple build variants are pushed, so repository_push_image references build_vars.tag
repository_push_image: "{% if repository is defined %}{{ repository }}/{% endif %}{{ docker_image_name }}:{{ build_vars.tag }}"
# but only one build variant (the one with the unaltered tag) is deployed, so repository_deploy_image references only version
Expand Down
9 changes: 9 additions & 0 deletions roles/aws_tools/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
awscli_package: awscli-exe-linux-x86_64.zip
awscli_stage_dest: /tmp/
awscli_bindir: /usr/local/aws
awscli_instdir: /usr/bin/aws
aws_ecr_username: AWS
aws_region_ecr: YOUR_AWS_REGION
aws_access_key_ecr: YOUR_ACCESS_KEY
aws_secret_key_ecr: YOUR_SECRET_KEY
28 changes: 28 additions & 0 deletions roles/aws_tools/tasks/install_awscli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- name: Install AWS CLI
when: awscli_command_path.rc != 0
block:
- name: Download AWS CLI
get_url:
url: "https://awscli.amazonaws.com/{{ awscli_package }}"
dest: "{{ awscli_stage_dest }}"
register: downloaded_awscli_archive

- name: Unarchive "{{ awscli_stage_archive.dest }}"
unarchive:
src: "{{ downloaded_awscli_archive.dest }}"
dest: "{{ awscli_stage_dest }}"
register: unarchive_awscli
when: downloaded_awscli_archive.rc == "0"

- name: Install AWS CLI
shell: "{{ unarchive_awscli.dest }}/install -i {{ awscli_instdir }} -b {{ awscli_bindir }}"
become: yes

- name: Remove temp files
file:
state: absent
path: "{{ item }}"
with_items:
- "{{ downloaded_awscli_archive.dest }}"
- "{{ unarchive_awscli.dest }}"

31 changes: 31 additions & 0 deletions roles/aws_tools/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
- name: Configure AWS CLI
block:
- name: Check if AWS CLI is installed
command: "which aws"
register: awscli_command_path
ignore_errors: yes
changed_when: no

- name: Install AWS CLI
when: awscli_command_path.rc != 0
include_tasks: install_awscli.yml

- name: Authenticate to ECR
environment:
AWS_ACCESS_KEY_ID: "{{ ecr_access_key_id }}"
AWS_SECRET_ACCESS_KEY: "{{ ecr_secret_access_key }}"
AWS_DEFAULT_REGION: "{{ ecr_aws_region }}"
shell: "{{ awscli_command_path.stdout }} ecr get-login-password"
become: "{{ docker_become_user is defined }}"
become_user: "{{ docker_become_user|default(omit) }}"
delegate_to: "{{ groups['docker_build_hosts'] | first }}"
run_once: true
no_log: yes
changed_when: no
register: ecr_password

- name: Set registry password
no_log: yes
changed_when: no
set_fact:
registry_password: "{{ ecr_password.stdout }}"
2 changes: 1 addition & 1 deletion roles/docker_image_build/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
build_vars: "{{ build_variant.build_vars }}"
loop: "{{ build_variants }}"
loop_control:
loop_var: build_variant
loop_var: build_variant
5 changes: 5 additions & 0 deletions roles/docker_login/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
- name: Set up AWS CLI for ECR Registry
when: is_ecr_registry is defined
include_role:
name: aws_tools

- name: Log in to repositories
docker_login:
registry: "{{ registry }}"
Expand Down

0 comments on commit 28064e2

Please sign in to comment.