Skip to content

Commit 8a21b05

Browse files
committed
Add logic to determine when to build and push images. Add registry vars. Remove old registry vars
1 parent a64328b commit 8a21b05

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

ansible/delta-hosts/variables

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,13 @@ pg_pass=59a5524e-a772-11e5-bedc-1bdc0db458b3
176176
pg_user=astral
177177
rabbit_password=wKK7g7NWKpQXEeSzyWB7mIpxZIL8H2mDSf3Q6czR3Vk
178178
rabbit_username=o2mdLh9N9Ke2GzhoK8xsruYPhIQFN7iEL44dQJoq7OM
179-
registry_host=10.8.4.126
180179
user_content_domain=runnableapp.com
181180
vault_auth_token=578c9767-5af8-8490-0954-5d330f27b088
182181
vault_token_01=0d324dc7d4cbd94790fd08809d06fb1e28e21e185910081c7646e3e49924f6ed01
183182
vault_token_02=42dc8a69df174e77eb47a63b6ef4709bec57101cb1bff11a71c91b73b8bc046102
184183
vault_token_03=47f3cb74f5374fa3c51c90fd25e3d4cc851034de97584995fce5fc5382342f1f03
184+
registry_username=runnable+deltapush
185+
registry_token=4PX2AU9QIJSCDLZEXILYX6ZP2RCXY1HR10WVZKWVR0JW8DS5IIY87D96V0RACMK5
185186

186187
[ec2:vars]
187188
aws_custid=437258487404

ansible/group_vars/alpha-shiva.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ container_envs: >
2121
-e AWS_ACCESS_KEY_ID={{ aws_access_key_id }}
2222
-e AWS_SECRET_ACCESS_KEY={{ aws_secret_access_key }}
2323
-e NODE_ENV={{ node_env }}
24-
-e REGISTRY_HOST={{ registry_host }}
2524
-e ROLLBAR_KEY={{ shiva_rollbar_token }}
2625
-e DOCKER_PORT={{ docker_port }}
2726
Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,81 @@
11
---
22
# commands to build an image
3-
#
3+
- name: check if image is a tag and check environment
4+
tags: deploy
5+
set_fact:
6+
is_image_tag: '{{ git_branch | match("^v([0-9]+)\.([0-9]+)\.([0-9]+)$") }}'
7+
is_production_delta: '{{ node_env is defined and node_env=="production-delta" }}'
8+
49
- name: Ensure Tag Deploy For Prod
510
tags: deploy
6-
when: node_env is defined and node_env=="production-delta" and not git_branch | match("^v([0-9]+)\.([0-9]+)\.([0-9]+)$")
7-
fail: msg="only tag can be deployed on prod not {{ git_branch }}"
11+
when: (is_production_delta and not is_image_tag)
12+
fail: msg="only tag can be deployed on prod not {{ container_tag }}"
13+
14+
- name: set if is image tag and production delta
15+
tags: deploy
16+
set_fact:
17+
is_prod_and_tag: "{{ is_image_tag and is_production_delta }}"
18+
19+
- name: query registry for tag
20+
tags: deploy
21+
uri:
22+
# Overwrite the name of the image repository (`runnable-angular`, big-poppa-http) with `repository_name`
23+
url: https://quay.io/api/v1/repository/runnable/{{ repository_name | default(name) }}/tag/?limit=1&specificTag={{ container_tag }}
24+
method: GET
25+
headers:
26+
Authorization: 'Bearer QB9UzzNhwClqMgRyMgNGrSGLdUYZPJLJALdcpKRa'
27+
register: currently_built_tags
28+
29+
- name: set number of images built
30+
tags: deploy
31+
set_fact:
32+
no_images_found: "{{ currently_built_tags|json_query('json.tags')|length == 0 }}"
33+
34+
- name: set number of images built
35+
tags: deploy
36+
set_fact:
37+
# Only build the image if no images are found
38+
# Use built images in staging/gamma unless forced
39+
build_image: '{{ no_images_found or (force_image_push is defined and force_image_push) }}'
40+
# Only push the image if on prod environment
41+
# Also, only push image if and no images are found or image push is forced
42+
# Use `-e "force_push_image=true"` in the command line to force image push
43+
push_image: '{{ is_prod_and_tag and (no_images_found or (force_image_push is defined and force_image_push)) }}'
44+
45+
- name: debug
46+
debug:
47+
msg: 'build_image: {{ build_image }}, push_image: {{ push_image }}, no_images_found: {{ no_images_found }}, is_prod_and_tag: {{ is_prod_and_tag }}'
848

