Merge pull request #2887 from balena-io/refactor-build

Refactor balena build for clarity
This commit is contained in:
flowzone-app[bot] 2024-11-08 08:09:19 +00:00 committed by GitHub
commit 92fd9e0883
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 32 deletions

24
npm-shrinkwrap.json generated
View File

@ -6943,9 +6943,9 @@
}
},
"node_modules/cross-env/node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz",
"integrity": "sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==",
"dev": true,
"dependencies": {
"path-key": "^3.1.0",
@ -8261,9 +8261,9 @@
}
},
"node_modules/eslint/node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz",
"integrity": "sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==",
"dev": true,
"dependencies": {
"path-key": "^3.1.0",
@ -9221,9 +9221,9 @@
}
},
"node_modules/foreground-child/node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz",
"integrity": "sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==",
"dependencies": {
"path-key": "^3.1.0",
"shebang-command": "^2.0.0",
@ -14081,9 +14081,9 @@
}
},
"node_modules/patch-package/node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz",
"integrity": "sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==",
"dependencies": {
"path-key": "^3.1.0",
"shebang-command": "^2.0.0",

View File

@ -36,14 +36,16 @@ import { buildProject, composeCliFlags } from '../../utils/compose_ts';
import type { BuildOpts, DockerCliFlags } from '../../utils/docker';
import { dockerCliFlags } from '../../utils/docker';
// TODO: For this special one we can't use Interfaces.InferredFlags/InferredArgs
// because of the 'registry-secrets' type which is defined in the actual code
// as a path (string | undefined) but then the cli turns it into an object
interface FlagsDef extends ComposeCliFlags, DockerCliFlags {
type ComposeGenerateOptsParam = Parameters<typeof compose.generateOpts>[0];
interface PrepareBuildOpts
extends ComposeCliFlags,
DockerCliFlags,
ComposeGenerateOptsParam {
arch?: string;
deviceType?: string;
fleet?: string;
source?: string; // Not part of command profile - source param copied here.
source?: string;
}
export default class BuildCmd extends Command {
@ -113,29 +115,31 @@ ${dockerignoreHelp}
const logger = Logger.getLogger();
logger.logDebug('Parsing input...');
// `build` accepts `source` as a parameter, but compose expects it as an option
options.source = params.source;
delete params.source;
const prepareBuildOpts = {
...options,
source: params.source,
};
await this.resolveArchFromDeviceType(sdk, options);
await this.resolveArchFromDeviceType(sdk, prepareBuildOpts);
await this.validateOptions(options, sdk);
await this.validateOptions(prepareBuildOpts, sdk);
// Build args are under consideration for removal - warn user
if (options.buildArg) {
if (prepareBuildOpts.buildArg) {
console.log(buildArgDeprecation);
}
const app = await this.getAppAndResolveArch(options);
const app = await this.getAppAndResolveArch(prepareBuildOpts);
const { docker, buildOpts, composeOpts } = await this.prepareBuild(options);
const { docker, buildOpts, composeOpts } =
await this.prepareBuild(prepareBuildOpts);
try {
await this.buildProject(docker, logger, composeOpts, {
appType: app?.application_type?.[0],
arch: options.arch!,
deviceType: options.deviceType!,
buildEmulated: options.emulated,
arch: prepareBuildOpts.arch!,
deviceType: prepareBuildOpts.deviceType!,
buildEmulated: prepareBuildOpts.emulated,
buildOpts,
});
} catch (err) {
@ -147,7 +151,7 @@ ${dockerignoreHelp}
logger.logSuccess('Build succeeded!');
}
protected async validateOptions(opts: FlagsDef, sdk: BalenaSDK) {
protected async validateOptions(opts: PrepareBuildOpts, sdk: BalenaSDK) {
// Validate option combinations
if (
(opts.fleet == null && (opts.arch == null || opts.deviceType == null)) ||
@ -175,7 +179,10 @@ ${dockerignoreHelp}
opts['registry-secrets'] = registrySecrets;
}
protected async resolveArchFromDeviceType(sdk: BalenaSDK, opts: FlagsDef) {
protected async resolveArchFromDeviceType(
sdk: BalenaSDK,
opts: PrepareBuildOpts,
) {
if (opts.deviceType != null && opts.arch == null) {
try {
const deviceTypeOpts = {
@ -208,7 +215,7 @@ ${dockerignoreHelp}
}
}
protected async getAppAndResolveArch(opts: FlagsDef) {
protected async getAppAndResolveArch(opts: PrepareBuildOpts) {
if (opts.fleet) {
const { getAppWithArch } = await import('../../utils/helpers');
const app = await getAppWithArch(opts.fleet);
@ -218,7 +225,7 @@ ${dockerignoreHelp}
}
}
protected async prepareBuild(options: FlagsDef) {
protected async prepareBuild(options: PrepareBuildOpts) {
const { getDocker, generateBuildOpts } = await import('../../utils/docker');
const [docker, buildOpts, composeOpts] = await Promise.all([
getDocker(options),