Skip to content

Commit 740a686

Browse files
Merge pull request #846 from prathameshzarkar9/devcontainer-cli-add-label
#837 Devcontainer cli add label
2 parents 58e0a47 + c82d704 commit 740a686

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

src/spec-node/containerFeatures.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export async function extendImage(params: DockerResolverParameters, config: Subs
113113
'--target', featureBuildInfo.overrideTarget,
114114
'-f', dockerfilePath,
115115
...additionalImageNames.length > 0 ? additionalImageNames.map(name => ['-t', name]).flat() : ['-t', updatedImageName],
116+
...params.additionalLabels.length > 0 ? params.additionalLabels.map(label => ['--label', label]).flat() : [],
116117
emptyTempDir
117118
);
118119

src/spec-node/devContainers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface ProvisionOptions {
5252
omitLoggerHeader?: boolean | undefined;
5353
buildxPlatform: string | undefined;
5454
buildxPush: boolean;
55+
additionalLabels: string[];
5556
buildxOutput: string | undefined;
5657
buildxCacheTo: string | undefined;
5758
additionalFeatures?: Record<string, string | boolean | Record<string, string | boolean>>;
@@ -227,6 +228,7 @@ export async function createDockerParams(options: ProvisionOptions, disposables:
227228
experimentalFrozenLockfile,
228229
buildxPlatform: common.buildxPlatform,
229230
buildxPush: common.buildxPush,
231+
additionalLabels: options.additionalLabels,
230232
buildxOutput: common.buildxOutput,
231233
buildxCacheTo: common.buildxCacheTo,
232234
platformInfo

src/spec-node/devContainersSpecCLI.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ async function provision({
267267
useBuildKit: buildkit,
268268
buildxPlatform: undefined,
269269
buildxPush: false,
270+
additionalLabels: [],
270271
buildxOutput: undefined,
271272
buildxCacheTo: addCacheTo,
272273
additionalFeatures,
@@ -431,6 +432,7 @@ async function doSetUp({
431432
useBuildKit: 'auto',
432433
buildxPlatform: undefined,
433434
buildxPush: false,
435+
additionalLabels: [],
434436
buildxOutput: undefined,
435437
buildxCacheTo: undefined,
436438
skipFeatureAutoMapping: false,
@@ -508,6 +510,7 @@ function buildOptions(y: Argv) {
508510
'buildkit': { choices: ['auto' as 'auto', 'never' as 'never'], default: 'auto' as 'auto', description: 'Control whether BuildKit should be used' },
509511
'platform': { type: 'string', description: 'Set target platforms.' },
510512
'push': { type: 'boolean', default: false, description: 'Push to a container registry.' },
513+
'label': { type: 'string', description: 'Provide key and value configuration that adds metadata to an image' },
511514
'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.' },
512515
'additional-features': { type: 'string', description: 'Additional features to apply to the dev container (JSON as per "features" section in devcontainer.json)' },
513516
'skip-feature-auto-mapping': { type: 'boolean', default: false, hidden: true, description: 'Temporary option for testing.' },
@@ -546,6 +549,7 @@ async function doBuild({
546549
'buildkit': buildkit,
547550
'platform': buildxPlatform,
548551
'push': buildxPush,
552+
'label': buildxLabel,
549553
'output': buildxOutput,
550554
'cache-to': buildxCacheTo,
551555
'additional-features': additionalFeaturesJson,
@@ -593,6 +597,7 @@ async function doBuild({
593597
useBuildKit: buildkit,
594598
buildxPlatform,
595599
buildxPush,
600+
additionalLabels: [],
596601
buildxOutput,
597602
buildxCacheTo,
598603
skipFeatureAutoMapping,
@@ -629,6 +634,9 @@ async function doBuild({
629634
// Support multiple use of `--image-name`
630635
const imageNames = (argImageName && (Array.isArray(argImageName) ? argImageName : [argImageName]) as string[]) || undefined;
631636

637+
// Support multiple use of `--label`
638+
params.additionalLabels = (buildxLabel && (Array.isArray(buildxLabel) ? buildxLabel : [buildxLabel]) as string[]) || [];
639+
632640
if (isDockerFileConfig(config)) {
633641

634642
// Build the base image and extend with features etc.
@@ -858,6 +866,7 @@ async function doRunUserCommands({
858866
useBuildKit: 'auto',
859867
buildxPlatform: undefined,
860868
buildxPush: false,
869+
additionalLabels: [],
861870
buildxOutput: undefined,
862871
buildxCacheTo: undefined,
863872
skipFeatureAutoMapping,
@@ -1306,6 +1315,7 @@ export async function doExec({
13061315
omitLoggerHeader: true,
13071316
buildxPlatform: undefined,
13081317
buildxPush: false,
1318+
additionalLabels: [],
13091319
buildxCacheTo: undefined,
13101320
skipFeatureAutoMapping,
13111321
buildxOutput: undefined,

src/spec-node/featuresCLI/testCommandImpl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ async function launchProject(params: DockerResolverParameters, workspaceFolder:
550550
const options: ProvisionOptions = {
551551
...staticProvisionParams,
552552
workspaceFolder,
553+
additionalLabels: [],
553554
logLevel: common.getLogLevel(),
554555
mountWorkspaceGitRoot: true,
555556
remoteEnv: common.remoteEnv,
@@ -625,6 +626,7 @@ async function generateDockerParams(workspaceFolder: string, args: FeaturesTestC
625626
const { logLevel, quiet, disposables } = args;
626627
return await createDockerParams({
627628
workspaceFolder,
629+
additionalLabels: [],
628630
dockerPath: undefined,
629631
dockerComposePath: undefined,
630632
containerDataFolder: undefined,

src/spec-node/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export interface DockerResolverParameters {
116116
experimentalFrozenLockfile?: boolean;
117117
buildxPlatform: string | undefined;
118118
buildxPush: boolean;
119+
additionalLabels: string[];
119120
buildxOutput: string | undefined;
120121
buildxCacheTo: string | undefined;
121122
platformInfo: PlatformInfo;

src/test/cli.build.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ describe('Dev Containers CLI', function () {
2727

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

30+
it('should build successfully with valid image metadata --label property', async () => {
31+
const testFolder = `${__dirname}/configs/example`;
32+
const response = await shellExec(`${cli} build --workspace-folder ${testFolder} --label 'name=label-test' --label 'type=multiple-labels'`);
33+
const res = JSON.parse(response.stdout);
34+
assert.equal(res.outcome, 'success');
35+
const labels = await shellExec(`docker inspect --format '{{json .Config.Labels}}' ${res.imageName} | jq`);
36+
assert.match(labels.stdout.toString(), /\"name\": \"label-test\"/);
37+
});
38+
3039
it('should fail to build with correct error message for local feature', async () => {
3140
const testFolder = `${__dirname}/configs/image-with-local-feature`;
3241
try {

0 commit comments

Comments
 (0)