diff --git a/packages/nx-docker/src/executors/build/executor.spec.ts b/packages/nx-docker/src/executors/build/executor.spec.ts index fa039e4..8e0d9bb 100644 --- a/packages/nx-docker/src/executors/build/executor.spec.ts +++ b/packages/nx-docker/src/executors/build/executor.spec.ts @@ -47,7 +47,6 @@ describe('executor', () => { context: undefined, tags: ['latest'], args: ['ARG1=value1'], - ci: false, outputs: ['image'], flatforms: [], }; @@ -229,4 +228,25 @@ describe('executor', () => { { stdio: 'inherit', cwd: context.root } ); }); + + it('should build Docker image with labels arguments when labels are provided', async () => { + options.labels = { 'com.example.label': 'label-value' }; + await executor(options, context); + const expectedTagArgs = ['--label', 'com.example.label="label-value"']; + expect(execFileSync).toHaveBeenCalledWith('docker', expect.arrayContaining(expectedTagArgs), { + stdio: 'inherit', + cwd: context.root, + }); + }); + + it('should build Docker image without labels arguments when labels are not provided', async () => { + options.labels = undefined; + await executor(options, context); + expect(execFileSync).toHaveBeenCalledWith('docker', expect.not.arrayContaining(['--label']), { + stdio: 'inherit', + cwd: context.root, + }); + }); + + }); diff --git a/packages/nx-docker/src/executors/build/executor.ts b/packages/nx-docker/src/executors/build/executor.ts index 1034813..afab4fe 100644 --- a/packages/nx-docker/src/executors/build/executor.ts +++ b/packages/nx-docker/src/executors/build/executor.ts @@ -78,6 +78,11 @@ const executor: PromiseExecutor = async (options, context) const shmSizeArgs = options.shmSize ? ['--shm-size', options.shmSize] : []; const uLimitArgs = options.ulimit ? [`--ulimit${options.ulimit}`] : []; const targetArgs = options.target ? ['--target', options.target] : []; + const labelsArgs = options.labels + ? Object.entries(options.labels) + .map(([key, value]) => ['--label', `${key}="${value}"`]) + .flat() + : []; try { const command = [ @@ -96,6 +101,7 @@ const executor: PromiseExecutor = async (options, context) ...uLimitArgs, ...tagArgs, ...targetArgs, + ...labelsArgs, '-f', dockerfilePath, contextPath, diff --git a/packages/nx-docker/src/executors/build/schema.d.ts b/packages/nx-docker/src/executors/build/schema.d.ts index 368591c..d6ab84a 100644 --- a/packages/nx-docker/src/executors/build/schema.d.ts +++ b/packages/nx-docker/src/executors/build/schema.d.ts @@ -15,4 +15,5 @@ export interface DockerExecutorSchema { ulimit?: string[]; metadataFile?: string; flatforms: string[]; + labels?: { [key: string]: string }; } diff --git a/packages/nx-docker/src/executors/build/schema.json b/packages/nx-docker/src/executors/build/schema.json index b72f467..f127710 100644 --- a/packages/nx-docker/src/executors/build/schema.json +++ b/packages/nx-docker/src/executors/build/schema.json @@ -97,6 +97,13 @@ "flatforms": { "type": "string", "description": "Set the target platform" + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "description": "The list of labels to add to the image" } }, "required": ["tags"]