Skip to content

Commit

Permalink
Support environment variables defined in containerEnv (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlehmann authored Mar 3, 2024
1 parent ff637ae commit c83d7b6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Spec struct {
Build BuildSpec `json:"build"`
RemoteUser string `json:"remoteUser"`
ContainerUser string `json:"containerUser"`
ContainerEnv map[string]string `json:"containerEnv"`
RemoteEnv map[string]string `json:"remoteEnv"`
// Features is a map of feature names to feature configurations.
Features map[string]any `json:"features"`
Expand Down Expand Up @@ -86,8 +87,10 @@ func (s Spec) HasDockerfile() bool {
// be written to if one doesn't exist.
func (s *Spec) Compile(fs billy.Filesystem, devcontainerDir, scratchDir, fallbackDockerfile string) (*Compiled, error) {
env := make([]string, 0)
for key, value := range s.RemoteEnv {
env = append(env, key+"="+value)
for _, envMap := range []map[string]string{s.ContainerEnv, s.RemoteEnv} {
for key, value := range envMap {
env = append(env, key+"="+value)
}
}
params := &Compiled{
User: s.ContainerUser,
Expand Down
4 changes: 2 additions & 2 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,8 @@ func Run(ctx context.Context, options Options) error {

configFile.Config.User = container.RemoteUser
}
if container.RemoteEnv != nil {
for key, value := range container.RemoteEnv {
for _, env := range []map[string]string{container.ContainerEnv, container.RemoteEnv} {
for key, value := range env {
os.Setenv(key, value)
exportEnv(key, value)
}
Expand Down
8 changes: 6 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,11 @@ func TestExportEnvFile(t *testing.T) {
"build": {
"dockerfile": "Dockerfile"
},
"containerEnv": {
"FROM_CONTAINER_ENV": "bar"
},
"remoteEnv": {
"FROM_DEVCONTAINER_JSON": "bar"
"FROM_REMOTE_ENV": "baz"
}
}`,
".devcontainer/Dockerfile": "FROM alpine:latest\nENV FROM_DOCKERFILE=foo",
Expand All @@ -445,7 +448,8 @@ func TestExportEnvFile(t *testing.T) {
output := execContainer(t, ctr, "cat /env")
require.Contains(t, strings.TrimSpace(output),
`FROM_DOCKERFILE=foo
FROM_DEVCONTAINER_JSON=bar`)
FROM_CONTAINER_ENV=bar
FROM_REMOTE_ENV=baz`)
}

func TestLifecycleScripts(t *testing.T) {
Expand Down

0 comments on commit c83d7b6

Please sign in to comment.