Commit a6e0bbb6 authored by José Ángel Fentánez's avatar José Ángel Fentánez
Browse files

[FIX] gitlab_tools: only push the image of the specified customer

parent 485ab298
......@@ -4,7 +4,7 @@ from deployv_addon_gitlab_tools.common import check_env_vars
from deployv_addon_gitlab_tools.commands.check_keys import check_credentials, is_docker_login
from deployv.helpers import utils
from docker import errors, APIClient as Client
from os import environ, unsetenv
from os import environ
import re
import subprocess
import time
......@@ -279,13 +279,13 @@ def push_image(image_name, image_tag):
logger.info('Image pushed correctly')
def notify_orchest(tag, is_latest=False):
def notify_orchest(tag, customer, is_latest=False):
image_name = '{image}:{tag}'.format(image=environ['_IMAGE_REPO'], tag=tag)
res = requests.post(
environ['ORCHEST_REGISTRY'], data=json.dumps({
'image_name': image_name, 'is_latest': is_latest, 'branch_name': environ['CI_COMMIT_REF_NAME'],
'job_id': environ['CI_JOB_ID'], 'project_id': environ['CI_PROJECT_ID'],
'commit': environ['CI_COMMIT_SHA'][:7]}),
'commit': environ['CI_COMMIT_SHA'][:7], 'customer_id': customer}),
headers={'Content-Type': 'application/json', 'Orchest-Token': environ['ORCHEST_TOKEN']})
if res.status_code != 200:
logger.error('Failed to notify orchest about the new image: %s', res.text)
......@@ -341,7 +341,7 @@ def test_images(**kwargs):
instance_image = generate_image_name('instance{0}_{1}'.format(
base_name, kwargs.get('ci_pipeline_id')))
customers = environ.get('CUSTOMER', environ.get('CI_PROJECT_NAME'))
customer = environ.get('CUSTOMER', environ.get('CI_PROJECT_NAME'))
version_tag = environ.get('VERSION').replace('.', '')
environ.update({
......@@ -367,17 +367,16 @@ def test_images(**kwargs):
sys.exit(1)
is_latest = False
if kwargs.get('push_image'):
for customer in customers.split(','):
customer_img = '{customer}{ver}'.format(customer=customer.strip(),
ver=version_tag)
image_repo = '{url}/{image}'.format(url=environ.get('IMAGE_REPO_URL'),
image=customer_img)
environ.update({'_IMAGE_REPO': image_repo})
# TODO: if we decide to build and push every image, just move the _IMAGE_TAG outside the if
if environ.get('CI_COMMIT_REF_NAME') == environ.get('VERSION'):
push_image(instance_image, 'latest')
is_latest = True
push_image(instance_image, environ['_IMAGE_TAG'])
notify_orchest(environ['_IMAGE_TAG'], is_latest=is_latest)
customer_img = '{customer}{ver}'.format(customer=customer.strip(),
ver=version_tag)
image_repo = '{url}/{image}'.format(url=environ.get('IMAGE_REPO_URL'),
image=customer_img)
environ.update({'_IMAGE_REPO': image_repo})
# TODO: if we decide to build and push every image, just move the _IMAGE_TAG outside the if
if environ.get('CI_COMMIT_REF_NAME') == environ.get('VERSION'):
push_image(instance_image, 'latest')
is_latest = True
push_image(instance_image, environ['_IMAGE_TAG'])
notify_orchest(environ['_IMAGE_TAG'], customer, is_latest=is_latest)
clear_images()
sys.exit(0)
......@@ -51,14 +51,14 @@ def push_image(tags):
sys.exit(1)
def notify_orchest(tag, is_latest=False):
def notify_orchest(tag, customer, is_latest=False):
image_name = '{image}:{tag}'.format(image=environ['_IMAGE_REPO'],
tag=tag)
res = requests.post(
environ['ORCHEST_REGISTRY'], data=json.dumps({
'image_name': image_name, 'is_latest': is_latest, 'branch_name': environ['CI_COMMIT_REF_NAME'],
'job_id': environ['CI_JOB_ID'], 'project_id': environ['CI_PROJECT_ID'],
'commit': environ['CI_COMMIT_SHA'][:7]}),
'commit': environ['CI_COMMIT_SHA'][:7], 'customer_id': customer}),
headers={'Content-Type': 'application/json', 'Orchest-Token': environ['ORCHEST_TOKEN']})
if res.status_code != 200:
_logger.error('Failed to notify orchest about the new image: %s', res.text)
......@@ -99,10 +99,10 @@ def notify_orchest(tag, is_latest=False):
help=("Env var: ORCHEST_TOKEN."))
def upload_image(**kwargs):
check_env_vars(**kwargs)
customer_names = environ.get('CUSTOMER', environ.get('CI_PROJECT_NAME'))
customer = environ.get('CUSTOMER', environ.get('CI_PROJECT_NAME'))
version_tag = environ.get('VERSION').replace('.', '')
image_name = '{customer}_{ver}'.format(
customer=customer_names.replace(' ', '').replace(',', '_'),
customer=customer.replace(' ', '').replace(',', '_'),
ver=version_tag
)
check_credentials(environ.get('PRIVATE_DEPLOY_KEY', False))
......@@ -113,13 +113,11 @@ def upload_image(**kwargs):
if environ.get('CI_COMMIT_REF_NAME') == environ.get('VERSION'):
tags.append('latest')
is_latest = True
for customer_name in customer_names.split(','):
if customer_name.strip():
customer_img = '{customer}{ver}'.format(customer=customer_name.strip(),
ver=version_tag)
image_repo = '{url}/{image}'.format(url=environ.get('IMAGE_REPO_URL'),
image=customer_img)
environ.update({'_IMAGE_REPO': image_repo})
push_image(tags)
notify_orchest(image_sha, is_latest=is_latest)
customer_img = '{customer}{ver}'.format(customer=customer.strip(),
ver=version_tag)
image_repo = '{url}/{image}'.format(url=environ.get('IMAGE_REPO_URL'),
image=customer_img)
environ.update({'_IMAGE_REPO': image_repo})
push_image(tags)
notify_orchest(image_sha, customer, is_latest=is_latest)
sys.exit(0)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment