-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
260b043
commit e2e10ad
Showing
2 changed files
with
123 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# to run locally: | ||
# export CSPROJ_FILE=.github/test/src/core-demo-api.csproj | ||
# export CSPROJ_FILE=.github/test/src/core-demo-api.csproj or export GO_MOD_FILE=go.mod | ||
# export GITHUB_OUTPUT="./tmp/output.txt" | ||
# python3 build/create_dockerfile.py | ||
|
||
|
@@ -11,7 +11,10 @@ | |
def find_assembly_name(csproj_file): | ||
root = ET.parse(csproj_file).getroot() | ||
if len(root.findall("PropertyGroup/AssemblyName")) > 0: | ||
return root.findall("PropertyGroup/AssemblyName")[-1].text + ".dll" | ||
assembly_name = root.findall("PropertyGroup/AssemblyName")[-1].text | ||
if not isinstance(assembly_name, str): | ||
raise ValueError("Unable to find AssemblyName in csproj-file") | ||
return f"{assembly_name}.dll" | ||
else: | ||
return os.path.splitext(os.path.basename(csproj_file))[0] + ".dll" | ||
|
||
|
@@ -21,7 +24,9 @@ def find_docker_base_image_tag(csproj_file): | |
if len(root.findall("PropertyGroup/TargetFramework")) > 0: | ||
# .NET 8.0 | ||
framework = root.findall("PropertyGroup/TargetFramework")[-1].text | ||
tag = framework[3:] + "-alpine" | ||
if not isinstance(framework, str): | ||
raise ValueError("Unable to find TargetFramework in csproj-file") | ||
tag = f"{framework[3:]}-alpine" | ||
return tag | ||
else: | ||
raise ValueError("Unable to find TargetFramework in csproj-file") | ||
|
@@ -30,7 +35,7 @@ def find_docker_base_image_tag(csproj_file): | |
def find_docker_runtime_base_image(csproj_file): | ||
root = ET.parse(csproj_file).getroot() | ||
if "Sdk" not in root.attrib: | ||
raise ValueError("Unable to find Sdk in csproj-file") | ||
raise ValueError("Unable to find SDK in csproj-file") | ||
sdk = root.attrib["Sdk"] | ||
if sdk in ["Microsoft.NET.Sdk"]: | ||
return "mcr.microsoft.com/dotnet/runtime" | ||
|
@@ -102,31 +107,96 @@ def write_dockerfile( | |
print("Dockerfile:\n" + f.read()) | ||
|
||
|
||
def write_dockerfile_go(go_mod_path, filename): | ||
with open(os.environ["GITHUB_OUTPUT"], "a") as fh: | ||
key = "DOCKERFILE" | ||
print(f"{key}={filename}", file=fh) | ||
|
||
template = """FROM golang:alpine AS build | ||
LABEL maintainer="[email protected]" | ||
ENV GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 | ||
WORKDIR /app | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
COPY *.go ./ | ||
RUN go build -o ./out/executable . | ||
FROM alpine:latest | ||
LABEL maintainer="[email protected]" | ||
RUN addgroup application-group --gid 1001 && \\ | ||
adduser application-user --uid 1001 \\ | ||
--ingroup application-group \\ | ||
--disabled-password | ||
WORKDIR /app | ||
COPY --from=build /app/out . | ||
RUN chown --recursive application-user . | ||
USER application-user | ||
EXPOSE 8080 | ||
ENTRYPOINT ["./executable"] | ||
""" | ||
context = {"go_mod_path": go_mod_path} | ||
with open(filename, | ||
"w") as f: | ||
f.write(template.format(**context)) | ||
|
||
f = open(filename, "a") | ||
f.write( | ||
""" | ||
""" | ||
) | ||
f.close() | ||
|
||
|
||
def main(): | ||
csproj_file = os.environ.get("CSPROJ_FILE") | ||
if csproj_file is None: | ||
raise ValueError("Missing required Environment Variable CSPROJ_FILE") | ||
print("csproj_file: " + csproj_file) | ||
csproj_file = csproj_file.strip("'\" ") | ||
|
||
directory = os.path.join("tmp", str(uuid.uuid4())) | ||
os.makedirs(directory) | ||
filename = os.path.join(directory, "Dockerfile") | ||
print("filename: " + filename) | ||
|
||
assembly_name = find_assembly_name(csproj_file) | ||
print("assembly name: " + assembly_name) | ||
docker_base_image_tag = find_docker_base_image_tag(csproj_file) | ||
print("base image tag: " + docker_base_image_tag) | ||
docker_runtime_base_image = find_docker_runtime_base_image(csproj_file) | ||
print("runtime base image: " + docker_runtime_base_image) | ||
write_dockerfile( | ||
csproj_file, | ||
assembly_name, | ||
docker_base_image_tag, | ||
docker_runtime_base_image, | ||
filename, | ||
) | ||
go_mod_file = os.environ.get("GO_MOD_FILE") | ||
|
||
if csproj_file is not None: | ||
print("csproj_file: " + csproj_file) | ||
csproj_file = csproj_file.strip("'\" ") | ||
|
||
directory = os.path.join("tmp", str(uuid.uuid4())) | ||
os.makedirs(directory) | ||
filename = os.path.join(directory, "Dockerfile") | ||
print("filename: " + filename) | ||
|
||
assembly_name = find_assembly_name(csproj_file) | ||
print("assembly name: " + assembly_name) | ||
docker_base_image_tag = find_docker_base_image_tag(csproj_file) | ||
print("base image tag: " + docker_base_image_tag) | ||
docker_runtime_base_image = find_docker_runtime_base_image(csproj_file) | ||
print("runtime base image: " + docker_runtime_base_image) | ||
write_dockerfile( | ||
csproj_file, | ||
assembly_name, | ||
docker_base_image_tag, | ||
docker_runtime_base_image, | ||
filename, | ||
) | ||
elif go_mod_file is not None: | ||
print("go_mod_file: " + go_mod_file) | ||
go_mod_file = go_mod_file.strip("'\" ") | ||
go_mod_path = os.path.dirname(go_mod_file) | ||
|
||
directory = os.path.join("tmp", str(uuid.uuid4())) | ||
os.makedirs(directory) | ||
filename = os.path.join(directory, "Dockerfile") | ||
print("filename: " + filename) | ||
|
||
write_dockerfile_go(go_mod_path, filename) | ||
else: | ||
raise ValueError("CSPROJ_FILE or GO_MOD_FILE must be set.") | ||
|
||
|
||
if __name__ == "__main__": | ||
|