Skip to content

Commit

Permalink
feat(nx-docker): add labels options to docker build executor
Browse files Browse the repository at this point in the history
  • Loading branch information
johnitvn committed Nov 24, 2024
1 parent f08f1eb commit 7a4ebb8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/nx-docker/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe('executor', () => {
context: undefined,
tags: ['latest'],
args: ['ARG1=value1'],
ci: false,
outputs: ['image'],
flatforms: [],
};
Expand Down Expand Up @@ -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,
});
});


});
6 changes: 6 additions & 0 deletions packages/nx-docker/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ const executor: PromiseExecutor<DockerExecutorSchema> = 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 = [
Expand All @@ -96,6 +101,7 @@ const executor: PromiseExecutor<DockerExecutorSchema> = async (options, context)
...uLimitArgs,
...tagArgs,
...targetArgs,
...labelsArgs,
'-f',
dockerfilePath,
contextPath,
Expand Down
1 change: 1 addition & 0 deletions packages/nx-docker/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface DockerExecutorSchema {
ulimit?: string[];
metadataFile?: string;
flatforms: string[];
labels?: { [key: string]: string };
}
7 changes: 7 additions & 0 deletions packages/nx-docker/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 7a4ebb8

Please sign in to comment.