Skip to content

Commit 7676122

Browse files
committed
CI: Add check for conflict image build definition
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
1 parent 8192c31 commit 7676122

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: Check Duplicated Images
5+
6+
on:
7+
pull_request:
8+
branches: [main]
9+
types: [opened, reopened, ready_for_review, synchronize]
10+
paths:
11+
- "**/docker_image_build/*.yaml"
12+
- ".github/workflows/pr-check-duplicated-image.yml"
13+
- ".github/workflows/scripts/check_duplicated_image.py"
14+
workflow_dispatch:
15+
16+
# If there is a new commit, the previous jobs will be canceled
17+
concurrency:
18+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
19+
cancel-in-progress: true
20+
21+
jobs:
22+
check-duplicated-image:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Clean Up Working Directory
26+
run: sudo rm -rf ${{github.workspace}}/*
27+
28+
- name: Checkout Repo
29+
uses: actions/checkout@v4
30+
31+
- name: Check all the docker image build files
32+
run: |
33+
pip install PyYAML
34+
cd ${{github.workspace}}
35+
build_files=""
36+
for f in `find . -path "*/docker_image_build/build.yaml"`; do
37+
build_files="$build_files $f"
38+
done
39+
python3 .github/workflows/scripts/check_duplicated_image.py $build_files
40+
shell: bash
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import argparse
5+
import os.path
6+
import subprocess
7+
import sys
8+
9+
import yaml
10+
11+
images = {}
12+
13+
14+
def check_docker_compose_build_definition(file_path):
15+
with open(file_path, "r") as f:
16+
data = yaml.load(f, Loader=yaml.FullLoader)
17+
for service in data["services"]:
18+
if "build" in data["services"][service] and "image" in data["services"][service]:
19+
bash_command = "echo " + data["services"][service]["image"]
20+
image = (
21+
subprocess.run(["bash", "-c", bash_command], check=True, capture_output=True)
22+
.stdout.decode("utf-8")
23+
.strip()
24+
)
25+
build = data["services"][service]["build"]
26+
context = build.get("context", "")
27+
dockerfile = os.path.normpath(
28+
os.path.join(os.path.dirname(file_path), context, build.get("dockerfile", ""))
29+
)
30+
if not os.path.isfile(dockerfile):
31+
# dockerfile not exists in the current repo context, assume it's in 3rd party context
32+
dockerfile = os.path.normpath(os.path.join(context, build.get("dockerfile", "")))
33+
item = {"file_path": file_path, "service": service, "dockerfile": dockerfile}
34+
if image in images and dockerfile != images[image]["dockerfile"]:
35+
print("ERROR: !!! Found Conflicts !!!")
36+
print(f"Image: {image}, Dockerfile: {dockerfile}, defined in Service: {service}, File: {file_path}")
37+
print(
38+
f"Image: {image}, Dockerfile: {images[image]['dockerfile']}, defined in Service: {images[image]['service']}, File: {images[image]['file_path']}"
39+
)
40+
sys.exit(1)
41+
else:
42+
# print(f"Add Image: {image} Dockerfile: {dockerfile}")
43+
images[image] = item
44+
45+
46+
def parse_arg():
47+
parser = argparse.ArgumentParser(
48+
description="Check for conflicts in image build definition in docker-compose.yml files"
49+
)
50+
parser.add_argument("files", nargs="+", help="list of files to be checked")
51+
return parser.parse_args()
52+
53+
54+
def main():
55+
args = parse_arg()
56+
for file_path in args.files:
57+
check_docker_compose_build_definition(file_path)
58+
print("SUCCESS: No Conlicts Found.")
59+
return 0
60+
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)