| """Step class for uploading new Docker images for testing team.""" |
| |
| from __future__ import absolute_import |
| import logging |
| import os |
| |
| from slave import base_step |
| |
| class DockerPushTestingException(Exception): |
| """Raised when an error occurs with DockerPushTestingStep.""" |
| |
| |
| class DockerPushTestingStep(base_step.BaseStep): |
| """Uploads the latest Docker image.""" |
| |
| def __init__(self, bucket, path_info, **kwargs): |
| """ Creates a DockerPushTestingStep instance. |
| |
| Args: |
| bucket: The URL of the docker bucket where the image will be pushed and |
| stored |
| path_info: Dictionary to have the project and internal path to the |
| Dockerfile. For example: |
| 'path_info': { |
| 'project': 'project_repo', |
| 'dockerfile': 'path/to/dockerfile/folder' |
| } |
| **kwargs: Any additional args to pass to BaseStep. |
| """ |
| base_step.BaseStep.__init__(self, |
| name='push home-testing Docker image', |
| **kwargs) |
| self._docker_bucket = bucket |
| self._dockerfile_path = os.path.join( |
| self.get_project_path(path_info['project']), |
| path_info['dockerfile']) |
| |
| def _push_image(self): |
| """Pushes the image to GCR. |
| |
| Returns: |
| True if push succeeds, False otherwise |
| """ |
| cmd = [ |
| 'cd', |
| self._dockerfile_path, |
| ';', |
| 'docker', |
| 'push', |
| self._docker_bucket] |
| return_code, _, stderr = self.exec_subprocess(cmd) |
| if return_code != 0: |
| logging.error('Failed to push image: %s', stderr) |
| return False |
| return True |
| |
| def run(self): |
| """Push the build Docker image. |
| |
| Returns: |
| True if successfull, else False. |
| """ |
| success = self._push_image() |
| if not success: |
| self.add_review({'message': 'Failed to push the image'}) |
| return success |