Skip to content

Support metaarg expansion#151

Open
jedinym wants to merge 2 commits intokeilerkonzept:masterfrom
jedinym:fix-metaarg-expansion
Open

Support metaarg expansion#151
jedinym wants to merge 2 commits intokeilerkonzept:masterfrom
jedinym:fix-metaarg-expansion

Conversation

@jedinym
Copy link
Copy Markdown

@jedinym jedinym commented Aug 27, 2025

This PR adds support for metaarg expansion if an argument is nested.

Example Dockerfile:

ARG ALPINE_MINOR=10
ARG ALPINE_TAG=3.${ALPINE_MINOR}
ARG APP_BASE=scratch

FROM alpine:${ALPINE_TAG} AS build
# ...

FROM build
# ...

FROM ${APP_BASE}
# ...

The master version does not support argument nesting like that, resulting in unexpanded variables. Comparing the master version with this branch shows the difference:

$ dockerfile-json examples/Dockerfile.3
--- actual      2025-08-27 10:06:40.138591062 +0200
+++ expected    2025-08-27 10:05:48.028701625 +0200
@@ -10,7 +10,7 @@
       "Key": "ALPINE_TAG",
       "DefaultValue": "3.${ALPINE_MINOR}",
       "ProvidedValue": null,
-      "Value": "3.${ALPINE_MINOR}"
+      "Value": "3.10"
     },
     {
       "Key": "APP_BASE",
@@ -22,7 +22,8 @@
   "Stages": [
     {
       "Name": "build",
-      "BaseName": "alpine:3.${ALPINE_MINOR}",
+      "OrigCmd": "FROM",
+      "BaseName": "alpine:3.10",
       "Platform": "",
       "Comment": "",
       "SourceCode": "FROM alpine:${ALPINE_TAG} AS build",
@@ -40,12 +41,13 @@
       ],
       "As": "build",
       "From": {
-        "Image": "alpine:3.${ALPINE_MINOR}"
+        "Image": "alpine:3.10"
       },
       "Commands": null
     },
     {
       "Name": "",
+      "OrigCmd": "FROM",
       "BaseName": "build",
       "Platform": "",
       "Comment": "",
@@ -72,6 +74,7 @@
     },
     {
       "Name": "",
+      "OrigCmd": "FROM",
       "BaseName": "scratch",
       "Platform": "",
       "Comment": "",

Attaching full outputs of master branch and this branch below.
actual.json
expected.json

To support the use case where the Dockerfile uses a nested argument in
an image base name, we need to expand meta arguments with already
defined arguments.

Signed-off-by: Martin Jediny <jedinym@proton.me>
Changed the example Dockerfile.3 to contain nested arguments, to
showcase that dockerfile-json will expand arguments with previous
arguments.

Signed-off-by: Martin Jediny <jedinym@proton.me>
@jedinym jedinym marked this pull request as ready for review August 27, 2025 08:13
@mkosiarc
Copy link
Copy Markdown

@sgreben could you please take a look here?

@jedinym
Copy link
Copy Markdown
Author

jedinym commented Sep 30, 2025

@sgreben have you had the chance to take a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants