2023-01-31 03:34:51 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright 2023 actions-toolkit authors
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-17 11:53:57 +01:00
|
|
|
import * as core from '@actions/core';
|
2023-01-23 10:07:14 +01:00
|
|
|
import * as semver from 'semver';
|
2023-01-17 11:53:57 +01:00
|
|
|
|
2023-02-01 11:47:01 +01:00
|
|
|
import {Buildx} from '../buildx/buildx';
|
|
|
|
|
import {Builder} from '../buildx/builder';
|
2024-07-16 17:15:12 +02:00
|
|
|
import {Docker} from '../docker/docker';
|
2023-02-01 11:47:01 +01:00
|
|
|
import {Config} from './config';
|
2023-01-31 21:13:39 +01:00
|
|
|
|
2024-05-15 17:08:37 +02:00
|
|
|
import {BuilderInfo, NodeInfo} from '../types/buildx/builder';
|
2023-01-17 11:53:57 +01:00
|
|
|
|
2023-01-23 10:07:14 +01:00
|
|
|
export interface BuildKitOpts {
|
|
|
|
|
buildx?: Buildx;
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-23 10:07:14 +01:00
|
|
|
export class BuildKit {
|
2023-01-30 00:08:45 +01:00
|
|
|
private readonly buildx: Buildx;
|
2023-01-23 10:07:14 +01:00
|
|
|
|
2023-02-01 11:47:01 +01:00
|
|
|
public readonly config: Config;
|
|
|
|
|
|
2023-02-20 07:19:57 +01:00
|
|
|
constructor(opts?: BuildKitOpts) {
|
|
|
|
|
this.config = new Config();
|
|
|
|
|
this.buildx = opts?.buildx || new Buildx();
|
2023-01-23 15:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-18 07:31:21 +01:00
|
|
|
public async getVersion(node: NodeInfo): Promise<string | undefined> {
|
2023-02-24 19:41:10 +01:00
|
|
|
if (!node.buildkit && node.name) {
|
2023-01-23 10:07:14 +01:00
|
|
|
try {
|
|
|
|
|
return await this.getVersionWithinImage(node.name);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
core.warning(e);
|
|
|
|
|
}
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|
2023-02-24 19:41:10 +01:00
|
|
|
return node.buildkit;
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-23 10:07:14 +01:00
|
|
|
private async getVersionWithinImage(nodeName: string): Promise<string> {
|
2023-02-18 10:09:41 +01:00
|
|
|
core.debug(`BuildKit.getVersionWithinImage nodeName: ${nodeName}`);
|
2024-07-16 17:15:12 +02:00
|
|
|
return Docker.getExecOutput(['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], {
|
2023-02-20 09:01:01 +01:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
|
silent: true
|
|
|
|
|
}).then(bkitimage => {
|
|
|
|
|
if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) {
|
|
|
|
|
core.debug(`BuildKit.getVersionWithinImage image: ${bkitimage.stdout.trim()}`);
|
2024-07-16 17:15:12 +02:00
|
|
|
return Docker.getExecOutput(['run', '--rm', bkitimage.stdout.trim(), '--version'], {
|
2023-02-20 09:01:01 +01:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
|
silent: true
|
|
|
|
|
}).then(bkitversion => {
|
|
|
|
|
if (bkitversion.exitCode == 0 && bkitversion.stdout.length > 0) {
|
|
|
|
|
return `${bkitimage.stdout.trim()} => ${bkitversion.stdout.trim()}`;
|
|
|
|
|
} else if (bkitversion.stderr.length > 0) {
|
|
|
|
|
throw new Error(bkitimage.stderr.trim());
|
|
|
|
|
}
|
|
|
|
|
return bkitversion.stdout.trim();
|
|
|
|
|
});
|
|
|
|
|
} else if (bkitimage.stderr.length > 0) {
|
|
|
|
|
throw new Error(bkitimage.stderr.trim());
|
|
|
|
|
}
|
|
|
|
|
return bkitimage.stdout.trim();
|
|
|
|
|
});
|
2023-01-23 10:07:14 +01:00
|
|
|
}
|
2023-01-17 11:53:57 +01:00
|
|
|
|
2023-02-18 07:31:21 +01:00
|
|
|
public async versionSatisfies(builderName: string, range: string, builderInfo?: BuilderInfo): Promise<boolean> {
|
|
|
|
|
if (!builderInfo) {
|
2023-02-20 07:19:57 +01:00
|
|
|
builderInfo = await new Builder({buildx: this.buildx}).inspect(builderName);
|
2023-02-18 07:31:21 +01:00
|
|
|
}
|
2023-01-23 10:07:14 +01:00
|
|
|
for (const node of builderInfo.nodes) {
|
2023-02-24 19:41:10 +01:00
|
|
|
let bkversion = node.buildkit;
|
2023-02-20 10:00:39 +01:00
|
|
|
core.debug(`BuildKit.versionSatisfies ${bkversion}: ${range}`);
|
2023-01-23 10:07:14 +01:00
|
|
|
if (!bkversion) {
|
|
|
|
|
try {
|
|
|
|
|
bkversion = await this.getVersionWithinImage(node.name || '');
|
2025-12-15 22:55:44 +01:00
|
|
|
} catch {
|
2023-02-20 10:00:39 +01:00
|
|
|
core.debug(`BuildKit.versionSatisfies ${node.name}: can't get version`);
|
2023-01-23 10:07:14 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-20 10:00:39 +01:00
|
|
|
core.debug(`BuildKit.versionSatisfies ${node.name}: version ${bkversion}`);
|
2023-01-23 10:07:14 +01:00
|
|
|
// BuildKit version reported by moby is in the format of `v0.11.0-moby`
|
|
|
|
|
if (builderInfo.driver == 'docker' && !bkversion.endsWith('-moby')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!semver.satisfies(bkversion.replace(/-moby$/, ''), range)) {
|
2023-01-17 11:53:57 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-23 10:07:14 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|