]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Updates the utility script to allow building of other Dockerfiles for building any...
authorTrenton Holmes <holmes.trenton@gmail.com>
Sun, 24 Apr 2022 22:20:36 +0000 (15:20 -0700)
committerTrenton Holmes <holmes.trenton@gmail.com>
Mon, 25 Apr 2022 18:32:55 +0000 (11:32 -0700)
.build-config.json [new file with mode: 0644]
build-docker-imaage.sh [deleted file]
build-docker-image.sh [new file with mode: 0755]
docker-builders/get-build-json.py

diff --git a/.build-config.json b/.build-config.json
new file mode 100644 (file)
index 0000000..32cf968
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "qpdf": {
+      "version": "10.6.3"
+    },
+  "jbig2enc": {
+      "version": "0.29",
+      "git_tag": "0.29"
+    }
+}
diff --git a/build-docker-imaage.sh b/build-docker-imaage.sh
deleted file mode 100755 (executable)
index 7d097fe..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env bash
-
-# Example Usage: ./build-docker-imaage.sh -t paperless-ngx:my-awesome-feature
-
-set -eux
-
-# Parse what we can from Pipfile.lock
-pikepdf_version=$(jq ".default.pikepdf.version" Pipfile.lock  | sed 's/=//g' | sed 's/"//g')
-psycopg2_version=$(jq ".default.psycopg2.version" Pipfile.lock | sed 's/=//g' | sed 's/"//g')
-
-# Get the branch name
-frontend=$(git rev-parse --abbrev-ref HEAD)
-
-# Directly set these
-# Future enhancement: Set this in a single location
-qpdf_version="10.6.3"
-jbig2enc_version="0.29"
-
-docker build . \
-       --build-arg JBIG2ENC_VERSION="${jbig2enc_version}" \
-       --build-arg QPDF_VERSION="${qpdf_version}" \
-       --build-arg PIKEPDF_VERSION="${pikepdf_version}" \
-       --build-arg PSYCOPG2_VERSION="${psycopg2_version}" \
-       --build-arg FRONTEND_VERSION="${frontend}" "$@"
diff --git a/build-docker-image.sh b/build-docker-image.sh
new file mode 100755 (executable)
index 0000000..41c9516
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+
+# Helper script for building the Docker image locally.
+# Parses and provides the nessecary versions of other images to Docker
+# before passing in the rest of script args
+
+# First Argument: The Dockerfile to build
+# Other Arguments: Additional arguments to docker build
+
+# Example Usage:
+#      ./build-docker-image.sh Dockerfile -t paperless-ngx:my-awesome-feature
+#      ./build-docker-image.sh docker-builders/Dockerfile.qpdf -t paperless-ngx-build-qpdf:x.y.z
+
+set -eux
+
+# Parse what we can from Pipfile.lock
+pikepdf_version=$(jq ".default.pikepdf.version" Pipfile.lock  | sed 's/=//g' | sed 's/"//g')
+psycopg2_version=$(jq ".default.psycopg2.version" Pipfile.lock | sed 's/=//g' | sed 's/"//g')
+# Read this from the other config file
+qpdf_version=$(jq ".qpdf.version" .build-config.json | sed 's/"//g')
+jbig2enc_version=$(jq ".jbig2enc.version" .build-config.json | sed 's/"//g')
+# Get the branch name
+frontend=$(git rev-parse --abbrev-ref HEAD)
+
+if [ ! -f "$1" ]; then
+       echo "$1 is not a file, please provide the Dockerfile"
+       exit 1
+fi
+
+docker build --file "$1" \
+       --build-arg JBIG2ENC_VERSION="${jbig2enc_version}" \
+       --build-arg QPDF_VERSION="${qpdf_version}" \
+       --build-arg PIKEPDF_VERSION="${pikepdf_version}" \
+       --build-arg PSYCOPG2_VERSION="${psycopg2_version}" \
+       --build-arg FRONTEND_VERSION="${frontend}" "${@:2}" .
index 07a33b4d469cda9c3b6dd6a85e12bb03abd462e9..4b96ac8d79a95dffe4a723e8509f9de3dd035db9 100755 (executable)
@@ -20,28 +20,6 @@ import os
 from pathlib import Path
 from typing import Final
 
