Skip to content

Commit 70c98b3

Browse files
fmoessbauerjan-kiszka
authored andcommitted
GitLab CI: add git safe dir for project root
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: #141 Signed-off-by: Felix Moessbauer <[email protected]> Signed-off-by: Jan Kiszka <[email protected]>
1 parent 5c24e6b commit 70c98b3

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

kas/libcmds.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,19 @@ def _setup_gitconfig(self):
310310
if os.environ.get('GIT_CREDENTIAL_USEHTTPPATH', False):
311311
config['credential']['useHttpPath'] = \
312312
os.environ.get('GIT_CREDENTIAL_USEHTTPPATH')
313-
# in GitLab CI, add ssh -> https rewrites if no config is present
314-
ci_server = os.environ.get('CI_SERVER_HOST', None)
315-
if get_context().managed_env == ME.GITLAB_CI and ci_server and \
316-
not self._ssh_config_present() and \
313+
314+
if get_context().managed_env == ME.GITLAB_CI and \
317315
not os.path.exists(gitconfig_host):
318-
logging.debug('Adding GitLab CI ssh -> https rewrites')
319-
self._setup_gitlab_ci_ssh_rewrite(config)
316+
ci_project_dir = os.environ.get('CI_PROJECT_DIR', False)
317+
if ci_project_dir:
318+
logging.debug('Adding git safe.directory %s',
319+
ci_project_dir)
320+
config.add_value('safe', 'directory', ci_project_dir)
321+
322+
ci_server = os.environ.get('CI_SERVER_HOST', None)
323+
if ci_server and not self._ssh_config_present():
324+
logging.debug('Adding GitLab CI ssh -> https rewrites')
325+
self._setup_gitlab_ci_ssh_rewrite(config)
320326
config.write()
321327

322328
def execute(self, ctx):

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'SSH_AUTH_SOCK',
4040
'CI_SERVER_HOST',
4141
'CI_JOB_TOKEN',
42+
'CI_PROJECT_DIR',
4243
'GITLAB_CI',
4344
'GITHUB_ACTIONS',
4445
'REMOTE_CONTAINERS'

tests/test_commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def test_checkout_with_ci_rewrite(monkeykas, tmpdir):
9898
mp.setenv('GITLAB_CI', 'true')
9999
mp.setenv('CI_SERVER_HOST', 'github.com')
100100
mp.setenv('CI_JOB_TOKEN', 'not-needed')
101+
mp.setenv('CI_PROJECT_DIR', '/build')
101102
kas.kas(['checkout', 'test-url-rewrite.yml'])
102103

103104

0 commit comments

Comments
 (0)