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

@ -314,7 +314,6 @@ jobs:
fi fi
# Verify auth token # Verify auth token
git config --global --add safe.directory "*"
git fetch --no-tags --depth=1 origin +refs/heads/main:refs/remotes/origin/main git fetch --no-tags --depth=1 origin +refs/heads/main:refs/remotes/origin/main
# needed to make checkout post cleanup succeed # needed to make checkout post cleanup succeed

View file

@ -15,9 +15,6 @@ fi
cd "$CHECKOUT_PATH" cd "$CHECKOUT_PATH"
# Add safe directory for container environments
git config --global --add safe.directory "*" 2>/dev/null || true
# Show the includeIf configuration # Show the includeIf configuration
echo "Git config includeIf entries:" echo "Git config includeIf entries:"
git config --list --show-origin | grep -i include || true git config --list --show-origin | grep -i include || true

View file

@ -44,20 +44,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
if (git) { if (git) {
authHelper = gitAuthHelper.createAuthHelper(git, settings) authHelper = gitAuthHelper.createAuthHelper(git, settings)
if (settings.setSafeDirectory) { 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 await addSafeDirectory(settings.repositoryPath, git)
// Otherwise all git commands we run in a container fail const containerPath = getContainerRepositoryPath(
await authHelper.configureTempGlobalConfig() settings.repositoryPath,
core.info( settings.githubWorkspacePath
`Adding repository directory to the temporary git global config as a safe directory`
) )
if (containerPath && containerPath !== settings.repositoryPath) {
await git await addSafeDirectory(containerPath, git)
.config('safe.directory', settings.repositoryPath, true, true) }
.catch(error => {
core.info(
`Failed to initialize safe directory with error: ${error}`
)
})
stateHelper.setSafeDirectory() stateHelper.setSafeDirectory()
} }
@ -373,3 +367,37 @@ async function getGitCommandManager(
return undefined 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 { export interface IGitSourceSettings {
/**
* The workflow workspace path
*/
githubWorkspacePath?: string
/** /**
* The location on disk where the repository will be placed * 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') throw new Error('GITHUB_WORKSPACE not defined')
} }
githubWorkspacePath = path.resolve(githubWorkspacePath) githubWorkspacePath = path.resolve(githubWorkspacePath)
result.githubWorkspacePath = githubWorkspacePath
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`) core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
fsHelper.directoryExistsSync(githubWorkspacePath, true) fsHelper.directoryExistsSync(githubWorkspacePath, true)