|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +################################################################################ |
| 16 | +""" |
| 17 | +A CI script to ensure that the base OS version specified in a project's |
| 18 | +project.yaml file matches the FROM line in its Dockerfile. |
| 19 | +""" |
| 20 | + |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import yaml |
| 24 | + |
| 25 | +# Defines the base OS versions that are currently supported for use in project.yaml. |
| 26 | +# For now, only 'legacy' is permitted. This list will be expanded as new |
| 27 | +# base images are rolled out. |
| 28 | +SUPPORTED_VERSIONS = [ |
| 29 | + 'legacy', |
| 30 | + # 'ubuntu-20-04', |
| 31 | + # 'ubuntu-24-04', |
| 32 | +] |
| 33 | + |
| 34 | +# A map from the base_os_version in project.yaml to the expected Dockerfile |
| 35 | +# FROM tag. |
| 36 | +BASE_OS_TO_DOCKER_TAG = { |
| 37 | + 'legacy': 'latest', |
| 38 | + 'ubuntu-20-04': 'ubuntu-20-04', |
| 39 | + 'ubuntu-24-04': 'ubuntu-24-04', |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + """Checks the Dockerfile FROM tag against the project's base_os_version.""" |
| 45 | + if len(sys.argv) < 2: |
| 46 | + print(f'Usage: {sys.argv[0]} <project_path>', file=sys.stderr) |
| 47 | + return 1 |
| 48 | + |
| 49 | + project_path = sys.argv[1] |
| 50 | + project_yaml_path = os.path.join(project_path, 'project.yaml') |
| 51 | + dockerfile_path = os.path.join(project_path, 'Dockerfile') |
| 52 | + |
| 53 | + # 1. Get the base_os_version from project.yaml, defaulting to 'legacy'. |
| 54 | + base_os_version = 'legacy' |
| 55 | + if os.path.exists(project_yaml_path): |
| 56 | + with open(project_yaml_path) as f: |
| 57 | + config = yaml.safe_load(f) |
| 58 | + if config and 'base_os_version' in config: |
| 59 | + base_os_version = config['base_os_version'] |
| 60 | + |
| 61 | + # 2. Validate that the version is currently supported. |
| 62 | + if base_os_version not in SUPPORTED_VERSIONS: |
| 63 | + print( |
| 64 | + f'Error: base_os_version "{base_os_version}" is not yet supported. ' |
| 65 | + f'The currently supported versions are: "{", ".join(SUPPORTED_VERSIONS)}"', |
| 66 | + file=sys.stderr) |
| 67 | + return 1 |
| 68 | + |
| 69 | + # 3. Get the expected Dockerfile tag from our mapping. |
| 70 | + expected_tag = BASE_OS_TO_DOCKER_TAG[base_os_version] |
| 71 | + |
| 72 | + # 4. Read the Dockerfile and find the tag in the FROM line. |
| 73 | + if not os.path.exists(dockerfile_path): |
| 74 | + print(f'Error: Dockerfile not found at {dockerfile_path}', file=sys.stderr) |
| 75 | + return 1 |
| 76 | + |
| 77 | + dockerfile_tag = '' |
| 78 | + with open(dockerfile_path) as f: |
| 79 | + for line in f: |
| 80 | + if line.strip().startswith('FROM'): |
| 81 | + try: |
| 82 | + if ':' not in line: |
| 83 | + print( |
| 84 | + f'Error: Malformed FROM line in Dockerfile (missing tag): {line.strip()}', |
| 85 | + file=sys.stderr) |
| 86 | + return 1 |
| 87 | + dockerfile_tag = line.split(':')[1].strip() |
| 88 | + except IndexError: |
| 89 | + print(f'Error: Could not parse tag from Dockerfile FROM line: {line}', |
| 90 | + file=sys.stderr) |
| 91 | + return 1 |
| 92 | + break |
| 93 | + |
| 94 | + # 5. Compare and report. |
| 95 | + if dockerfile_tag != expected_tag: |
| 96 | + print( |
| 97 | + f'Error: Mismatch found in {project_path}.\n' |
| 98 | + f' - project.yaml (base_os_version): "{base_os_version}" (expects Dockerfile tag "{expected_tag}")\n' |
| 99 | + f' - Dockerfile FROM tag: "{dockerfile_tag}"\n' |
| 100 | + f'Please align the Dockerfile\'s FROM line to use the tag "{expected_tag}".', |
| 101 | + file=sys.stderr) |
| 102 | + return 1 |
| 103 | + |
| 104 | + print( |
| 105 | + f'Success: {project_path} is consistent (base_os_version: "{base_os_version}", Dockerfile tag: "{dockerfile_tag}").' |
| 106 | + ) |
| 107 | + return 0 |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + sys.exit(main()) |
0 commit comments