Skip to content

Commit

Permalink
feat: added buildContext param to buildModule handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 22, 2018
1 parent d3a44cd commit 141abe9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export interface PluginContext extends PluginContextGuard, WrappedFromGarden {
parseModule: <T extends Module>(moduleConfig: T["_ConfigType"]) => Promise<T>
getModuleBuildPath: <T extends Module>(module: T) => Promise<string>
getModuleBuildStatus: <T extends Module>(module: T, logEntry?: LogEntry) => Promise<BuildStatus>
buildModule: <T extends Module>(module: T, logEntry?: LogEntry) => Promise<BuildResult>
buildModule: <T extends Module>(
module: T, buildContext: PrimitiveMap, logEntry?: LogEntry,
) => Promise<BuildResult>
pushModule: <T extends Module>(module: T, logEntry?: LogEntry) => Promise<PushResult>
testModule: <T extends Module>(module: T, testSpec: TestSpec, logEntry?: LogEntry) => Promise<TestResult>
getTestResult: <T extends Module>(module: T, version: TreeVersion, logEntry?: LogEntry) => Promise<TestResult | null>
Expand Down Expand Up @@ -165,11 +167,11 @@ export function createPluginContext(garden: Garden): PluginContext {
return handler({ ...commonParams(handler), module, logEntry })
},

buildModule: async <T extends Module>(module: T, logEntry?: LogEntry) => {
await garden.buildDir.syncDependencyProducts(module)
buildModule: async <T extends Module>(module: T, buildContext: PrimitiveMap, logEntry?: LogEntry) => {
await ctx.stageBuild(module)
const defaultHandler = garden.getModuleActionHandler("buildModule", "generic")
const handler = garden.getModuleActionHandler("buildModule", module.type, defaultHandler)
return handler({ ...commonParams(handler), module, logEntry })
return handler({ ...commonParams(handler), module, buildContext, logEntry })
},

stageBuild: async <T extends Module>(module: T) => {
Expand Down
23 changes: 18 additions & 5 deletions src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ export interface ServiceEndpointSpec {
port: string
}

export type ServicePortProtocol = "TCP" | "UDP"

export interface ServicePortSpec {
protocol: "TCP" | "UDP"
protocol: ServicePortProtocol
containerPort: number
hostPort?: number
nodePort?: number
Expand Down Expand Up @@ -257,8 +259,14 @@ export const gardenPlugin = (): GardenPlugin => ({
return { ready: !!identifier }
},

async buildModule({ ctx, module, logEntry }: BuildModuleParams<ContainerModule>) {
if (!!module.image && !module.hasDockerfile()) {
async buildModule({ ctx, module, buildContext, logEntry }: BuildModuleParams<ContainerModule>) {
const buildPath = await module.getBuildPath()
const dockerfilePath = join(buildPath, "Dockerfile")

if (!!module.image && !existsSync(dockerfilePath)) {
if (await module.imageExistsLocally()) {
return { fresh: false }
}
logEntry && logEntry.setState(`Pulling image ${module.image}...`)
await module.pullImage(ctx)
return { fetched: true }
Expand All @@ -269,11 +277,16 @@ export const gardenPlugin = (): GardenPlugin => ({
// build doesn't exist, so we create it
logEntry && logEntry.setState(`Building ${identifier}...`)

const buildArgs = Object.entries(buildContext).map(([key, value]) => {
// TODO: may need to escape this
return `--build-arg ${key}=${value}`
}).join(" ")

// TODO: log error if it occurs
// TODO: stream output to log if at debug log level
await module.dockerCli(`build -t ${identifier} ${await module.getBuildPath()}`)
await module.dockerCli(`build ${buildArgs} -t ${identifier} ${buildPath}`)

return { fresh: true }
return { fresh: true, details: { identifier } }
},

async pushModule({ module, logEntry }: PushModuleParams<ContainerModule>) {
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ServiceConfig,
} from "../types/service"
import { spawn } from "../util"
import { mapValues } from "lodash"

export const name = "generic"

Expand All @@ -43,14 +44,19 @@ export const genericPlugin = {
return { ready: !(await module.getConfig()).build.command }
},

async buildModule({ module }: BuildModuleParams): Promise<BuildResult> {
async buildModule({ module, buildContext }: BuildModuleParams): Promise<BuildResult> {
// By default we run the specified build command in the module root, if any.
// TODO: Keep track of which version has been built (needs local data store/cache).
const config: ModuleConfig = await module.getConfig()

const contextEnv = mapValues(buildContext, v => v + "")

if (config.build.command) {
const buildPath = await module.getBuildPath()
const result = await exec(config.build.command, { cwd: buildPath })
const result = await exec(config.build.command, {
cwd: buildPath,
env: { ...process.env, contextEnv },
})

return {
fresh: true,
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BuildTask<T extends Module> extends Task {
})

const startTime = new Date().getTime()
const result = await this.ctx.buildModule(this.module, entry)
const result = await this.ctx.buildModule(this.module, {}, entry)
const buildTime = (new Date().getTime()) - startTime

entry.setSuccess({ msg: chalk.green(`Done (took ${round(buildTime / 1000, 1)} sec)`), append: true })
Expand Down
2 changes: 2 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface GetModuleBuildStatusParams<T extends Module = Module> extends P

export interface BuildModuleParams<T extends Module = Module> extends PluginActionParamsBase {
module: T
buildContext: PrimitiveMap
}

export interface PushModuleParams<T extends Module = Module> extends PluginActionParamsBase {
Expand Down Expand Up @@ -153,6 +154,7 @@ export interface BuildResult {
fetched?: boolean
fresh?: boolean
version?: string
details?: any
}

export interface PushResult {
Expand Down
16 changes: 9 additions & 7 deletions test/src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ describe("container", () => {
variables: {},
}))

td.when(module.hasDockerfile()).thenReturn(false)
td.when(module.getBuildPath()).thenResolve("/tmp/jaoigjwaeoigjweaoglwaeghe")
td.when(module.pullImage(ctx)).thenResolve(null)

const result = await ctx.buildModule(module)
const result = await ctx.buildModule(module, {})

expect(result).to.eql({ fetched: true })
})
Expand All @@ -395,15 +395,17 @@ describe("container", () => {
variables: {},
}))

td.when(module.hasDockerfile()).thenReturn(true)
td.when(module.getLocalImageId()).thenResolve("some/image")
td.when(module.getBuildPath()).thenResolve("/tmp/something")
td.when(module.getBuildPath()).thenResolve(modulePath)

const result = await ctx.buildModule(module)
const result = await ctx.buildModule(module, {})

expect(result).to.eql({ fresh: true })
expect(result).to.eql({
fresh: true,
details: { identifier: "some/image" },
})

td.verify(module.dockerCli("build -t some/image /tmp/something"))
td.verify(module.dockerCli("build -t some/image " + modulePath))
})
})

Expand Down

0 comments on commit 141abe9

Please sign in to comment.