Skip to content

Commit 0fdd72f

Browse files
committed
Only set proxy environment variables in proxy environments
The approach using /etc/profile.d/ to unset empty variables does not work, because it is never executed in the devcontainer. The proposed solution should be more robust, because now the variables are never present, if not needed and the build will fail, if it is broken. Files in /etc/bash_completion.d/ will be executed whenever a new bash instance is spawned and should work as a /etc/profile.d/ replacement.
1 parent 74c2964 commit 0fdd72f

8 files changed

Lines changed: 92 additions & 22 deletions

File tree

scripts/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
set -euxo pipefail
1818

19+
SCRIPT_PATH=$(readlink -f "$0")
20+
SCRIPT_DIR=$(dirname -- "${SCRIPT_PATH}")
21+
1922
if [[ "$#" -lt 1 || "${1}" != "--arm64" && "${1}" != "--amd64" ]]; then
2023
echo "Error: First parameter must be --arm64 or --amd64."
2124
exit 1
@@ -47,6 +50,9 @@ for LABEL in "${LABELS[@]}"; do
4750
IMAGES+=("--image-name \"ghcr.io/eclipse-score/devcontainer:${LABEL}-${ARCH}\"")
4851
done
4952

53+
. "${SCRIPT_DIR}/functions.sh"
54+
set_dockerfile_name
55+
5056
# Prepare devcontainer build command
5157
DEVCONTAINER_CALL="devcontainer build --workspace-folder src/s-core-devcontainer --cache-from ghcr.io/eclipse-score/devcontainer"
5258

scripts/functions.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# *******************************************************************************
4+
# Copyright (c) 2026 Contributors to the Eclipse Foundation
5+
#
6+
# See the NOTICE file(s) distributed with this work for additional
7+
# information regarding copyright ownership.
8+
#
9+
# This program and the accompanying materials are made available under the
10+
# terms of the Apache License Version 2.0 which is available at
11+
# https://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# SPDX-FileCopyrightText: 2026 Contributors to the Eclipse Foundation
14+
# SPDX-License-Identifier: Apache-2.0
15+
# *******************************************************************************
16+
17+
set_dockerfile_name() {
18+
DEVCONTAINER_DOCKERFILE_NAME="Dockerfile"
19+
20+
# Check if proxies are configured in the environment
21+
set +u
22+
if [ -n "${HTTP_PROXY}${HTTPS_PROXY}${http_proxy}${https_proxy}${NO_PROXY}${no_proxy}" ]; then
23+
DEVCONTAINER_DOCKERFILE_NAME="Dockerfile-with-proxy-vars"
24+
echo "Proxy environment detected."
25+
fi
26+
set -u
27+
28+
export DEVCONTAINER_DOCKERFILE_NAME
29+
echo "Using Dockerfile: ${DEVCONTAINER_DOCKERFILE_NAME}"
30+
}

scripts/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ SCRIPT_DIR=$(dirname -- "${SCRIPT_PATH}")
2525
PROJECT_DIR=$(dirname -- "${SCRIPT_DIR}")
2626
ID_LABEL="test-container=${IMAGE}"
2727

28+
. "${SCRIPT_DIR}/functions.sh"
29+
set_dockerfile_name
30+
2831
devcontainer up \
2932
--id-label "${ID_LABEL}" \
3033
--workspace-folder "${PROJECT_DIR}/src/${IMAGE}/" \

src/s-core-devcontainer/.devcontainer/Dockerfile

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,6 @@
1414

1515
FROM buildpack-deps:noble-curl
1616

17-
# Proxy arguments for build-time network access
18-
ARG HTTP_PROXY=""
19-
ARG HTTPS_PROXY=""
20-
ARG http_proxy=""
21-
ARG https_proxy=""
22-
ARG NO_PROXY=""
23-
ARG no_proxy=""
24-
25-
# Set proxy environment variables for the build process
26-
ENV HTTP_PROXY=${HTTP_PROXY}
27-
ENV HTTPS_PROXY=${HTTPS_PROXY}
28-
ENV http_proxy=${http_proxy}
29-
ENV https_proxy=${https_proxy}
30-
ENV NO_PROXY=${NO_PROXY}
31-
ENV no_proxy=${no_proxy}
32-
3317
LABEL dev.containers.features="common"
3418

35-
# Unset proxy variables for all login shells
36-
COPY unset-proxy.sh /etc/profile.d/unset-proxy.sh
37-
3819
RUN userdel -f -r ubuntu
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-FileCopyrightText: 2026 Contributors to the Eclipse Foundation
12+
# SPDX-License-Identifier: Apache-2.0
13+
# *******************************************************************************
14+
15+
FROM buildpack-deps:noble-curl
16+
17+
# Proxy arguments for build-time network access
18+
ARG HTTP_PROXY=""
19+
ARG HTTPS_PROXY=""
20+
ARG http_proxy=""
21+
ARG https_proxy=""
22+
ARG NO_PROXY=""
23+
ARG no_proxy=""
24+
25+
# Set proxy environment variables for the build process
26+
ENV HTTP_PROXY=${HTTP_PROXY}
27+
ENV HTTPS_PROXY=${HTTPS_PROXY}
28+
ENV http_proxy=${http_proxy}
29+
ENV https_proxy=${https_proxy}
30+
ENV NO_PROXY=${NO_PROXY}
31+
ENV no_proxy=${no_proxy}
32+
33+
LABEL dev.containers.features="common"
34+
35+
# Unset proxy variables for all login shells
36+
COPY unset-proxy.sh /etc/bash_completion.d/unset-proxy.sh
37+
38+
RUN userdel -f -r ubuntu

src/s-core-devcontainer/.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"build": {
33
// Installs latest version from the Distribution
4-
"dockerfile": "./Dockerfile",
4+
"dockerfile": "./${localEnv:DEVCONTAINER_DOCKERFILE_NAME:Dockerfile}",
55
"context": ".",
66
"args": {
77
"HTTP_PROXY": "${localEnv:HTTP_PROXY}",

src/s-core-devcontainer/.devcontainer/unset-proxy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# SPDX-License-Identifier: Apache-2.0
1515
# *******************************************************************************
1616

17-
# /etc/profile.d/unset-proxy.sh
17+
# /etc/bash_completion.d/unset-proxy.sh
1818
# Unset proxy variables for all login shells if they are empty
1919
for var in HTTP_PROXY HTTPS_PROXY http_proxy https_proxy NO_PROXY no_proxy; do
2020
if [ -z "${!var}" ]; then

src/s-core-devcontainer/test-project/test.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
set -euo pipefail
1818

19-
source "test-utils.sh" vscode
19+
SCRIPT_PATH=$(readlink -f "$0")
20+
SCRIPT_DIR=$(dirname -- "${SCRIPT_PATH}")
21+
22+
source "${SCRIPT_DIR}/test-utils.sh" vscode
2023

2124
# C++ tooling
2225
check "validate clangd is working and has the correct version" bash -c "clangd --version | grep '20.1.8'"
@@ -37,5 +40,14 @@ source /devcontainer/features/s-core-local/tests/test_default.sh
3740
# Tests from the local bazel feature
3841
source /devcontainer/features/bazel/tests/test_default.sh
3942

43+
# Check that no environment variables are empty
44+
. /etc/bash_completion
45+
for var in $(compgen -e); do
46+
if [[ "${var}" == "LS_COLORS" ]]; then
47+
continue
48+
fi
49+
check "validate environment variable ${var} is not empty" bash -c "[ -n \"\${${var}}\" ]"
50+
done
51+
4052
# Report result
4153
reportResults

0 commit comments

Comments
 (0)