Skip to content

Commit

Permalink
feat(#8841): Support multiplatform images
Browse files Browse the repository at this point in the history
feat(#8841): Support multiplatform images
  • Loading branch information
1yuv authored Mar 15, 2024
2 parents 831bd6e + 23d1d39 commit 448c700
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ jobs:
- run: npm ci
- name: Compile
run: npm run ci-compile
- name: Setup QEMU
if: ${{ env.INTERNAL_CONTRIBUTOR }}
uses: docker/setup-qemu-action@v3
- name: Setup Buildx
if: ${{ env.INTERNAL_CONTRIBUTOR }}
uses: docker/setup-buildx-action@v3
- name: Publish for testing
run: npm run publish-for-testing
- name: Upload docker images as artifacts
Expand Down Expand Up @@ -275,6 +281,10 @@ jobs:
if: ${{ github.event_name != 'pull_request' }}

steps:
- name: Install regctl
if: ${{ env.INTERNAL_CONTRIBUTOR }}
uses: regclient/actions/regctl-installer@main

- name: Configure AWS credentials
if: ${{ env.INTERNAL_CONTRIBUTOR }}
uses: aws-actions/configure-aws-credentials@v4
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/build-service-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ uglifyjs api/build/static/login/script.js -o api/build/static/login/script.js
uglifyjs api/build/static/login/lib-bowser.js -o api/build/static/login/lib-bowser.js
./scripts/build/cleancss-api.sh
node scripts/build/cli buildServiceImages
node scripts/build/cli buildImages
node scripts/build/cli buildInfrastructureImages

echo "build-service-images: done"
71 changes: 50 additions & 21 deletions scripts/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mustache = require('mustache');

const packageJson = require('../../package.json');
const versions = require('./versions');
const BUILD_PLATFORMS = ['linux/amd64', 'linux/arm64/v8'];

const {
TAG,
Expand Down Expand Up @@ -66,7 +67,7 @@ const setBuildInfo = () => {
};

const mkdirSync = (dirPath) => {
if (!fs.existsSync(dirPath)){
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
};
Expand Down Expand Up @@ -147,10 +148,11 @@ const localDockerComposeFiles = () => {
};

const saveServiceTags = () => {
const tags = [...versions.SERVICES, ...versions.INFRASTRUCTURE].map(service => ({
container_name: `cht-${service}`,
image: versions.getImageTag(service, true),
}));
const tags = [...versions.SERVICES, ...versions.INFRASTRUCTURE]
.map(service => ({
container_name: `cht-${service}`,
image: versions.getImageTag(service, true),
}));
const tagsFilePath = path.resolve(stagingPath, 'tags.json');
fs.writeFileSync(tagsFilePath, JSON.stringify(tags));
};
Expand Down Expand Up @@ -190,7 +192,7 @@ const setDdocsVersion = () => {
});
};

const exec = async (command, args, options=({})) => {
const exec = async (command, args, options = ({})) => {
options.stdio = 'inherit';
const ci = spawn(command, args, options);
await new Promise((resolve, reject) => {
Expand All @@ -210,7 +212,9 @@ const npmCiModules = async () => {
}
};

const buildServiceImages = async () => {


const buildSinglePlatformServiceImages = async () => {
for (const service of versions.SERVICES) {
console.log(`\n\nBuilding docker image for ${service}\n\n`);
const tag = versions.getImageTag(service);
Expand All @@ -220,39 +224,65 @@ const buildServiceImages = async () => {
}
};

const buildImages = async () => {
const buildMultiPlatformServiceImages = async () => {
for (const service of versions.SERVICES) {
console.log(`\n\nBuilding and pushing multiplatform docker image for ${service}\n\n`);
const tag = versions.getImageTag(service);
await exec('npm', ['ci', '--omit=dev'], { cwd: service });
await exec('npm', ['dedupe'], { cwd: service });
await exec('docker', ['buildx', 'build', '--provenance=false', '--platform=' + BUILD_PLATFORMS.join(','),
'-f', `./${service}/Dockerfile`, '--tag', tag, '--push', '.']);
}
};

const buildServiceImages = async () => {
if (INTERNAL_CONTRIBUTOR) {
await buildMultiPlatformServiceImages();
} else {
await buildSinglePlatformServiceImages();
}
};

const buildSinglePlatformImages = async () => {
for (const service of versions.INFRASTRUCTURE) {
console.log(`\n\nBuilding docker image for ${service}\n\n`);
const tag = versions.getImageTag(service);
await exec('docker', ['build', '-f', `./Dockerfile`, '--tag', tag, '.'], { cwd: service });
}
};

const saveServiceImages = async () => {
for (const service of [...versions.SERVICES, ...versions.INFRASTRUCTURE]) {
console.log(`\n\nSaving docker image for ${service}\n\n`);
const buildInfrastructureImages = async () => {
if (INTERNAL_CONTRIBUTOR) {
await buildMultiPlatformImages();
} else {
await buildSinglePlatformImages();
}
};

const buildMultiPlatformImages = async () => {
for (const service of versions.INFRASTRUCTURE) {
console.log(`\n\nBuilding and pushing multiplatform docker image for ${service}\n\n`);
const tag = versions.getImageTag(service);
await exec('docker', ['save', tag, '-o', `images/${service}.tar`]);
await exec('docker', ['buildx', 'build', '--provenance=false', '--platform=' + BUILD_PLATFORMS.join(','),
'-f', `./Dockerfile`, '--tag', tag, '--push', '.'], { cwd: service });
}
};

const pushServiceImages = async () => {
const saveServiceImages = async () => {
for (const service of [...versions.SERVICES, ...versions.INFRASTRUCTURE]) {
console.log(`\n\nPushing docker image for ${service}\n\n`);
console.log(`\n\nSaving docker image for ${service}\n\n`);
const tag = versions.getImageTag(service);
await exec('docker', ['push', tag]);
await exec('docker', ['save', tag, '-o', `images/${service}.tar`]);
}
};

const publishServiceImages = async () => {
if (!BUILD_NUMBER) {
return;
}

if (INTERNAL_CONTRIBUTOR) {
return await pushServiceImages();
if (!INTERNAL_CONTRIBUTOR){
return await saveServiceImages();
}
return await saveServiceImages();
};

module.exports = {
Expand All @@ -264,8 +294,7 @@ module.exports = {
setDdocsVersion,
updateServiceWorker,
buildServiceImages,
buildImages,
buildInfrastructureImages,
saveServiceImages,
pushServiceImages,
publishServiceImages
};
16 changes: 7 additions & 9 deletions scripts/ci/tag-docker-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const { spawn } = require('child_process');

const buildVersions = require('../build/versions');

const dockerCmd = (...params) => new Promise((resolve, reject) => {
console.log('docker', ...params);
const proc = spawn('docker', params);
const regctlCmd = (...params) => new Promise((resolve, reject) => {
console.log('regctl', ...params);
const proc = spawn('regctl', params);
proc.on('error', (err) => {
console.error('Error while running docker command', err);
console.error('Error while running regctl command', err);
reject(err);
});
const log = data => console.log(data.toString());
Expand All @@ -17,12 +17,10 @@ const dockerCmd = (...params) => new Promise((resolve, reject) => {
});

(async () => {
for (const service of [...buildVersions.SERVICES, ...buildVersions.INFRASTRUCTURE]) {
for (const service of
[...buildVersions.SERVICES, ...buildVersions.INFRASTRUCTURE]) {
const existentTag = buildVersions.getImageTag(service);
const releaseTag = buildVersions.getImageTag(service, true);

await dockerCmd('pull', existentTag);
await dockerCmd('image', 'tag', existentTag, releaseTag);
await dockerCmd('push', releaseTag);
await regctlCmd('image', 'copy', existentTag, releaseTag);
}
})();

0 comments on commit 448c700

Please sign in to comment.