1
0
Fork 0
mirror of https://code.forgejo.org/actions/checkout.git synced 2026-04-22 23:59:39 +00:00

fix: fix adding safe-directory in container

This commit is contained in:
Marcus Wirtz 2026-04-21 11:32:17 +02:00
parent 0c366fd6a8
commit a0eb51f8ea
No known key found for this signature in database
5 changed files with 47 additions and 17 deletions

View file

@ -44,20 +44,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
if (git) {
authHelper = gitAuthHelper.createAuthHelper(git, settings)
if (settings.setSafeDirectory) {
// Setup the repository path as a safe directory, so if we pass this into a container job with a different user it doesn't fail
// Otherwise all git commands we run in a container fail
await authHelper.configureTempGlobalConfig()
core.info(
`Adding repository directory to the temporary git global config as a safe directory`
await addSafeDirectory(settings.repositoryPath, git)
const containerPath = getContainerRepositoryPath(
settings.repositoryPath,
settings.githubWorkspacePath
)
await git
.config('safe.directory', settings.repositoryPath, true, true)
.catch(error => {
core.info(
`Failed to initialize safe directory with error: ${error}`
)
})
if (containerPath && containerPath !== settings.repositoryPath) {
await addSafeDirectory(containerPath, git)
}
stateHelper.setSafeDirectory()
}
@ -373,3 +367,37 @@ async function getGitCommandManager(
return undefined
}
}
async function addSafeDirectory(
safeDirectory: string,
git: IGitCommandManager
): Promise<void> {
core.info(`Adding '${safeDirectory}' to the git global config as a safe directory`)
await git.config('safe.directory', safeDirectory, true, true).catch(error => {
core.info(`Failed to initialize safe directory with error: ${error}`)
})
}
function getContainerRepositoryPath(
repositoryPath: string,
githubWorkspace?: string
): string {
if (!githubWorkspace) {
return ''
}
let relativeRepositoryPath = path.relative(githubWorkspace, repositoryPath)
if (!relativeRepositoryPath || relativeRepositoryPath === '.') {
return '/github/workspace'
}
if (
relativeRepositoryPath.startsWith('..') ||
path.isAbsolute(relativeRepositoryPath)
) {
return ''
}
relativeRepositoryPath = relativeRepositoryPath.replace(/\\/g, '/')
return path.posix.join('/github/workspace', relativeRepositoryPath)
}

View file

@ -1,4 +1,9 @@
export interface IGitSourceSettings {
/**
* The workflow workspace path
*/
githubWorkspacePath?: string
/**
* The location on disk where the repository will be placed
*/

View file

@ -14,6 +14,7 @@ export async function getInputs(): Promise<IGitSourceSettings> {
throw new Error('GITHUB_WORKSPACE not defined')
}
githubWorkspacePath = path.resolve(githubWorkspacePath)
result.githubWorkspacePath = githubWorkspacePath
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
fsHelper.directoryExistsSync(githubWorkspacePath, true)