Skip to content

Commit

Permalink
GitLab CI: add git safe dir for project root
Browse files Browse the repository at this point in the history
The GitLab CI runner mounts the $CI_PROJECT_DIR git project root from
the outside but does not align the ownership of that directory with the
user of the docker container. By that, git does not allow to perform any
operation on that repository.

As this is a well-known case, we mark that directory as a safe dir if
running in GitLab CI.

Xref: siemens#141
Signed-off-by: Felix Moessbauer <[email protected]>
  • Loading branch information
fmoessbauer committed Feb 28, 2025
1 parent 2c29ec6 commit ed2fca7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions kas/libcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,19 @@ def _setup_gitconfig(self):
if os.environ.get('GIT_CREDENTIAL_USEHTTPPATH', False):
config['credential']['useHttpPath'] = \
os.environ.get('GIT_CREDENTIAL_USEHTTPPATH')
# in GitLab CI, add ssh -> https rewrites if no config is present
ci_server = os.environ.get('CI_SERVER_HOST', None)
if get_context().managed_env == ME.GITLAB_CI and ci_server and \
not self._ssh_config_present() and \

if get_context().managed_env == ME.GITLAB_CI and \
not os.path.exists(gitconfig_host):
logging.debug('Adding GitLab CI ssh -> https rewrites')
self._setup_gitlab_ci_ssh_rewrite(config)
ci_project_dir = os.environ.get('CI_PROJECT_DIR', False)
if ci_project_dir:
logging.debug('Adding git safe.directory %s',
ci_project_dir)
config.add_value('safe', 'directory', ci_project_dir)

ci_server = os.environ.get('CI_SERVER_HOST', None)
if ci_server and not self._ssh_config_present():
logging.debug('Adding GitLab CI ssh -> https rewrites')
self._setup_gitlab_ci_ssh_rewrite(config)
config.write()

def execute(self, ctx):
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'SSH_AUTH_SOCK',
'CI_SERVER_HOST',
'CI_JOB_TOKEN',
'CI_PROJECT_DIR',
'GITLAB_CI',
'GITHUB_ACTIONS',
'REMOTE_CONTAINERS'
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_checkout_with_ci_rewrite(monkeykas, tmpdir):
mp.setenv('GITLAB_CI', 'true')
mp.setenv('CI_SERVER_HOST', 'github.com')
mp.setenv('CI_JOB_TOKEN', 'not-needed')
mp.setenv('CI_PROJECT_DIR', '/build')
kas.kas(['checkout', 'test-url-rewrite.yml'])


Expand Down

0 comments on commit ed2fca7

Please sign in to comment.