Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions app/triggers/providers/dockercompose/Dockercompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,26 @@ class Dockercompose extends Docker {
return;
}

// [{ current: '1.0.0', update: '2.0.0' }, {...}]
const currentVersionToUpdateVersionArray = containersFiltered
.map((container) =>
this.mapCurrentVersionToUpdateVersion(compose, container),
)
.filter((map) => map !== undefined);

// Dry-run?
// Build an array with container + version mapping in one pass
// [{ container, current, update }]
const versionMappings = containersFiltered
.map((container) => {
const map = this.mapCurrentVersionToUpdateVersion(compose, container);
if (!map) return undefined;
return { container, ...map }; // { container, current, update }
})
.filter((entry) => entry !== undefined);

// Split into need-update and no-op updates
const mappingsNeedingUpdate = versionMappings.filter(
({ current, update }) => current !== update
);

// Nothing to update?
if (mappingsNeedingUpdate.length === 0) {
this.log.info(`All containers in ${composeFile} are already up to date`);
}

if (this.configuration.dryrun) {
this.log.info(
`Do not replace existing docker-compose file ${composeFile} (dry-run mode enabled)`,
Expand All @@ -182,22 +194,19 @@ class Dockercompose extends Docker {

// Read the compose file as a string
let composeFileStr = (await this.getComposeFile(composeFile)).toString();

// Apply only changes that actually update a version
mappingsNeedingUpdate.forEach(({ current, update }) => {
composeFileStr = composeFileStr.replaceAll(current, update);
});

// Replace all versions
currentVersionToUpdateVersionArray.forEach(
({ current, update }) => {
composeFileStr = composeFileStr.replaceAll(current, update);
},
);

// Write docker-compose.yml file back
// Write compose file back
await this.writeComposeFile(composeFile, composeFileStr);
}

// Update all containers
// (super.notify will take care of the dry-run mode for each container as well)
// Trigger only containers that actually need updates
await Promise.all(
containersFiltered.map((container) => super.trigger(container)),
mappingsNeedingUpdate.map(({ container }) => super.trigger(container)),
);
}

Expand Down