Skip to content

Commit 00616d1

Browse files
committed
actually get compose path when native >.<
1 parent a5909b8 commit 00616d1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

app/triggers/providers/dockercompose/Dockercompose.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,50 @@ describe('Dockercompose Trigger', () => {
12271227
expect(result).toBeNull();
12281228
});
12291229

1230+
test('getComposeFileForContainer should use native compose labels with working dir', () => {
1231+
trigger.configuration.file = undefined;
1232+
const container = {
1233+
labels: {
1234+
'dd.compose.native': 'true',
1235+
'com.docker.compose.project.working_dir': '/opt/mautrix-whatsapp',
1236+
'com.docker.compose.project.config_files': 'compose.yml',
1237+
},
1238+
};
1239+
1240+
const result = trigger.getComposeFileForContainer(container);
1241+
1242+
expect(result).toBe('/opt/mautrix-whatsapp/compose.yml');
1243+
});
1244+
1245+
test('getComposeFileForContainer should use first native compose file when multiple config files are set', () => {
1246+
trigger.configuration.file = undefined;
1247+
const container = {
1248+
labels: {
1249+
'dd.compose.native': 'true',
1250+
'com.docker.compose.project.working_dir': '/opt/stack',
1251+
'com.docker.compose.project.config_files': 'compose.yml,compose.override.yml',
1252+
},
1253+
};
1254+
1255+
const result = trigger.getComposeFileForContainer(container);
1256+
1257+
expect(result).toBe('/opt/stack/compose.yml');
1258+
});
1259+
1260+
test('getComposeFileForContainer should ignore native compose labels when compose.native is not true', () => {
1261+
trigger.configuration.file = undefined;
1262+
const container = {
1263+
labels: {
1264+
'com.docker.compose.project.working_dir': '/opt/stack',
1265+
'com.docker.compose.project.config_files': 'compose.yml',
1266+
},
1267+
};
1268+
1269+
const result = trigger.getComposeFileForContainer(container);
1270+
1271+
expect(result).toBeNull();
1272+
});
1273+
12301274
test('getComposeFileForContainer should fall back to default config file', () => {
12311275
trigger.configuration.file = '/default/compose.yml';
12321276
const container = { labels: {} };

app/triggers/providers/dockercompose/Dockercompose.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
// @ts-nocheck
22
import fs from 'node:fs/promises';
3+
import path from 'node:path';
34
import yaml from 'yaml';
45
import { getState } from '../../../registry/index.js';
56
import { resolveConfiguredPath } from '../../../runtime/paths.js';
67
import Docker from '../docker/Docker.js';
78

9+
const COMPOSE_PROJECT_CONFIG_FILES_LABEL = 'com.docker.compose.project.config_files';
10+
const COMPOSE_PROJECT_WORKING_DIR_LABEL = 'com.docker.compose.project.working_dir';
11+
const DD_COMPOSE_NATIVE_LABEL = 'dd.compose.native';
12+
const WUD_COMPOSE_NATIVE_LABEL = 'wud.compose.native';
13+
14+
function isNativeComposeEnabled(labels = {}) {
15+
const nativeLabel = labels[DD_COMPOSE_NATIVE_LABEL] ?? labels[WUD_COMPOSE_NATIVE_LABEL];
16+
if (nativeLabel === undefined) {
17+
return false;
18+
}
19+
const normalizedNativeLabel = `${nativeLabel}`.trim().toLowerCase();
20+
return normalizedNativeLabel === 'true';
21+
}
22+
823
function splitDigestReference(image) {
924
if (!image) {
1025
return {
@@ -220,6 +235,36 @@ class Dockercompose extends Docker {
220235
}
221236
}
222237

238+
// Fall back to native Docker Compose labels when explicitly enabled
239+
const nativeComposeEnabled = isNativeComposeEnabled(container.labels);
240+
const composeConfigFiles = container.labels?.[COMPOSE_PROJECT_CONFIG_FILES_LABEL];
241+
if (nativeComposeEnabled && composeConfigFiles && composeConfigFiles.trim() !== '') {
242+
const composeProjectWorkingDir = container.labels?.[COMPOSE_PROJECT_WORKING_DIR_LABEL];
243+
const configFiles = composeConfigFiles
244+
.split(',')
245+
.map((configFile) => configFile.trim())
246+
.filter((configFile) => configFile !== '');
247+
248+
const configFile = configFiles[0];
249+
if (configFile) {
250+
const resolvedConfigFile =
251+
path.isAbsolute(configFile) || !composeProjectWorkingDir
252+
? configFile
253+
: path.join(composeProjectWorkingDir.trim(), configFile);
254+
255+
try {
256+
return resolveConfiguredPath(resolvedConfigFile, {
257+
label: `Native compose file labels ${COMPOSE_PROJECT_CONFIG_FILES_LABEL}/${COMPOSE_PROJECT_WORKING_DIR_LABEL}`,
258+
});
259+
} catch (e) {
260+
this.log.warn(
261+
`Native compose file labels on container ${container.name} are invalid (${e.message})`,
262+
);
263+
return null;
264+
}
265+
}
266+
}
267+
223268
// Fall back to default configuration file
224269
if (!this.configuration.file) {
225270
return null;

0 commit comments

Comments
 (0)