synclounge/.github/getDockerPlatforms.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

const { readFileSync } = require('fs');
const { execSync } = require('child_process');
const findLastFromLineIndex = (lines) => lines.reduce((acc, line, index) => (
line.startsWith('FROM') ? index : acc
), 0);
const getDockerfileImage = (file) => {
const lines = readFileSync(file, { encoding: 'utf8' })
.split('\n');
const lastFromLine = lines[findLastFromLineIndex(lines)];
return lastFromLine.split(' ')[1];
};
const getImageManifest = (image) => execSync(`docker manifest inspect ${image}`, {
encoding: 'utf8',
});
const translateManifestPlatform = ({ os, architecture, variant }) => (variant
? `${os}/${architecture}/${variant}`
: `${os}/${architecture}`);
const getImagePlatforms = (image) => JSON.parse(getImageManifest(image)).manifests
.map(({ platform }) => translateManifestPlatform(platform));
const getDockerfileArches = (file, supportedPlatformsStr) => {
const basePlatforms = getImagePlatforms(getDockerfileImage(file));
2020-09-28 07:28:26 +00:00
console.log('Base platforms: ', basePlatforms.join(', '));
const supportedPlatforms = supportedPlatformsStr.split(',');
return basePlatforms.filter((platform) => supportedPlatforms.includes(platform)).join(',');;
}
module.exports = getDockerfileArches;