Skip to content

Commit

Permalink
add support for running inside vscode devcontainer
Browse files Browse the repository at this point in the history
When running inside a vscode devcontainer, git credential settings are
injected using a modified .gitconfig file. In addition, the injected
credential handler needs access to environment variables like
REMOTE_CONTAINERS_IPC.

As all this is rather undocumented territory, we first detect if we are
running inside a remotecontainers environment and then forward all
REMOTE_CONTAINERS_* variables.

Xref: https://patchwork.yoctoproject.org/project/bitbake/patch/[email protected]/

Closes: #101
Proposed-by: Matthias Schnelte <[email protected]>
Signed-off-by: Felix Moessbauer <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
fmoessbauer authored and jan-kiszka committed Sep 26, 2024
1 parent 13a2fff commit f537669
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/command-line/environment-variables.inc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ Variables Glossary
| (K) | automatically imported. |
| | For details, see ``GITCONFIG_FILE``. |
+--------------------------+--------------------------------------------------+
| ``REMOTE_CONTAINERS`` (K)| Environment variables related to VSCode Remote |
| ``REMOTE_CONTAINERS_<x>``| Containers. If running in this environment, |
| (K,E) | `.gitconfig` is automatically imported. |
+--------------------------+--------------------------------------------------+
| ``BB_NUMBER_THREADS`` | Environment variables to control the concurrency.|
| ``PARALLEL_MAKE`` | |
| (C,K,E) | |
Expand Down
11 changes: 11 additions & 0 deletions kas/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ class ManagedEnvironment(Enum):
"""
GITHUB_ACTIONS = 1
GITLAB_CI = 2
VSCODE_REMOTE_CONTAINERS = 3

def __str__(self):
if self == self.GITHUB_ACTIONS:
return 'GitHub Actions'
if self == self.GITLAB_CI:
return 'GitLab CI'
if self == self.VSCODE_REMOTE_CONTAINERS:
return 'VSCode Remote Containers'
return f'{self.name}'


Expand Down Expand Up @@ -133,6 +136,12 @@ def setup_initial_environ(self):
if val:
self.environ[key] = val

# make remote containers environment available in kas
if self.managed_env == ManagedEnvironment.VSCODE_REMOTE_CONTAINERS:
for k in os.environ.keys():
if k.startswith('REMOTE_CONTAINERS_'):
self.environ[k] = os.environ[k]

@staticmethod
def _get_managed_env():
"""
Expand All @@ -143,6 +152,8 @@ def _get_managed_env():
return ManagedEnvironment.GITHUB_ACTIONS
if os.environ.get('GITLAB_CI', False) == 'true':
return ManagedEnvironment.GITLAB_CI
if os.environ.get('REMOTE_CONTAINERS', False) == 'true':
return ManagedEnvironment.VSCODE_REMOTE_CONTAINERS
return None

@property
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
'CI_SERVER_HOST',
'CI_JOB_TOKEN',
'GITLAB_CI',
'GITHUB_ACTIONS'
'GITHUB_ACTIONS',
'REMOTE_CONTAINERS'
]

ENVVARS_TOOLS = [
Expand All @@ -56,6 +57,10 @@ def monkeykas(monkeypatch, tmpdir):
homedir.mkdir()
monkeypatch.setenv('HOME', str(homedir))

# remove all VSCode devcontainers related variables
for var in os.environ.keys():
if var.startswith('REMOTE_CONTAINERS_'):
monkeypatch.delenv(var)
# remove all git related variables
for var in os.environ.keys():
if var.startswith('GIT_'):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,11 @@ def test_managed_env_detection(monkeykas):
me = ctx.managed_env
assert bool(me)
assert str(me) == 'GitHub Actions'
with monkeykas.context() as mp:
mp.setenv('REMOTE_CONTAINERS', 'true')
mp.setenv('REMOTE_CONTAINERS_FOO', 'bar')
ctx = create_global_context([])
me = ctx.managed_env
assert bool(me)
assert str(me) == 'VSCode Remote Containers'
assert ctx.environ['REMOTE_CONTAINERS_FOO'] == 'bar'

0 comments on commit f537669

Please sign in to comment.