949
- name: create build folder
1050
become: true
51+
when: build_image
1152
file:
1253
path: "{{ build_dir }}/{{ name }}"
1354
state: directory
1455

1556
- name: pull the git repository
1657
tags: deploy
58+
when: build_image
1759
become: true
1860
git:
1961
repo: "{{ repo }}"
2062
dest: "{{ build_dir }}/{{ name }}/repo"
21-
version: "{{ git_branch }}"
63+
version: "{{ container_tag }}"
2264
update: yes
2365
accept_hostkey: yes
2466
force: yes
2567

2668
- name: get new tags from remote
2769
tags: deploy
70+
when: build_image
2871
become: true
2972
shell: "git fetch --tags"
3073
args:
3174
chdir: "{{ build_dir }}/{{ name }}/repo"
3275

3376
- name: get latest tag name
3477
tags: deploy
78+
when: build_image
3579
become: true
3680
shell: "git describe --tags `git rev-list --tags --max-count=1`"
3781
args:
@@ -40,26 +84,28 @@
4084

4185
- name: ensure latest tag is deployed
4286
tags: deploy
43-
fail: msg="Cannot deploy -{{ git_branch }}- because latest is -{{latest_tag.stdout}}-. Bypass with `-t i_am_deploying_an_old_tag`"
44-
when: (node_env is not defined or node_env is defined and node_env=="production-delta") and
45-
(latest_tag.stdout != git_branch and i_am_deploying_an_old_tag is not defined)
87+
fail: msg="Cannot deploy {{ container_tag}} because latest is {{latest_tag.stdout}}. Bypass with `-t i_am_deploying_an_old_tag`"
88+
when: build_image and is_production_delta and (latest_tag.stdout != container_tag and i_am_deploying_an_old_tag is not defined)
4689

4790
- name: copy dockerfile to build folder
4891
tags: deploy
92+
when: build_image
4993
become: true
5094
template:
5195
src: "{{ dockerfile }}"
5296
dest: "{{ build_dir }}/{{ name }}"
5397

5498
- name: copy .dockerignore file into build folder
5599
tags: deploy
100+
when: build_image
56101
become: true
57102
template:
58103
src: ".dockerignore"
59104
dest: "{{ build_dir }}/{{ name }}"
60105

61106
- name: copy secrets into build dir
62107
tags: [ deploy ]
108+
when: build_image
63109
become: true
64110
copy:
65111
src=./secrets/docker-client/{{ file_name_item }}
@@ -75,11 +121,13 @@
75121

76122
- name: build docker image and tag
77123
tags: deploy
124+
when: build_image
78125
become: yes
79126
command: docker build {{ build_args | default("") }} --tag="{{ container_image }}:{{ container_tag }}" "{{ build_dir }}/{{ name }}"
80127

81128
- name: remove secrets from build dir
82129
tags: [ deploy ]
130+
when: build_image
83131
become: true
84132
file:
85133
path: "{{ build_dir }}/{{ name }}/{{ file_name_item }}"
@@ -90,22 +138,20 @@
90138
loop_control:
91139
loop_var: file_name_item
92140

93-
- name: set whether image will be pushed
94-
when: git_branch | match("^v([0-9]+)\.([0-9]+)\.([0-9]+)$") and ((node_env is defined and node_env=='production-delta') or force_push_image)
95-
set_fact:
96-
push_image: true
97-
98-
- name: login to docker hub
141+
- name: login to registry
142+
tags: deploy
99143
become: yes
100-
when: push_image is defined
101-
command: docker login -u {{ docker_hub_username }} -p {{ docker_hub_password }}
144+
when: build_image and push_image
145+
command: docker login -u {{ registry_username }} -p {{ registry_token }} {{ registry_host }}
102146

103147
- name: push docker image
148+
tags: deploy
104149
become: yes
105-
when: push_image is defined
150+
when: build_image and push_image
106151
command: docker push {{ container_image }}:{{ container_tag }}
107152

108-
- name: logout of docker hub
153+
- name: logout of registry
154+
tags: deploy
109155
become: yes
110-
when: push_image is defined
156+
when: build_image and push_image
111157
command: docker logout

0 commit comments

Comments
 (0)