@@ -6572,9 +6572,13 @@ class GitAuthHelper {
6572
6572
yield this.configureToken();
6573
6573
});
6574
6574
}
6575
- configureGlobalAuth( ) {
6576
- var _a;
6575
+ configureTempGlobalConfig(repositoryPath ) {
6576
+ var _a, _b ;
6577
6577
return __awaiter(this, void 0, void 0, function* () {
6578
+ // Already setup global config
6579
+ if (((_a = this.temporaryHomePath) === null || _a === void 0 ? void 0 : _a.length) > 0) {
6580
+ return path.join(this.temporaryHomePath, '.gitconfig');
6581
+ }
6578
6582
// Create a temp home directory
6579
6583
const runnerTemp = process.env['RUNNER_TEMP'] || '';
6580
6584
assert.ok(runnerTemp, 'RUNNER_TEMP is not defined');
@@ -6590,7 +6594,7 @@ class GitAuthHelper {
6590
6594
configExists = true;
6591
6595
}
6592
6596
catch (err) {
6593
- if (((_a = err) === null || _a === void 0 ? void 0 : _a .code) !== 'ENOENT') {
6597
+ if (((_b = err) === null || _b === void 0 ? void 0 : _b .code) !== 'ENOENT') {
6594
6598
throw err;
6595
6599
}
6596
6600
}
@@ -6601,10 +6605,25 @@ class GitAuthHelper {
6601
6605
else {
6602
6606
yield fs.promises.writeFile(newGitConfigPath, '');
6603
6607
}
6608
+ // Override HOME
6609
+ core.info(`Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes`);
6610
+ this.git.setEnvironmentVariable('HOME', this.temporaryHomePath);
6611
+ // Setup the workspace as a safe directory, so if we pass this into a container job with a different user it doesn't fail
6612
+ // Otherwise all git commands we run in a container fail
6613
+ core.info(`Adding working directory to the temporary git global config as a safe directory`);
6614
+ yield this.git
6615
+ .config('safe.directory', repositoryPath !== null && repositoryPath !== void 0 ? repositoryPath : this.settings.repositoryPath, true, true)
6616
+ .catch(error => {
6617
+ core.info(`Failed to initialize safe directory with error: ${error}`);
6618
+ });
6619
+ return newGitConfigPath;
6620
+ });
6621
+ }
6622
+ configureGlobalAuth() {
6623
+ return __awaiter(this, void 0, void 0, function* () {
6624
+ // noops if already set, just returns the path
6625
+ const newGitConfigPath = yield this.configureTempGlobalConfig();
6604
6626
try {
6605
- // Override HOME
6606
- core.info(`Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes`);
6607
- this.git.setEnvironmentVariable('HOME', this.temporaryHomePath);
6608
6627
// Configure the token
6609
6628
yield this.configureToken(newGitConfigPath, true);
6610
6629
// Configure HTTPS instead of SSH
@@ -6657,11 +6676,14 @@ class GitAuthHelper {
6657
6676
yield this.removeToken();
6658
6677
});
6659
6678
}
6660
- removeGlobalAuth() {
6679
+ removeGlobalConfig() {
6680
+ var _a;
6661
6681
return __awaiter(this, void 0, void 0, function* () {
6662
- core.debug(`Unsetting HOME override`);
6663
- this.git.removeEnvironmentVariable('HOME');
6664
- yield io.rmRF(this.temporaryHomePath);
6682
+ if (((_a = this.temporaryHomePath) === null || _a === void 0 ? void 0 : _a.length) > 0) {
6683
+ core.debug(`Unsetting HOME override`);
6684
+ this.git.removeEnvironmentVariable('HOME');
6685
+ yield io.rmRF(this.temporaryHomePath);
6686
+ }
6665
6687
});
6666
6688
}
6667
6689
configureSsh() {
@@ -7326,40 +7348,48 @@ function getSource(settings) {
7326
7348
core.startGroup('Getting Git version info');
7327
7349
const git = yield getGitCommandManager(settings);
7328
7350
core.endGroup();
7329
- // Prepare existing directory, otherwise recreate
7330
- if (isExisting) {
7331
- yield gitDirectoryHelper.prepareExistingDirectory(git, settings.repositoryPath, repositoryUrl, settings.clean, settings.ref);
7332
- }
7333
- if (!git) {
7334
- // Downloading using REST API
7335
- core.info(`The repository will be downloaded using the GitHub REST API`);
7336
- core.info(`To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH`);
7337
- if (settings.submodules) {
7338
- throw new Error(`Input 'submodules' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`);
7351
+ let authHelper = null;
7352
+ try {
7353
+ if (git) {
7354
+ authHelper = gitAuthHelper.createAuthHelper(git, settings);
7355
+ yield authHelper.configureTempGlobalConfig();
7339
7356
}
7340
- else if (settings.sshKey) {
7341
- throw new Error(`Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`);
7357
+ // Prepare existing directory, otherwise recreate
7358
+ if (isExisting) {
7359
+ yield gitDirectoryHelper.prepareExistingDirectory(git, settings.repositoryPath, repositoryUrl, settings.clean, settings.ref);
7360
+ }
7361
+ if (!git) {
7362
+ // Downloading using REST API
7363
+ core.info(`The repository will be downloaded using the GitHub REST API`);
7364
+ core.info(`To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH`);
7365
+ if (settings.submodules) {
7366
+ throw new Error(`Input 'submodules' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`);
7367
+ }
7368
+ else if (settings.sshKey) {
7369
+ throw new Error(`Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`);
7370
+ }
7371
+ yield githubApiHelper.downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath);
7372
+ return;
7373
+ }
7374
+ // Save state for POST action
7375
+ stateHelper.setRepositoryPath(settings.repositoryPath);
7376
+ // Initialize the repository
7377
+ if (!fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))) {
7378
+ core.startGroup('Initializing the repository');
7379
+ yield git.init();
7380
+ yield git.remoteAdd('origin', repositoryUrl);
7381
+ core.endGroup();
7382
+ }
7383
+ // Disable automatic garbage collection
7384
+ core.startGroup('Disabling automatic garbage collection');
7385
+ if (!(yield git.tryDisableAutomaticGarbageCollection())) {
7386
+ core.warning(`Unable to turn off git automatic garbage collection. The git fetch operation may trigger garbage collection and cause a delay.`);
7342
7387
}
7343
- yield githubApiHelper.downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath);
7344
- return;
7345
- }
7346
- // Save state for POST action
7347
- stateHelper.setRepositoryPath(settings.repositoryPath);
7348
- // Initialize the repository
7349
- if (!fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))) {
7350
- core.startGroup('Initializing the repository');
7351
- yield git.init();
7352
- yield git.remoteAdd('origin', repositoryUrl);
7353
7388
core.endGroup();
7354
- }
7355
- // Disable automatic garbage collection
7356
- core.startGroup('Disabling automatic garbage collection');
7357
- if (!(yield git.tryDisableAutomaticGarbageCollection())) {
7358
- core.warning(`Unable to turn off git automatic garbage collection. The git fetch operation may trigger garbage collection and cause a delay.`);
7359
- }
7360
- core.endGroup();
7361
- const authHelper = gitAuthHelper.createAuthHelper(git, settings);
7362
- try {
7389
+ // If we didn't initialize it above, do it now
7390
+ if (!authHelper) {
7391
+ authHelper = gitAuthHelper.createAuthHelper(git, settings);
7392
+ }
7363
7393
// Configure auth
7364
7394
core.startGroup('Setting up auth');
7365
7395
yield authHelper.configureAuth();
@@ -7415,27 +7445,21 @@ function getSource(settings) {
7415
7445
core.endGroup();
7416
7446
// Submodules
7417
7447
if (settings.submodules) {
7418
- try {
7419
- // Temporarily override global config
7420
- core.startGroup('Setting up auth for fetching submodules');
7421
- yield authHelper.configureGlobalAuth();
7422
- core.endGroup();
7423
- // Checkout submodules
7424
- core.startGroup('Fetching submodules');
7425
- yield git.submoduleSync(settings.nestedSubmodules);
7426
- yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules);
7427
- yield git.submoduleForeach('git config --local gc.auto 0', settings.nestedSubmodules);
7448
+ // Temporarily override global config
7449
+ core.startGroup('Setting up auth for fetching submodules');
7450
+ yield authHelper.configureGlobalAuth();
7451
+ core.endGroup();
7452
+ // Checkout submodules
7453
+ core.startGroup('Fetching submodules');
7454
+ yield git.submoduleSync(settings.nestedSubmodules);
7455
+ yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules);
7456
+ yield git.submoduleForeach('git config --local gc.auto 0', settings.nestedSubmodules);
7457
+ core.endGroup();
7458
+ // Persist credentials
7459
+ if (settings.persistCredentials) {
7460
+ core.startGroup('Persisting credentials for submodules');
7461
+ yield authHelper.configureSubmoduleAuth();
7428
7462
core.endGroup();
7429
- // Persist credentials
7430
- if (settings.persistCredentials) {
7431
- core.startGroup('Persisting credentials for submodules');
7432
- yield authHelper.configureSubmoduleAuth();
7433
- core.endGroup();
7434
- }
7435
- }
7436
- finally {
7437
- // Remove temporary global config override
7438
- yield authHelper.removeGlobalAuth();
7439
7463
}
7440
7464
}
7441
7465
// Get commit information
@@ -7447,10 +7471,13 @@ function getSource(settings) {
7447
7471
}
7448
7472
finally {
7449
7473
// Remove auth
7450
- if (!settings.persistCredentials) {
7451
- core.startGroup('Removing auth');
7452
- yield authHelper.removeAuth();
7453
- core.endGroup();
7474
+ if (authHelper) {
7475
+ if (!settings.persistCredentials) {
7476
+ core.startGroup('Removing auth');
7477
+ yield authHelper.removeAuth();
7478
+ core.endGroup();
7479
+ }
7480
+ authHelper.removeGlobalConfig();
7454
7481
}
7455
7482
}
7456
7483
});
@@ -7472,7 +7499,13 @@ function cleanup(repositoryPath) {
7472
7499
}
7473
7500
// Remove auth
7474
7501
const authHelper = gitAuthHelper.createAuthHelper(git);
7475
- yield authHelper.removeAuth();
7502
+ try {
7503
+ yield authHelper.configureTempGlobalConfig(repositoryPath);
7504
+ yield authHelper.removeAuth();
7505
+ }
7506
+ finally {
7507
+ yield authHelper.removeGlobalConfig();
7508
+ }
7476
7509
});
7477
7510
}
7478
7511
exports.cleanup = cleanup;
0 commit comments