Skip to content

Commit 17cd7eb

Browse files
committed
feat(nx-docker): add labels options to docker build executor
1 parent a99d80f commit 17cd7eb

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

packages/nx-docker/src/executors/build/executor.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ describe('executor', () => {
4747
context: undefined,
4848
tags: ['latest'],
4949
args: ['ARG1=value1'],
50-
ci: false,
5150
outputs: ['image'],
5251
flatforms: [],
5352
};
@@ -229,4 +228,25 @@ describe('executor', () => {
229228
{ stdio: 'inherit', cwd: context.root }
230229
);
231230
});
231+
232+
it('should build Docker image with labels arguments when labels are provided', async () => {
233+
options.labels = { 'com.example.label': 'label-value' };
234+
await executor(options, context);
235+
const expectedTagArgs = ['--label', 'com.example.label="label-value"'];
236+
expect(execFileSync).toHaveBeenCalledWith('docker', expect.arrayContaining(expectedTagArgs), {
237+
stdio: 'inherit',
238+
cwd: context.root,
239+
});
240+
});
241+
242+
it('should build Docker image without labels arguments when labels are not provided', async () => {
243+
options.labels = undefined;
244+
await executor(options, context);
245+
expect(execFileSync).toHaveBeenCalledWith('docker', expect.not.arrayContaining(['--label']), {
246+
stdio: 'inherit',
247+
cwd: context.root,
248+
});
249+
});
250+
251+
232252
});

packages/nx-docker/src/executors/build/executor.ts

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const executor: PromiseExecutor<DockerExecutorSchema> = async (options, context)
7878
const shmSizeArgs = options.shmSize ? ['--shm-size', options.shmSize] : [];
7979
const uLimitArgs = options.ulimit ? [`--ulimit${options.ulimit}`] : [];
8080
const targetArgs = options.target ? ['--target', options.target] : [];
81+
const labelsArgs = options.labels
82+
? Object.entries(options.labels)
83+
.map(([key, value]) => ['--label', `${key}="${value}"`])
84+
.flat()
85+
: [];
8186

8287
try {
8388
const command = [
@@ -96,6 +101,7 @@ const executor: PromiseExecutor<DockerExecutorSchema> = async (options, context)
96101
...uLimitArgs,
97102
...tagArgs,
98103
...targetArgs,
104+
...labelsArgs,
99105
'-f',
100106
dockerfilePath,
101107
contextPath,

packages/nx-docker/src/executors/build/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export interface DockerExecutorSchema {
1515
ulimit?: string[];
1616
metadataFile?: string;
1717
flatforms: string[];
18+
labels?: { [key: string]: string };
1819
}

packages/nx-docker/src/executors/build/schema.json

+7
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@
9797
"flatforms": {
9898
"type": "string",
9999
"description": "Set the target platform"
100+
},
101+
"labels": {
102+
"type": "object",
103+
"additionalProperties": {
104+
"type": "string"
105+
},
106+
"description": "The list of labels to add to the image"
100107
}
101108
},
102109
"required": ["tags"]

0 commit comments

Comments
 (0)