2020-11-17 20:38:45 +00:00
|
|
|
import csvparse from 'csv-parse/lib/sync';
|
2020-08-17 20:18:15 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-08-23 01:31:38 +00:00
|
|
|
import * as semver from 'semver';
|
2021-06-22 17:52:21 +00:00
|
|
|
import * as exec from '@actions/exec';
|
2020-11-17 20:38:45 +00:00
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
import * as context from './context';
|
2020-08-15 22:36:41 +00:00
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
export async function getImageIDFile(): Promise<string> {
|
2020-10-19 19:17:06 +00:00
|
|
|
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
|
2020-08-17 20:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getImageID(): Promise<string | undefined> {
|
|
|
|
const iidFile = await getImageIDFile();
|
|
|
|
if (!fs.existsSync(iidFile)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:08:19 +00: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 17:49:33 +00:00
|
|
|
const delimiterIndex = kvp.indexOf('=');
|
|
|
|
const key = kvp.substring(0, delimiterIndex);
|
2021-02-15 09:08:19 +00:00
|
|
|
let value = kvp.substring(delimiterIndex + 1);
|
2020-11-17 20:38:45 +00:00
|
|
|
if (key.length == 0 || value.length == 0) {
|
|
|
|
throw new Error(`${kvp} is not a valid secret`);
|
|
|
|
}
|
2021-02-15 09:08:19 +00: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 19:17:06 +00:00
|
|
|
const secretFile = context.tmpNameSync({
|
|
|
|
tmpdir: context.tmpDir()
|
2020-09-02 08:07:11 +00:00
|
|
|
});
|
2021-02-15 09:08:19 +00:00
|
|
|
fs.writeFileSync(secretFile, value);
|
|
|
|
|
2020-09-02 08:07:11 +00:00
|
|
|
return `id=${key},src=${secretFile}`;
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:12:33 +00:00
|
|
|
export function isLocalOrTarExporter(outputs: string[]): Boolean {
|
2020-10-20 13:18:02 +00:00
|
|
|
for (let output of csvparse(outputs.join(`\n`), {
|
|
|
|
delimiter: ',',
|
|
|
|
trim: true,
|
|
|
|
columns: false,
|
2020-11-17 20:38:45 +00:00
|
|
|
relaxColumnCount: true
|
2020-10-20 13:18:02 +00:00
|
|
|
})) {
|
2020-10-20 15:53:03 +00:00
|
|
|
// Local if no type is defined
|
|
|
|
// https://github.com/docker/buildx/blob/d2bf42f8b4784d83fde17acb3ed84703ddc2156b/build/output.go#L29-L43
|
|
|
|
if (output.length == 1 && !output[0].startsWith('type=')) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-20 13:18:02 +00:00
|
|
|
for (let [key, value] of output.map(chunk => chunk.split('=').map(item => item.trim()))) {
|
2020-10-19 20:12:33 +00:00
|
|
|
if (key == 'type' && (value == 'local' || value == 'tar')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasGitAuthToken(secrets: string[]): Boolean {
|
|
|
|
for (let secret of secrets) {
|
|
|
|
if (secret.startsWith('GIT_AUTH_TOKEN=')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:36:41 +00:00
|
|
|
export async function isAvailable(): Promise<Boolean> {
|
2021-06-22 17:52:21 +00:00
|
|
|
return await exec
|
|
|
|
.getExecOutput('docker', ['buildx'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return res.exitCode == 0;
|
|
|
|
});
|
2020-08-15 22:36:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 01:31:38 +00:00
|
|
|
export async function getVersion(): Promise<string> {
|
2021-06-22 17:52:21 +00:00
|
|
|
return await exec
|
|
|
|
.getExecOutput('docker', ['buildx', 'version'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(res.stderr.trim());
|
|
|
|
}
|
|
|
|
return parseVersion(res.stdout);
|
|
|
|
});
|
2020-08-23 01:31:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 13:29:36 +00:00
|
|
|
export function parseVersion(stdout: string): string {
|
|
|
|
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
|
2020-08-23 01:31:38 +00:00
|
|
|
if (!matches) {
|
2021-04-27 14:16:22 +00:00
|
|
|
throw new Error(`Cannot parse buildx version`);
|
2020-08-23 01:31:38 +00:00
|
|
|
}
|
2021-07-01 13:29:36 +00: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 01:31:38 +00:00
|
|
|
}
|