| """Build step class for building home testing docker image.""" |
| |
| from __future__ import absolute_import |
| import logging |
| import os |
| |
| from slave import base_step |
| |
| |
| class DockerImageError(Exception): |
| """Raised on step exceptions.""" |
| |
| |
| class DockerImageBuildStep(base_step.BaseStep): |
| """Steps for building docker testing image.""" |
| |
| def __init__(self, bucket, path_info, **kwargs): |
| """Creates a DockerTestingImageBuildStep 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='docker_testing_image', |
| **kwargs) |
| self._docker_bucket = bucket |
| self._dockerfile_path = os.path.join( |
| self.get_project_path(path_info['project']), |
| path_info['dockerfile']) |
| |
| def _build_image(self): |
| """Builds and tags the image. |
| |
| Returns: |
| If successfully built returns True, else False. |
| """ |
| build_tag = ':{}'.format(self.build_number) |
| |
| cmd = [ |
| 'cd', |
| self._dockerfile_path, |
| ';', |
| 'docker', |
| 'build', |
| '-t', |
| self._docker_bucket, |
| '-t', |
| self._docker_bucket + build_tag, |
| '.'] |
| |
| return_code, _, stderr = self.exec_subprocess(cmd) |
| |
| if return_code != 0: |
| logging.error( |
| 'Failed to build or tag Docker image:\n%s', stderr) |
| return False |
| return True |
| |
| def run(self): |
| """Creates the Docker image and makes the push to GCR. |
| |
| Sets a build property "review" with the output in case of failure. |
| |
| Returns: |
| True if there were no test errors, False otherwise. |
| """ |
| success = self._build_image() |
| if not success: |
| self.add_review({'message': 'Failed to build Docker image.'}) |
| return success |