77import sys
88import tempfile
99
10- def read_file_list (list_path ):
11- """
12- Reads a file containing file paths, ignoring empty lines and lines starting with '#'.
13- Returns a list of relative file paths.
14- """
15- with open (list_path , "r" ) as f :
16- lines = [line .strip () for line in f ]
17- return [line for line in lines if line and not line .startswith ("#" )]
18-
19- def copy_files_to_dir (file_list , dest_dir ):
20- """
21- Copies files to dest_dir, preserving their relative paths.
22- """
23- for rel_path in file_list :
24- abs_src = os .path .abspath (rel_path )
25- abs_dest = os .path .abspath (os .path .join (dest_dir , rel_path ))
26- os .makedirs (os .path .dirname (abs_dest ), exist_ok = True )
27- shutil .copy2 (abs_src , abs_dest )
28-
29- def write_dockerfile (dest_dir , entrypoint ):
30- """
31- Writes a Dockerfile that installs Python dependencies, GitHub CLI, and CodeQL CLI.
32- """
33- dockerfile = f'''
34- FROM python:3.11-slim
35-
36- WORKDIR /app
37-
38- # Install system dependencies
39- RUN apt-get update && apt-get install -y \\
40- curl \\
41- unzip \\
42- git \\
43- ca-certificates \\
44- && rm -rf /var/lib/apt/lists/*
45-
46- # Install Docker CLI (debian)
47- RUN apt-get update \\
48- && install -m 0755 -d /etc/apt/keyrings \\
49- && curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc \\
50- && chmod a+r /etc/apt/keyrings/docker.asc \\
51- && echo \\
52- "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \\
53- $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \\
54- tee /etc/apt/sources.list.d/docker.list > /dev/null \\
55- && apt-get update && apt-get install -y docker-ce-cli \\
56- && rm -rf /var/lib/apt/lists/*
57-
58- # Install GitHub CLI
59- RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \\
60- && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \\
61- && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \\
62- && apt-get update \\
63- && apt-get install -y gh \\
64- && rm -rf /var/lib/apt/lists/*
65-
66- # Install CodeQL CLI
67- RUN curl -Ls -o /tmp/codeql.zip https://github.com/github/codeql-cli-binaries/releases/latest/download/codeql-linux64.zip \\
68- && unzip /tmp/codeql.zip -d /opt \\
69- && mv /opt/codeql /opt/codeql-cli \\
70- && ln -s /opt/codeql-cli/codeql /usr/local/bin/codeql \\
71- && rm /tmp/codeql.zip
72-
73- COPY . /app
74-
75- # Install CodeQL pack dependencies
76- RUN codeql pack install /app/src/seclab_taskflow_agent/mcp_servers/codeql/queries/mcp-cpp
77- RUN codeql pack install /app/src/seclab_taskflow_agent/mcp_servers/codeql/queries/mcp-js
78-
79- # Install Python dependencies if pyproject.toml exists
80- RUN pip install hatch
81- RUN if [ -f pyproject.toml ]; then hatch run sync-deps; fi
82-
83- ENTRYPOINT ["hatch", "run", "{ entrypoint } "]
84- '''
85- with open (os .path .join (dest_dir , "Dockerfile" ), "w" ) as f :
86- f .write (dockerfile )
87-
8810def get_image_digest (image_name , tag ):
8911 result = subprocess .run (
9012 ["docker" , "buildx" , "imagetools" , "inspect" , f"{ image_name } :{ tag } " ],
@@ -111,23 +33,13 @@ def build_and_push_image(dest_dir, image_name, tag):
11133 f .write (digest )
11234
11335if __name__ == "__main__" :
114- if len (sys .argv ) != 5 :
115- print ("Usage: python build_and_publish_docker.py <file_list.txt> <entrypoint.py> < ghcr_username/repo> <tag>" )
116- print ("Example: python build_and_publish_docker.py files.txt main.py ghcr.io/anticomputer/my-python-app latest" )
36+ if len (sys .argv ) != 3 :
37+ print ("Usage: python build_and_publish_docker.py <ghcr_username/repo> <tag>" )
38+ print ("Example: python build_and_publish_docker.py ghcr.io/anticomputer/my-python-app latest" )
11739 sys .exit (1 )
11840
119- file_list_path = sys .argv [1 ]
120- entrypoint_py = sys .argv [2 ]
121- image_name = sys .argv [3 ]
122- tag = sys .argv [4 ]
123-
124- # Read file paths
125- file_list = read_file_list (file_list_path )
41+ image_name = sys .argv [1 ]
42+ tag = sys .argv [2 ]
12643
127- with tempfile .TemporaryDirectory () as build_dir :
128- # Copy files
129- copy_files_to_dir (file_list , build_dir )
130- # Write Dockerfile
131- write_dockerfile (build_dir , entrypoint_py )
132- # Build and push image
133- build_and_push_image (build_dir , image_name , tag )
44+ # Build and push image
45+ build_and_push_image ("docker" , image_name , tag )
0 commit comments