2024-10-07 14:16:41 +02:00
import * as crypto from 'crypto' ;
2020-09-03 16:23:15 +02:00
import * as core from '@actions/core' ;
2024-07-03 23:10:05 +02:00
2026-02-28 10:02:54 +01:00
import { Docker } from '@docker/actions-toolkit/lib/docker/docker.js' ;
import { Util } from '@docker/actions-toolkit/lib/util.js' ;
import { Toolkit } from '@docker/actions-toolkit/lib/toolkit.js' ;
2024-07-03 23:10:05 +02:00
2026-02-28 10:02:54 +01:00
import { Node } from '@docker/actions-toolkit/lib/types/buildx/builder.js' ;
2020-09-03 16:23:15 +02:00
2023-02-19 02:12:29 +01:00
export const builderNodeEnvPrefix = 'BUILDER_NODE' ;
2024-10-02 11:36:15 +02:00
const defaultBuildkitdFlags = '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host' ;
2021-09-03 22:21:20 +02:00
2020-09-03 16:23:15 +02:00
export interface Inputs {
version : string ;
2022-09-19 11:36:58 +02:00
name : string ;
2020-09-03 16:23:15 +02:00
driver : string ;
driverOpts : string [];
buildkitdFlags : string ;
2024-03-01 08:57:18 +01:00
buildkitdConfig : string ;
buildkitdConfigInline : string ;
2022-09-22 11:54:00 +02:00
platforms : string [];
2020-09-03 16:23:15 +02:00
use : boolean ;
2020-09-08 15:52:09 +02:00
endpoint : string ;
2022-09-19 11:34:47 +02:00
append : string ;
2024-02-23 10:51:47 +01:00
cacheBinary : boolean ;
2023-03-03 11:56:21 +01:00
cleanup : boolean ;
2023-02-23 13:11:05 -08:00
keepState : boolean ;
2020-09-03 16:23:15 +02:00
}
export async function getInputs () : Promise < Inputs > {
return {
version : core.getInput ( 'version' ),
2023-02-23 13:11:05 -08:00
name : await getBuilderName ( core . getInput ( 'name' ), core . getInput ( 'driver' ) || 'docker-container' ),
2020-09-03 16:23:15 +02:00
driver : core.getInput ( 'driver' ) || 'docker-container' ,
2023-02-19 02:12:29 +01:00
driverOpts : Util.getInputList ( 'driver-opts' , { ignoreComma : true , quote : false }),
2024-10-02 11:36:15 +02:00
buildkitdFlags : core.getInput ( 'buildkitd-flags' ),
2023-02-19 02:12:29 +01:00
platforms : Util.getInputList ( 'platforms' ),
2021-06-23 15:43:25 +02:00
use : core.getBooleanInput ( 'use' ),
2021-04-21 18:37:54 +01:00
endpoint : core.getInput ( 'endpoint' ),
2025-12-19 10:48:23 +01:00
buildkitdConfig : core.getInput ( 'buildkitd-config' ),
buildkitdConfigInline : core.getInput ( 'buildkitd-config-inline' ),
2023-03-03 11:56:21 +01:00
append : core.getInput ( 'append' ),
2025-06-13 18:06:00 +02:00
keepState : core.getBooleanInput ( 'keep-state' ),
2024-02-23 10:51:47 +01:00
cacheBinary : core.getBooleanInput ( 'cache-binary' ),
2025-06-13 18:06:00 +02:00
cleanup : core.getBooleanInput ( 'cleanup' )
2020-09-03 16:23:15 +02:00
};
}
2023-02-23 13:11:05 -08:00
export async function getBuilderName ( name : string , driver : string ) : Promise < string > {
2025-06-13 18:06:00 +02:00
return driver == 'docker' ? await Docker . context () : name || `builder- ${ crypto . randomUUID () } ` ;
2022-09-19 11:36:58 +02:00
}
2023-02-19 02:12:29 +01:00
export async function getCreateArgs ( inputs : Inputs , toolkit : Toolkit ) : Promise < Array < string >> {
2022-09-19 11:36:58 +02:00
const args : Array < string > = [ 'create' , '--name' , inputs . name , '--driver' , inputs . driver ];
2026-08-26 18:23:46 +02:00
if ( inputs . driver === 'cloud' && ! inputs . version . startsWith ( 'cloud:' ) && ! inputs . version . startsWith ( 'lab:' ) && ! ( await toolkit . buildx . versionSatisfies ( '>=0.37.0-0' ))) {
throw new Error ( `Docker Build Cloud with official Buildx requires Buildx v0.37.0 or later. Set version to cloud:<version> or lab:<version> to use the legacy Docker Build Cloud release channel.` );
}
2023-02-19 02:12:29 +01:00
if ( await toolkit . buildx . versionSatisfies ( '>=0.3.0' )) {
2024-10-02 11:36:15 +02:00
await Util . asyncForEach ( inputs . driverOpts , async ( driverOpt : string ) => {
2022-09-19 11:36:58 +02:00
args . push ( '--driver-opt' , driverOpt );
});
2024-10-02 11:36:15 +02:00
if ( inputs . buildkitdFlags ) {
2022-09-19 11:36:58 +02:00
args . push ( '--buildkitd-flags' , inputs . buildkitdFlags );
2024-10-02 11:36:15 +02:00
} else if ( driverSupportsBuildkitdFlags ( inputs . driver )) {
args . push ( '--buildkitd-flags' , defaultBuildkitdFlags );
2022-09-19 11:36:58 +02:00
}
}
2022-09-22 11:54:00 +02:00
if ( inputs . platforms . length > 0 ) {
args . push ( '--platform' , inputs . platforms . join ( ',' ));
}
2022-09-19 11:36:58 +02:00
if ( inputs . use ) {
args . push ( '--use' );
}
2024-10-02 11:36:15 +02:00
if ( inputs . buildkitdConfig ) {
args . push ( '--config' , toolkit . buildkit . config . resolveFromFile ( inputs . buildkitdConfig ));
} else if ( inputs . buildkitdConfigInline ) {
args . push ( '--config' , toolkit . buildkit . config . resolveFromString ( inputs . buildkitdConfigInline ));
2022-09-19 11:36:58 +02:00
}
if ( inputs . endpoint ) {
args . push ( inputs . endpoint );
}
return args ;
}
2023-02-19 02:12:29 +01:00
export async function getAppendArgs ( inputs : Inputs , node : Node , toolkit : Toolkit ) : Promise < Array < string >> {
2022-09-19 11:34:47 +02:00
const args : Array < string > = [ 'create' , '--name' , inputs . name , '--append' ];
if ( node . name ) {
args . push ( '--node' , node . name );
2023-03-10 19:33:52 +01:00
} else if ( inputs . driver == 'kubernetes' && ( await toolkit . buildx . versionSatisfies ( '<0.11.0' ))) {
2024-10-07 14:16:41 +02:00
args . push ( '--node' , `node- ${ crypto . randomUUID () } ` );
2022-09-19 11:34:47 +02:00
}
2023-02-19 02:12:29 +01:00
if ( node [ 'driver-opts' ] && ( await toolkit . buildx . versionSatisfies ( '>=0.3.0' ))) {
2024-10-02 11:36:15 +02:00
await Util . asyncForEach ( node [ 'driver-opts' ], async ( driverOpt : string ) => {
2022-09-19 11:34:47 +02:00
args . push ( '--driver-opt' , driverOpt );
});
2024-10-02 11:36:15 +02:00
if ( node [ 'buildkitd-flags' ]) {
2022-09-19 11:34:47 +02:00
args . push ( '--buildkitd-flags' , node [ 'buildkitd-flags' ]);
2024-10-02 11:36:15 +02:00
} else if ( driverSupportsBuildkitdFlags ( inputs . driver )) {
args . push ( '--buildkitd-flags' , defaultBuildkitdFlags );
2022-09-19 11:34:47 +02:00
}
}
if ( node . platforms ) {
args . push ( '--platform' , node . platforms );
}
if ( node . endpoint ) {
args . push ( node . endpoint );
}
return args ;
}
2023-02-19 02:12:29 +01:00
export async function getInspectArgs ( inputs : Inputs , toolkit : Toolkit ) : Promise < Array < string >> {
2022-09-19 11:36:58 +02:00
const args : Array < string > = [ 'inspect' , '--bootstrap' ];
2023-02-19 02:12:29 +01:00
if ( await toolkit . buildx . versionSatisfies ( '>=0.4.0' )) {
2022-09-19 11:36:58 +02:00
args . push ( '--builder' , inputs . name );
}
return args ;
}
2023-06-28 09:12:40 -04:00
2024-10-02 11:36:15 +02:00
function driverSupportsBuildkitdFlags ( driver : string ) : boolean {
2023-06-28 09:12:40 -04:00
return driver == '' || driver == 'docker-container' || driver == 'docker' || driver == 'kubernetes' ;
}