Skip to content

Commit

Permalink
Merge pull request #846 from prathameshzarkar9/devcontainer-cli-add-l…
Browse files Browse the repository at this point in the history
…abel

#837 Devcontainer cli add label
  • Loading branch information
samruddhikhandale authored Jun 21, 2024
2 parents 58e0a47 + c82d704 commit 740a686
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/spec-node/containerFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export async function extendImage(params: DockerResolverParameters, config: Subs
'--target', featureBuildInfo.overrideTarget,
'-f', dockerfilePath,
...additionalImageNames.length > 0 ? additionalImageNames.map(name => ['-t', name]).flat() : ['-t', updatedImageName],
...params.additionalLabels.length > 0 ? params.additionalLabels.map(label => ['--label', label]).flat() : [],
emptyTempDir
);

Expand Down
2 changes: 2 additions & 0 deletions src/spec-node/devContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface ProvisionOptions {
omitLoggerHeader?: boolean | undefined;
buildxPlatform: string | undefined;
buildxPush: boolean;
additionalLabels: string[];
buildxOutput: string | undefined;
buildxCacheTo: string | undefined;
additionalFeatures?: Record<string, string | boolean | Record<string, string | boolean>>;
Expand Down Expand Up @@ -227,6 +228,7 @@ export async function createDockerParams(options: ProvisionOptions, disposables:
experimentalFrozenLockfile,
buildxPlatform: common.buildxPlatform,
buildxPush: common.buildxPush,
additionalLabels: options.additionalLabels,
buildxOutput: common.buildxOutput,
buildxCacheTo: common.buildxCacheTo,
platformInfo
Expand Down
10 changes: 10 additions & 0 deletions src/spec-node/devContainersSpecCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ async function provision({
useBuildKit: buildkit,
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: addCacheTo,
additionalFeatures,
Expand Down Expand Up @@ -431,6 +432,7 @@ async function doSetUp({
useBuildKit: 'auto',
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: undefined,
skipFeatureAutoMapping: false,
Expand Down Expand Up @@ -508,6 +510,7 @@ function buildOptions(y: Argv) {
'buildkit': { choices: ['auto' as 'auto', 'never' as 'never'], default: 'auto' as 'auto', description: 'Control whether BuildKit should be used' },
'platform': { type: 'string', description: 'Set target platforms.' },
'push': { type: 'boolean', default: false, description: 'Push to a container registry.' },
'label': { type: 'string', description: 'Provide key and value configuration that adds metadata to an image' },
'output': { type: 'string', description: 'Overrides the default behavior to load built images into the local docker registry. Valid options are the same ones provided to the --output option of docker buildx build.' },
'additional-features': { type: 'string', description: 'Additional features to apply to the dev container (JSON as per "features" section in devcontainer.json)' },
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
Expand Down Expand Up @@ -546,6 +549,7 @@ async function doBuild({
'buildkit': buildkit,
'platform': buildxPlatform,
'push': buildxPush,
'label': buildxLabel,
'output': buildxOutput,
'cache-to': buildxCacheTo,
'additional-features': additionalFeaturesJson,
Expand Down Expand Up @@ -593,6 +597,7 @@ async function doBuild({
useBuildKit: buildkit,
buildxPlatform,
buildxPush,
additionalLabels: [],
buildxOutput,
buildxCacheTo,
skipFeatureAutoMapping,
Expand Down Expand Up @@ -629,6 +634,9 @@ async function doBuild({
// Support multiple use of `--image-name`
const imageNames = (argImageName && (Array.isArray(argImageName) ? argImageName : [argImageName]) as string[]) || undefined;

// Support multiple use of `--label`
params.additionalLabels = (buildxLabel && (Array.isArray(buildxLabel) ? buildxLabel : [buildxLabel]) as string[]) || [];

if (isDockerFileConfig(config)) {

// Build the base image and extend with features etc.
Expand Down Expand Up @@ -858,6 +866,7 @@ async function doRunUserCommands({
useBuildKit: 'auto',
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxOutput: undefined,
buildxCacheTo: undefined,
skipFeatureAutoMapping,
Expand Down Expand Up @@ -1306,6 +1315,7 @@ export async function doExec({
omitLoggerHeader: true,
buildxPlatform: undefined,
buildxPush: false,
additionalLabels: [],
buildxCacheTo: undefined,
skipFeatureAutoMapping,
buildxOutput: undefined,
Expand Down
2 changes: 2 additions & 0 deletions src/spec-node/featuresCLI/testCommandImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ async function launchProject(params: DockerResolverParameters, workspaceFolder:
const options: ProvisionOptions = {
...staticProvisionParams,
workspaceFolder,
additionalLabels: [],
logLevel: common.getLogLevel(),
mountWorkspaceGitRoot: true,
remoteEnv: common.remoteEnv,
Expand Down Expand Up @@ -625,6 +626,7 @@ async function generateDockerParams(workspaceFolder: string, args: FeaturesTestC
const { logLevel, quiet, disposables } = args;
return await createDockerParams({
workspaceFolder,
additionalLabels: [],
dockerPath: undefined,
dockerComposePath: undefined,
containerDataFolder: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/spec-node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface DockerResolverParameters {
experimentalFrozenLockfile?: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
additionalLabels: string[];
buildxOutput: string | undefined;
buildxCacheTo: string | undefined;
platformInfo: PlatformInfo;
Expand Down
9 changes: 9 additions & 0 deletions src/test/cli.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('Dev Containers CLI', function () {

describe('Command build', () => {

it('should build successfully with valid image metadata --label property', async () => {
const testFolder = `${__dirname}/configs/example`;
const response = await shellExec(`${cli} build --workspace-folder ${testFolder} --label 'name=label-test' --label 'type=multiple-labels'`);
const res = JSON.parse(response.stdout);
assert.equal(res.outcome, 'success');
const labels = await shellExec(`docker inspect --format '{{json .Config.Labels}}' ${res.imageName} | jq`);
assert.match(labels.stdout.toString(), /\"name\": \"label-test\"/);
});

it('should fail to build with correct error message for local feature', async () => {
const testFolder = `${__dirname}/configs/image-with-local-feature`;
try {
Expand Down

0 comments on commit 740a686

Please sign in to comment.