Skip to content

Commit

Permalink
CLI add build args (#385)
Browse files Browse the repository at this point in the history
This feature allows users to pass arguments used during the template
build.

E.g.:
```
e2b template build --build-arg="HELLO=WORLD" --build-arg="MULTIPLE=True"
```
  • Loading branch information
jakubno authored Jun 11, 2024
2 parents ccbd0ef + c586ad7 commit e12387e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-readers-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2b/cli": patch
---

Add possibility to pass docker build args
3 changes: 3 additions & 0 deletions apps/docs/src/app/cli/commands/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ If there is no `e2b.toml` config a new template will be created.
<Option type="--memory-mb" name="memory-mb">
Specify the amount of memory in megabytes that will be used to run the sandbox. Must be an even number. The default value is 512.
</Option>
<Option type="--build-arg" name="build-arg">
Specify a build argument for the Dockerfile. The format is `key=value`. You can use this option multiple times.
</Option>
</Options>

## `template delete`
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/app/sandbox/templates/template-file/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The Dockerfile must be Debian based (e.g. Ubuntu). Only the following [Dockerfil
- `COPY`
- `RUN`
- `WORKDIR`

- `ARG`

## Example

Expand Down
17 changes: 15 additions & 2 deletions packages/cli/src/commands/template/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export const buildCommand = new commander.Command('build')
'specify the amount of memory in megabytes that will be used to run the sandbox. Must be an even number. The default value is 512.',
parseInt,
)
.option(
'--build-arg <args...>',
'specify additional build arguments for the build command. The format should be <varname>=<value>.',
)
.alias('bd')
.action(
async (
Expand All @@ -111,6 +115,7 @@ export const buildCommand = new commander.Command('build')
config?: string
cpuCount?: number
memoryMb?: number
buildArgs?: [string]
},
) => {
try {
Expand All @@ -122,6 +127,14 @@ export const buildCommand = new commander.Command('build')
process.exit(1)
}

const dockerBuildArgs: {[key: string]: string} = {}
if (opts.buildArgs) {
opts.buildArgs.forEach((arg) => {
const [key, value] = arg.split('=')
dockerBuildArgs[key] = value
})
}

const accessToken = ensureAccessToken()
process.stdout.write('\n')

Expand Down Expand Up @@ -265,7 +278,7 @@ export const buildCommand = new commander.Command('build')

console.log('Building docker image...')
child_process.execSync(
`docker build . -f ${dockerfileRelativePath} --platform linux/amd64 -t docker.${e2b.SANDBOX_DOMAIN}/e2b/custom-envs/${templateID}:${template.buildID}`,
`docker build . -f ${dockerfileRelativePath} --platform linux/amd64 -t docker.${e2b.SANDBOX_DOMAIN}/e2b/custom-envs/${templateID}:${template.buildID} ${Object.entries(dockerBuildArgs).map(([key, value]) => `--build-arg ${key}=${value}`).join(' ')}`,
{
stdio: 'inherit',
cwd: root,
Expand Down Expand Up @@ -502,7 +515,7 @@ function getDockerfile(root: string, file?: string) {
throw new Error(
`No ${asLocalRelative(defaultDockerfileRelativePath)} or ${asLocalRelative(
fallbackDockerfileRelativeName,
)} found in the root directory.You can specify a custom Dockerfile with ${asBold(
)} found in the root directory (${root}). You can specify a custom Dockerfile with ${asBold(
'--dockerfile <file>',
)} option.`,
)
Expand Down

0 comments on commit e12387e

Please sign in to comment.