Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Merge metadata logic for containerEnv - devcontainer build #392

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/spec-node/containerFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async function getFeaturesBuildOptions(params: DockerResolverParameters, devCont
// When copying via buildkit, the content is accessed via '.' (i.e. in the context root)
// When copying via temp image, the content is in '/tmp/build-features'
const contentSourceRootPath = useBuildKitBuildContexts ? '.' : '/tmp/build-features/';
const dockerfile = getContainerFeaturesBaseDockerFile()
let dockerfile = getContainerFeaturesBaseDockerFile()
.replace('#{nonBuildKitFeatureContentFallback}', useBuildKitBuildContexts ? '' : `FROM ${buildContentImageName} as dev_containers_feature_content_source`)
.replace('{contentSourceRootPath}', contentSourceRootPath)
.replace('#{featureBuildStages}', getFeatureBuildStages(featuresConfig, buildStageScripts, contentSourceRootPath))
Expand All @@ -294,6 +294,14 @@ async function getFeaturesBuildOptions(params: DockerResolverParameters, devCont
.replace('#{copyFeatureBuildStages}', getCopyFeatureBuildStages(featuresConfig, buildStageScripts))
.replace('#{devcontainerMetadata}', getDevcontainerMetadataLabel(imageMetadata, common.experimentalImageMetadata))
;

const rawContainerEnv = imageMetadata.raw.find(entry => entry.containerEnv)?.containerEnv;
samruddhikhandale marked this conversation as resolved.
Show resolved Hide resolved
if (rawContainerEnv !== undefined) {
let keys = Object.keys(rawContainerEnv);
const concatenatedEnv = keys.map(k => `ENV ${k}=${rawContainerEnv![k]}`).join('\n');
dockerfile += concatenatedEnv;
samruddhikhandale marked this conversation as resolved.
Show resolved Hide resolved
}

const syntax = imageBuildInfo.dockerfile?.preamble.directives.syntax;
const dockerfilePrefixContent = `${useBuildKitBuildContexts && !(imageBuildInfo.dockerfile && supportsBuildContexts(imageBuildInfo.dockerfile)) ?
'# syntax=docker/dockerfile:1.4' :
Expand Down
15 changes: 15 additions & 0 deletions src/test/cli.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,20 @@ describe('Dev Containers CLI', function () {
await shellExec(`docker buildx rm ${builderName}`);
}
});

it('should follow the correct merge logic for containerEnv', async () => {
const res = await shellExec(`${cli} build --workspace-folder ${__dirname}/configs/image-metadata-containerEnv --image-name "test-metadata"`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');

const resRun = await shellExec(`docker run -it -d "test-metadata"`);
const containerId: string = resRun.stdout.split('\n')[0];
assert.ok(containerId, 'Container id not found.');

const result = await shellExec(`docker exec ${containerId} bash -c 'echo $JAVA_HOME'`);
assert.equal('/usr/lib/jvm/msopenjdk-current\n', result.stdout);

await shellExec(`docker rm -f ${containerId}`);
});
});
});
13 changes: 13 additions & 0 deletions src/test/cli.up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,18 @@ describe('Dev Containers CLI', function () {
});
});
});

it('should follow the correct merge logic for containerEnv', async () => {
const res = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/image-metadata-containerEnv`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const containerId: string = response.containerId;
assert.ok(containerId, 'Container id not found.');

const result = await shellExec(`docker exec ${containerId} bash -c 'echo $JAVA_HOME'`);
assert.equal('/usr/lib/jvm/msopenjdk-current\n', result.stdout);

await shellExec(`docker rm -f ${containerId}`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none"
}
},
"containerEnv": {
"JAVA_HOME": "/usr/lib/jvm/msopenjdk-current"
}
}