-CONFIG: Final = {
-    # All packages need to be in the dict, even if not configured further
-    # as it is used for the possible choices in the argument
-    "psycopg2": {},
-    "frontend": {},
-    # Most information about Python packages comes from the Pipfile.lock
-    # Exception being pikepdf, which needs a specific qpdf version
-    "pikepdf": {
-        "qpdf_version": "10.6.3",
-    },
-    # For other packages, version and Git information are directly configured
-    # These require manual updates to this file for version updates
-    "qpdf": {
-        "version": "10.6.3",
-        "git_tag": "N/A",
-    },
-    "jbig2enc": {
-        "version": "0.29",
-        "git_tag": "0.29",
-    },
-}
-
 
 def _get_image_tag(
     repo_name: str,
@@ -58,47 +36,63 @@ def _main():
     parser.add_argument(
         "package",
         help="The name of the package to generate JSON for",
-        choices=CONFIG.keys(),
     )
 
-    args = parser.parse_args()
+    PIPFILE_LOCK_PATH: Final[Path] = Path("Pipfile.lock")
+    BUILD_CONFIG_PATH: Final[Path] = Path(".build-config.json")
 
-    pip_lock = Path("Pipfile.lock")
+    # Read the main config file
+    build_json: Final = json.loads(BUILD_CONFIG_PATH.read_text())
 
-    repo_name = os.environ["GITHUB_REPOSITORY"]
+    # Read Pipfile.lock file
+    pipfile_data: Final = json.loads(PIPFILE_LOCK_PATH.read_text())
 
-    # The JSON object we'll output
-    output = {"name": args.package}
+    args: Final = parser.parse_args()
 
-    # Read Pipfile.lock file
-    pipfile_data = json.loads(pip_lock.read_text())
+    repo_name: Final[str] = os.environ["GITHUB_REPOSITORY"]
 
-    # Read the version from Pipfile.lock
-    if args.package in pipfile_data["default"]:
+    # Default output values
+    version = None
+    git_tag = None
+    extra_config = {}
 
+    if args.package == "frontend":
+        # Version is just the branch or tag name
+        version = os.environ["GITHUB_REF_NAME"]
+    elif args.package in pipfile_data["default"]:
+        # Read the version from Pipfile.lock
         pkg_data = pipfile_data["default"][args.package]
-
         pkg_version = pkg_data["version"].split("==")[-1]
-
-        output["version"] = pkg_version
+        version = pkg_version
 
         # Based on the package, generate the expected Git tag name
         if args.package == "pikepdf":
-            git_tag_name = f"v{pkg_version}"
+            git_tag = f"v{pkg_version}"
         elif args.package == "psycopg2":
-            git_tag_name = pkg_version.replace(".", "_")
+            git_tag = pkg_version.replace(".", "_")
 
-        output["git_tag"] = git_tag_name
+        # Any extra/special values needed
+        if args.package == "pikepdf":
+            extra_config["qpdf_version"] = build_json["qpdf"]["version"]
 
-    # Use the basic ref name, minus refs/heads or refs/tags for frontend builder image
-    elif args.package == "frontend":
-        output["version"] = os.environ["GITHUB_REF_NAME"]
+    elif args.package in build_json:
+        version = build_json[args.package]["version"]
 
-    # Add anything special from the config
-    output.update(CONFIG[args.package])
+        if "git_tag" in build_json[args.package]:
+            git_tag = build_json[args.package]["git_tag"]
+    else:
+        raise NotImplementedError(args.package)
 
-    # Based on the package and environment, generate the Docker image tag
-    output["image_tag"] = _get_image_tag(repo_name, args.package, output["version"])
+    # The JSON object we'll output
+    output = {
+        "name": args.package,
+        "version": version,
+        "git_tag": git_tag,
+        "image_tag": _get_image_tag(repo_name, args.package, version),
+    }
+
+    # Add anything special a package may need
+    output.update(extra_config)
 
     # Output the JSON info to stdout
     print(json.dumps(output))