Skip to content

Commit aa93b25

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 c469291 commit aa93b25

8 files changed

Lines changed: 61 additions & 22 deletions

File tree

scripts/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
22
set -euxo pipefail
33

4+
SCRIPT_PATH=$(readlink -f "$0")
5+
SCRIPT_DIR=$(dirname -- "${SCRIPT_PATH}")
6+
47
if [[ "$#" -lt 1 || "$1" != "--arm64" && "$1" != "--amd64" ]]; then
58
echo "Error: First parameter must be --arm64 or --amd64."
69
exit 1
@@ -32,6 +35,9 @@ for LABEL in "${LABELS[@]}"; do
3235
IMAGES+=("--image-name \"ghcr.io/eclipse-score/devcontainer:${LABEL}-${ARCH}\"")
3336
done
3437

38+
. "${SCRIPT_DIR}/functions.sh"
39+
set_dockerfile_name
40+
3541
# Prepare devcontainer build command
3642
DEVCONTAINER_CALL="devcontainer build --workspace-folder src/s-core-devcontainer --cache-from ghcr.io/eclipse-score/devcontainer"
3743

scripts/functions.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set_dockerfile_name() {
4+
DEVCONTAINER_DOCKERFILE_NAME="Dockerfile"
5+
6+
# Check if proxies are configured in the environment
7+
set +u
8+
if [ -n "${HTTP_PROXY}${HTTPS_PROXY}${http_proxy}${https_proxy}${NO_PROXY}${no_proxy}" ]; then
9+
DEVCONTAINER_DOCKERFILE_NAME="Dockerfile-with-proxy-vars"
10+
echo "Proxy environment detected."
11+
fi
12+
set -u
13+
14+
export DEVCONTAINER_DOCKERFILE_NAME
15+
echo "Using Dockerfile: ${DEVCONTAINER_DOCKERFILE_NAME}"
16+
}

scripts/test.sh

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

13+
. "${SCRIPT_DIR}/functions.sh"
14+
set_dockerfile_name
15+
1316
devcontainer up \
1417
--id-label "${ID_LABEL}" \
1518
--workspace-folder "${PROJECT_DIR}/src/${IMAGE}/" \
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
FROM buildpack-deps:noble-curl
22

3-
# Proxy arguments for build-time network access
4-
ARG HTTP_PROXY=""
5-
ARG HTTPS_PROXY=""
6-
ARG http_proxy=""
7-
ARG https_proxy=""
8-
ARG NO_PROXY=""
9-
ARG no_proxy=""
10-
11-
# Set proxy environment variables for the build process
12-
ENV HTTP_PROXY=${HTTP_PROXY}
13-
ENV HTTPS_PROXY=${HTTPS_PROXY}
14-
ENV http_proxy=${http_proxy}
15-
ENV https_proxy=${https_proxy}
16-
ENV NO_PROXY=${NO_PROXY}
17-
ENV no_proxy=${no_proxy}
18-
193
LABEL dev.containers.features="common"
204

21-
# Unset proxy variables for all login shells
22-
COPY unset-proxy.sh /etc/profile.d/unset-proxy.sh
23-
RUN chmod +x /etc/profile.d/unset-proxy.sh
24-
255
RUN userdel -f -r ubuntu
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM buildpack-deps:noble-curl
2+
3+
# Proxy arguments for build-time network access
4+
ARG HTTP_PROXY=""
5+
ARG HTTPS_PROXY=""
6+
ARG http_proxy=""
7+
ARG https_proxy=""
8+
ARG NO_PROXY=""
9+
ARG no_proxy=""
10+
11+
# Set proxy environment variables for the build process
12+
ENV HTTP_PROXY=${HTTP_PROXY}
13+
ENV HTTPS_PROXY=${HTTPS_PROXY}
14+
ENV http_proxy=${http_proxy}
15+
ENV https_proxy=${https_proxy}
16+
ENV NO_PROXY=${NO_PROXY}
17+
ENV no_proxy=${no_proxy}
18+
19+
LABEL dev.containers.features="common"
20+
21+
# Unset proxy variables for all login shells
22+
COPY unset-proxy.sh /etc/bash_completion.d/unset-proxy.sh
23+
24+
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

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
# /etc/profile.d/unset-proxy.sh
23
# Unset proxy variables for all login shells if they are empty
34
for var in HTTP_PROXY HTTPS_PROXY http_proxy https_proxy NO_PROXY no_proxy; do

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
SCRIPT_PATH=$(readlink -f "$0")
55
SCRIPT_DIR=$(dirname -- "${SCRIPT_PATH}")
66

7-
source "test-utils.sh" vscode
7+
source "$SCRIPT_DIR/test-utils.sh" vscode
88

99
# C++ tooling
1010
check "validate clangd is working and has the correct version" bash -c "clangd --version | grep '20.1.8'"
@@ -25,5 +25,14 @@ source /devcontainer/features/s-core-local/tests/test_default.sh
2525
# Tests from the local bazel feature
2626
source /devcontainer/features/bazel/tests/test_default.sh
2727

28+
# Check that no environment variables are empty
29+
. /etc/bash_completion
30+
for var in $(compgen -e); do
31+
if [[ "$var" == "LS_COLORS" ]]; then
32+
continue
33+
fi
34+
check "validate environment variable $var is not empty" bash -c "[ -n \"\${$var}\" ]"
35+
done
36+
2837
# Report result
2938
reportResults

0 commit comments

Comments
 (0)