Skip to content

Commit

Permalink
fix: include target
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Dec 24, 2023
1 parent affa6d8 commit bee6f2a
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/core/loadDockerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function loadDockerfile(filePath, fileContext = {}) {
nestingLevel = 0,
dockerContext,
scope = [],
isRootInclude,
} = fileContext

const relativeFilePath = path.relative(dockerContext, filePath)
Expand Down Expand Up @@ -77,6 +78,7 @@ async function loadDockerfile(filePath, fileContext = {}) {
stageAlias: parentStageAlias,
filePath,
isRootFile,
isRootInclude,
scope,
relativeFilePath,
})
Expand Down
17 changes: 13 additions & 4 deletions src/core/postProcessDockerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ const scopeStages = require("./scopeStages")

module.exports = function postProcessDockerfile(
content,
{ stageTarget, stageAlias, filePath, isRootFile, relativeFilePath, scope },
{
stageTarget,
stageAlias,
filePath,
isRootFile,
isRootInclude,
relativeFilePath,
scope,
},
) {
const instructions = parseDockerfile(content)

if (!isRootFile) {
if (!isRootFile && !isRootInclude) {
scopeStages(instructions, { relativeFilePath })
}

Expand Down Expand Up @@ -46,7 +54,8 @@ module.exports = function postProcessDockerfile(
if (matchTargetStage) {
stageTargetFound = true
}
const isLastTarget = !localStageTarget && currentFromCount === fromCount
const isLastTarget =
!isRootInclude && !localStageTarget && currentFromCount === fromCount
if (stageAlias && (matchTargetStage || isLastTarget)) {
result += `FROM ${stageName} AS ${stageAlias}` + "\n"
}
Expand All @@ -61,7 +70,7 @@ module.exports = function postProcessDockerfile(

result = deduplicateStages(result)

if (isRootFile) {
if (isRootFile || isRootInclude) {
result = sanitizeStageNames(result)
}

Expand Down
2 changes: 1 addition & 1 deletion src/instruction/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const loadDockerfile = require("../core/loadDockerfile")
const generateIncluded = require("../core/generateIncluded")

module.exports = ({
nestingLevel,
nestingLevel = 0,
dockerContext,
filePath,
relativeFilePath,
Expand Down
2 changes: 1 addition & 1 deletion src/instruction/fromParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const loadDockerfile = require("../core/loadDockerfile")
const generateIncluded = require("../core/generateIncluded")

module.exports = ({
nestingLevel,
nestingLevel = 0,
dockerContext,
filePath,
relativeFilePath,
Expand Down
3 changes: 2 additions & 1 deletion src/instruction/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const loadDockerfile = require("../core/loadDockerfile")
const generateIncluded = require("../core/generateIncluded")

module.exports = ({
nestingLevel,
nestingLevel = 0,
dockerContext,
filePath,
relativeFilePath,
Expand All @@ -31,6 +31,7 @@ module.exports = ({
parentStageTarget: null,
parentStageAlias: stageAlias,
nestingLevel: nestingLevel + 1,
isRootInclude: nestingLevel === 0,
})

const result = []
Expand Down
5 changes: 2 additions & 3 deletions test/__snapshots__/INCLUDE/include.expected.dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# DOCKERFILE-X:START file="inc/downloader.dockerfile" includedBy="include.dockerfile"
# DOCKERFILE-X:START file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile"
ARG UBUNTU_VERSION=22.04
FROM ubuntu:$UBUNTU_VERSION AS downlo550515--ubuntu9e4275--final-stage
FROM downlo550515--ubuntu9e4275--final-stage AS downlo550515--ubuntu9e4275
FROM downlo550515--ubuntu9e4275 AS downlo550515
FROM ubuntu:$UBUNTU_VERSION AS ubuntu9e4275--final-stage
FROM ubuntu9e4275--final-stage AS ubuntu9e4275
RUN groupadd -g 1000 ubuntu && useradd -rm -d /home/ubuntu -s /bin/bash -g ubuntu -G sudo -u 1000 ubuntu
ENV HOME=/home/ubuntu
RUN chmod 0777 /home/ubuntu
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/issues/include-target.expected.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://codeberg.org/devthefuture/dockerfile-x.
ARG JAVA_VERSION=17
# Even if no JDK is required for rtx, use an Ubuntu base image that incudes the JDK and which is also used by other
# stages in order to reduce the total number of base images used.
FROM eclipse-temurin:$JAVA_VERSION-jdk-jammy AS build
# See https://docs.docker.com/build/cache/#use-the-dedicated-run-cache
RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends ca-certificates curl gnupg && install -dm 755 /etc/apt/keyrings && curl https://rtx.jdx.dev/gpg-key.pub | gpg --dearmor > /etc/apt/keyrings/rtx-archive-keyring.gpg && echo "deb [signed-by=/etc/apt/keyrings/rtx-archive-keyring.gpg arch=amd64] https://rtx.jdx.dev/deb stable main" > /etc/apt/sources.list.d/rtx.list && apt-get update && apt-get install -y --no-install-recommends rtx
RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends build-essential git
# Ease debugging of rtx errors in derived images.
ENV RTX_VERBOSE=1
# Make the tool versions available to derived images.
COPY .tool-versions .
# DOCKERFILE-X:START file="inc/python.dockerfile" includedBy="issue1.dockerfile"
FROM build AS python-build
RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends libbz2-dev libffi-dev liblzma-dev libncurses-dev libreadline-dev libssl-dev libz-dev
RUN rtx install python
# DOCKERFILE-X:END file="inc/python.dockerfile" includedBy="issue1.dockerfile"
6 changes: 6 additions & 0 deletions test/fixs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ describe("dedup", () => {
await testFixture("dedup")
})
})

describe("issues", () => {
it("include-target", async () => {
await testFixture("issue1")
})
})
15 changes: 15 additions & 0 deletions test/fixtures/inc/python.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM build AS python-build

RUN --mount=type=cache,target=/var/cache/apt \
# Install tools required for building.
apt-get update && \
apt-get install -y --no-install-recommends \
libbz2-dev \
libffi-dev \
liblzma-dev \
libncurses-dev \
libreadline-dev \
libssl-dev \
libz-dev

RUN rtx install python
39 changes: 39 additions & 0 deletions test/fixtures/issue1.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# syntax=devthefuture/dockerfile-x
# See https://codeberg.org/devthefuture/dockerfile-x.

ARG JAVA_VERSION=17

# Even if no JDK is required for rtx, use an Ubuntu base image that incudes the JDK and which is also used by other
# stages in order to reduce the total number of base images used.
FROM eclipse-temurin:$JAVA_VERSION-jdk-jammy AS build

# See https://docs.docker.com/build/cache/#use-the-dedicated-run-cache
RUN --mount=type=cache,target=/var/cache/apt \
# Install tools required to install rtx.
apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates \
curl \
gnupg \
&& \
# Install rtx (see https://github.com/jdx/rtx#installation).
install -dm 755 /etc/apt/keyrings && \
curl https://rtx.jdx.dev/gpg-key.pub | gpg --dearmor > /etc/apt/keyrings/rtx-archive-keyring.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/rtx-archive-keyring.gpg arch=amd64] https://rtx.jdx.dev/deb stable main" > /etc/apt/sources.list.d/rtx.list && \
apt-get update && \
apt-get install -y --no-install-recommends rtx

RUN --mount=type=cache,target=/var/cache/apt \
# Install tools required to run rtx.
apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
git

# Ease debugging of rtx errors in derived images.
ENV RTX_VERBOSE=1

# Make the tool versions available to derived images.
COPY .tool-versions .

INCLUDE inc/python.dockerfile

0 comments on commit bee6f2a

Please sign in to comment.