2022-04-25 06:47:48 +02:00
|
|
|
import {parse} from 'csv-parse/sync';
|
2020-08-17 22:18:15 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-08-23 03:31:38 +02:00
|
|
|
import * as semver from 'semver';
|
2021-06-22 19:52:21 +02:00
|
|
|
import * as exec from '@actions/exec';
|
2020-08-17 22:18:15 +02:00
|
|
|
import * as context from './context';
|
2020-08-16 00:36:41 +02:00
|
|
|
|
2023-01-11 12:10:34 +01:00
|
|
|
export type Builder = {
|
|
|
|
name?: string;
|
|
|
|
driver?: string;
|
|
|
|
nodes: Node[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Node = {
|
|
|
|
name?: string;
|
|
|
|
endpoint?: string;
|
|
|
|
'driver-opts'?: Array<string>;
|
|
|
|
status?: string;
|
|
|
|
'buildkitd-flags'?: string;
|
|
|
|
buildkit?: string;
|
|
|
|
platforms?: string;
|
|
|
|
};
|
|
|
|
|
2020-08-17 22:18:15 +02:00
|
|
|
export async function getImageIDFile(): Promise<string> {
|
2020-10-19 21:17:06 +02:00
|
|
|
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
|
2020-08-17 22:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getImageID(): Promise<string | undefined> {
|
|
|
|
const iidFile = await getImageIDFile();
|
|
|
|
if (!fs.existsSync(iidFile)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-24 16:49:17 +02:00
|
|
|
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim();
|
2020-08-17 22:18:15 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 23:44:13 +02:00
|
|
|
export async function getMetadataFile(): Promise<string> {
|
|
|
|
return path.join(context.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getMetadata(): Promise<string | undefined> {
|
|
|
|
const metadataFile = await getMetadataFile();
|
|
|
|
if (!fs.existsSync(metadataFile)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-24 16:49:17 +02:00
|
|
|
const content = fs.readFileSync(metadataFile, {encoding: 'utf-8'}).trim();
|
|
|
|
if (content === 'null') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return content;
|
2021-08-16 23:44:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 19:30:50 +01:00
|
|
|
export async function getDigest(metadata: string | undefined): Promise<string | undefined> {
|
|
|
|
if (metadata === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const metadataJSON = JSON.parse(metadata);
|
|
|
|
if (metadataJSON['containerimage.digest']) {
|
|
|
|
return metadataJSON['containerimage.digest'];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-15 10:08:19 +01:00
|
|
|
export async function getSecretString(kvp: string): Promise<string> {
|
|
|
|
return getSecret(kvp, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSecretFile(kvp: string): Promise<string> {
|
|
|
|
return getSecret(kvp, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSecret(kvp: string, file: boolean): Promise<string> {
|
2020-10-23 19:49:33 +02:00
|
|
|
const delimiterIndex = kvp.indexOf('=');
|
|
|
|
const key = kvp.substring(0, delimiterIndex);
|
2021-02-15 10:08:19 +01:00
|
|
|
let value = kvp.substring(delimiterIndex + 1);
|
2020-11-17 21:38:45 +01:00
|
|
|
if (key.length == 0 || value.length == 0) {
|
|
|
|
throw new Error(`${kvp} is not a valid secret`);
|
|
|
|
}
|
2021-02-15 10:08:19 +01:00
|
|
|
|
|
|
|
if (file) {
|
|
|
|
if (!fs.existsSync(value)) {
|
|
|
|
throw new Error(`secret file ${value} not found`);
|
|
|
|
}
|
|
|
|
value = fs.readFileSync(value, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:17:06 +02:00
|
|
|
const secretFile = context.tmpNameSync({
|
|
|
|
tmpdir: context.tmpDir()
|
2020-09-02 10:07:11 +02:00
|
|
|
});
|
2021-02-15 10:08:19 +01:00
|
|
|
fs.writeFileSync(secretFile, value);
|
|
|
|
|
2020-09-02 10:07:11 +02:00
|
|
|
return `id=${key},src=${secretFile}`;
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:59:52 +01:00
|
|
|
export function isLocalOrTarExporter(outputs: string[]): boolean {
|
2022-04-25 06:47:48 +02:00
|
|
|
const records = parse(outputs.join(`\n`), {
|
2020-10-20 15:18:02 +02:00
|
|
|
delimiter: ',',
|
|
|
|
trim: true,
|
|
|
|
columns: false,
|
2020-11-17 21:38:45 +01:00
|
|
|
relaxColumnCount: true
|
2022-04-25 06:47:48 +02:00
|
|
|
});
|
|
|
|
for (const record of records) {
|
2020-10-20 17:53:03 +02:00
|
|
|
// Local if no type is defined
|
|
|
|
// https://github.com/docker/buildx/blob/d2bf42f8b4784d83fde17acb3ed84703ddc2156b/build/output.go#L29-L43
|
2022-04-25 06:47:48 +02:00
|
|
|
if (record.length == 1 && !record[0].startsWith('type=')) {
|
2020-10-20 17:53:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-04-25 06:47:48 +02:00
|
|
|
for (const [key, value] of record.map(chunk => chunk.split('=').map(item => item.trim()))) {
|
2020-10-19 22:12:33 +02:00
|
|
|
if (key == 'type' && (value == 'local' || value == 'tar')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:59:52 +01:00
|
|
|
export function hasGitAuthToken(secrets: string[]): boolean {
|
|
|
|
for (const secret of secrets) {
|
2020-10-19 22:12:33 +02:00
|
|
|
if (secret.startsWith('GIT_AUTH_TOKEN=')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-28 09:31:47 +02:00
|
|
|
export async function isAvailable(standalone?: boolean): Promise<boolean> {
|
|
|
|
const cmd = getCommand([], standalone);
|
2021-06-22 19:52:21 +02:00
|
|
|
return await exec
|
2022-04-28 09:31:47 +02:00
|
|
|
.getExecOutput(cmd.command, cmd.args, {
|
2021-06-22 19:52:21 +02:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return res.exitCode == 0;
|
2022-04-28 09:31:47 +02:00
|
|
|
})
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
.catch(error => {
|
|
|
|
return false;
|
2021-06-22 19:52:21 +02:00
|
|
|
});
|
2020-08-16 00:36:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:10:34 +01:00
|
|
|
export async function satisfiesBuildKitVersion(builderName: string, range: string, standalone?: boolean): Promise<boolean> {
|
|
|
|
const builderInspect = await inspect(builderName, standalone);
|
|
|
|
for (const node of builderInspect.nodes) {
|
|
|
|
if (!node.buildkit) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// BuildKit version reported by moby is in the format of `v0.11.0-moby`
|
|
|
|
if (builderInspect.driver == 'docker' && !node.buildkit.endsWith('-moby')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const version = node.buildkit.replace(/-moby$/, '');
|
|
|
|
if (!semver.satisfies(version, range)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function inspect(name: string, standalone?: boolean): Promise<Builder> {
|
|
|
|
const cmd = getCommand(['inspect', name], standalone);
|
|
|
|
return await exec
|
|
|
|
.getExecOutput(cmd.command, cmd.args, {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(res.stderr.trim());
|
|
|
|
}
|
|
|
|
return parseInspect(res.stdout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function parseInspect(data: string): Promise<Builder> {
|
|
|
|
const builder: Builder = {
|
|
|
|
nodes: []
|
|
|
|
};
|
|
|
|
let node: Node = {};
|
|
|
|
for (const line of data.trim().split(`\n`)) {
|
|
|
|
const [key, ...rest] = line.split(':');
|
|
|
|
const value = rest.map(v => v.trim()).join(':');
|
|
|
|
if (key.length == 0 || value.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (key.toLowerCase()) {
|
|
|
|
case 'name': {
|
|
|
|
if (builder.name == undefined) {
|
|
|
|
builder.name = value;
|
|
|
|
} else {
|
|
|
|
if (Object.keys(node).length > 0) {
|
|
|
|
builder.nodes.push(node);
|
|
|
|
node = {};
|
|
|
|
}
|
|
|
|
node.name = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'driver': {
|
|
|
|
builder.driver = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'endpoint': {
|
|
|
|
node.endpoint = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'driver options': {
|
|
|
|
node['driver-opts'] = (value.match(/(\w+)="([^"]*)"/g) || []).map(v => v.replace(/^(.*)="(.*)"$/g, '$1=$2'));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'status': {
|
|
|
|
node.status = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'flags': {
|
|
|
|
node['buildkitd-flags'] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'buildkit': {
|
|
|
|
node.buildkit = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'platforms': {
|
|
|
|
let platforms: Array<string> = [];
|
|
|
|
// if a preferred platform is being set then use only these
|
|
|
|
// https://docs.docker.com/engine/reference/commandline/buildx_inspect/#get-information-about-a-builder-instance
|
|
|
|
if (value.includes('*')) {
|
|
|
|
for (const platform of value.split(', ')) {
|
|
|
|
if (platform.includes('*')) {
|
|
|
|
platforms.push(platform.replace('*', ''));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// otherwise set all platforms available
|
|
|
|
platforms = value.split(', ');
|
|
|
|
}
|
|
|
|
node.platforms = platforms.join(',');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(node).length > 0) {
|
|
|
|
builder.nodes.push(node);
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2022-04-28 09:31:47 +02:00
|
|
|
export async function getVersion(standalone?: boolean): Promise<string> {
|
|
|
|
const cmd = getCommand(['version'], standalone);
|
2021-06-22 19:52:21 +02:00
|
|
|
return await exec
|
2022-04-28 09:31:47 +02:00
|
|
|
.getExecOutput(cmd.command, cmd.args, {
|
2021-06-22 19:52:21 +02:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(res.stderr.trim());
|
|
|
|
}
|
2021-07-01 16:55:21 +02:00
|
|
|
return parseVersion(res.stdout.trim());
|
2021-06-22 19:52:21 +02:00
|
|
|
});
|
2020-08-23 03:31:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 15:29:36 +02:00
|
|
|
export function parseVersion(stdout: string): string {
|
|
|
|
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
|
2020-08-23 03:31:38 +02:00
|
|
|
if (!matches) {
|
2021-04-27 16:16:22 +02:00
|
|
|
throw new Error(`Cannot parse buildx version`);
|
2020-08-23 03:31:38 +02:00
|
|
|
}
|
2021-07-01 15:29:36 +02:00
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function satisfies(version: string, range: string): boolean {
|
|
|
|
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
|
2020-08-23 03:31:38 +02:00
|
|
|
}
|
2022-04-28 09:31:47 +02:00
|
|
|
|
|
|
|
export function getCommand(args: Array<string>, standalone?: boolean) {
|
|
|
|
return {
|
|
|
|
command: standalone ? 'buildx' : 'docker',
|
|
|
|
args: standalone ? args : ['buildx', ...args]
|
|
|
|
};
|
|
|
|
}
|