Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
- add cacheToLatest opt
- add help description
  • Loading branch information
Roy Razon committed Nov 12, 2023
1 parent ce0ca71 commit 4090a60
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
14 changes: 9 additions & 5 deletions packages/cli/src/commands/up.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, Flags, ux } from '@oclif/core'
import { Args, Flags, ux, Errors } from '@oclif/core'
import {
addBaseComposeTunnelAgentService,
commands, findComposeTunnelAgentUrl,
Expand All @@ -22,12 +22,16 @@ export default class Up extends MachineCreationDriverCommand<typeof Up> {
static flags = {
...envIdFlags,
...tunnelServerFlags,
'split-build': Flags.custom<commands.SplitBuildSpec>({
description: 'Build locally and deploy remotely using an image registry',
'local-build': Flags.custom<commands.LocalBuildSpec>({
description: `Build locally and deploy remotely using an image registry; ${Object.entries(commands.localBuildSpecSchema.shape).map(([k, v]) => `${k}: ${v.description}`).join(', ')}`,
required: false,
parse: async input => {
const pairs = input.split(',')
return commands.splitBuildSpecSchema.parse(Object.fromEntries(pairs.map(pair => pair.split('='))))
const result = commands.localBuildSpecSchema.safeParse(Object.fromEntries(pairs.map(pair => pair.split('='))))
if (!result.success) {
throw new Errors.CLIError(`Invalid local build arg: ${inspect(result.error)}`)
}
return result.data
},
})(),
'skip-unchanged-files': Flags.boolean({
Expand Down Expand Up @@ -152,7 +156,7 @@ export default class Up extends MachineCreationDriverCommand<typeof Up> {
cwd: process.cwd(),
skipUnchangedFiles: flags['skip-unchanged-files'],
version: this.config.version,
splitBuildSpec: flags['split-build'],
localBuildSpec: flags['local-build'],
})

this.log(`Preview environment ${envId} provisioned at: ${machine.locationDescription}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { SplitBuildSpec, splitBuildSpecSchema, default as up } from './up'
export { LocalBuildSpec, localBuildSpecSchema, default as up } from './up'
export { default as ls } from './ls'
export { default as shell } from './shell'

Expand Down
19 changes: 10 additions & 9 deletions packages/core/src/commands/up/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { Logger } from '../../log'
import { FileToCopy, uploadWithSpinner } from '../../upload-files'
import { envMetadata } from '../../env-metadata'
import { EnvId } from '../../env-id'
import { SplitBuildSpec } from './split-build'
import { LocalBuildSpec } from './split-build'
import { gitContext } from '../../git'
import { randomString } from '../../strings'

export { splitBuildSpecSchema, SplitBuildSpec } from './split-build'
export { localBuildSpecSchema, LocalBuildSpec } from './split-build'

const createCopiedFileInDataDir = (
{ projectLocalDataDir, filesToCopy } : {
Expand Down Expand Up @@ -103,7 +103,7 @@ const up = async ({
envId,
expectedServiceUrls,
projectName,
splitBuildSpec,
localBuildSpec,
}: {
debug: boolean
machineDriver: MachineDriver
Expand All @@ -124,7 +124,7 @@ const up = async ({
envId: EnvId
expectedServiceUrls: { name: string; port: number; url: string }[]
projectName: string
splitBuildSpec?: SplitBuildSpec
localBuildSpec?: LocalBuildSpec
}): Promise<{ machine: MachineBase }> => {
const remoteDir = remoteProjectDir(projectName)

Expand Down Expand Up @@ -180,15 +180,15 @@ const up = async ({
)
}

if (splitBuildSpec) {
if (localBuildSpec) {
const tagSuffix = await gitContext(cwd)?.commit({ short: true }) ?? randomString.lowercaseNumeric(8)
const serviceImage = (service: string, tag: string) => registryImageName(
{
registry: splitBuildSpec.registry,
registry: localBuildSpec.registry,
image: `preevy-${envId}-${service}`,
tag,
},
{ ecrFormat: splitBuildSpec.ecrFormat },
{ ecrFormat: localBuildSpec.ecrFormat },
)

const buildServices = mapValues(
Expand All @@ -200,6 +200,7 @@ const up = async ({
build: Object.assign(build, {
tags: (build.tags ?? []).concat(thisImage, latestImage),
cache_from: (build.cache_from ?? []).concat(latestImage),
cache_to: (build.cache_to ?? []).concat(...localBuildSpec.cacheToLatest ? [latestImage] : []),
}),
})
},
Expand All @@ -211,7 +212,7 @@ const up = async ({
await childProcessPromise(spawn('docker', [
'buildx', 'bake',
'-f', buildFilename,
`--set=*.platform=${splitBuildSpec.platform ?? dockerPlatform}`,
`--set=*.platform=${localBuildSpec.platform ?? dockerPlatform}`,
'--push',
], {
stdio: 'inherit',
Expand All @@ -238,7 +239,7 @@ const up = async ({
composeFiles: [composeFilePath.local],
projectName: userSpecifiedProjectName,
})
const composeArgs = calcComposeUpArgs({ userSpecifiedServices, debug, cwd, build: !splitBuildSpec })
const composeArgs = calcComposeUpArgs({ userSpecifiedServices, debug, cwd, build: !localBuildSpec })

const withDockerSocket = wrapWithDockerSocket({ connection, log })

Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/commands/up/split-build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import z from 'zod'

export const splitBuildSpecSchema = z.object({
registry: z.string(),
platform: z.string().optional(),
ecrFormat: z.boolean().optional(),
export const localBuildSpecSchema = z.object({
registry: z.string().describe('registry to use'),
platform: z.string().optional().describe('platform to build for (default: driver platform)'),
ecrFormat: z.boolean().optional().describe('use ECR format (default: auto detect)'),
cacheToLatest: z.boolean().default(false).describe('enable cache-to with latest tag (default: false)'),
})

export type SplitBuildSpec = z.infer<typeof splitBuildSpecSchema>
export type LocalBuildSpec = z.infer<typeof localBuildSpecSchema>

0 comments on commit 4090a60

Please sign in to comment.