Skip to content

ACTIONS_RUNNER_HOOK_JOB_COMPLETED not able to remove workspace #2154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rzamponiAtIgt opened this issue Sep 26, 2022 · 6 comments
Open

ACTIONS_RUNNER_HOOK_JOB_COMPLETED not able to remove workspace #2154

rzamponiAtIgt opened this issue Sep 26, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@rzamponiAtIgt
Copy link

Describe the bug

Using a simple workflow which just checks out the repository

name: JCO

on:
  workflow_dispatch:

jobs:
  build:
    runs-on: [self-hosted, windows]
    steps: 
      - uses: actions/checkout@v2

a simple powershell script

$time_to_wait = 30
Write-Output "Wait '${time_to_wait}' seconds for the file system to become unlocked."
Start-Sleep -Seconds $time_to_wait
Remove-Item "${env:GITHUB_WORKSPACE}" -Recurse -WhatIf
Remove-Item "${env:RUNNER_WORKSPACE}" -Recurse -WhatIf

called with ACTIONS_RUNNER_HOOK_JOB_COMPLETED is not able to remove neither GITHUB_WORKSPACE nor RUNNER_WORKSPACE.

Runner Version and Platform

Version of your runner?

2.296.2

OS of the machine running the runner? OSX/Windows/Linux/...

Windows

What's not working?

From documentation I would expect to be able to wipeout the workspace at the end of the run but the Remove-Item command tells me, the directory is locked.

Job Log Output

A job completed hook has been configured by the self-hosted runner administrator
Run '<path to>\sample_cleanup.ps1'
shell: C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -command ". '{0}'"
Wait '30' seconds for the file system to become unlocked.
Remove-Item : Cannot remove the item at 'C:\github_work\<repository name>\<repository name>'
because it is in use.
At <path to>\sample_cleanup.ps1:4 char:1

  • Remove-Item "${env:GITHUB_WORKSPACE}" -Recurse -WhatIf
  •   + CategoryInfo          : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
      + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand
    
    

Remove-Item : Cannot remove the item at 'C:\github_work\<repository name>' because it is in use.
At <path to>\sample_cleanup.ps1:5 char:1

  • Remove-Item "${env:RUNNER_WORKSPACE}" -Recurse -WhatIf
  •   + CategoryInfo          : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
      + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand
    

Runner and Worker's Diagnostic Logs

@rzamponiAtIgt rzamponiAtIgt added the bug Something isn't working label Sep 26, 2022
@ruvceskistefan
Copy link
Contributor

Hi @rzamponiAtIgt
Thanks for filling this issue! We will try to reproduce the bug, so I will get back to you as soon as I have some news.

@sesa529917
Copy link

sesa529917 commented Jan 10, 2024

Was the root cause of this ever discovered? I have the same issue on setup runner trying to clean out the working directory on windows with powershell.

@ivo-nussbaumer-sg
Copy link

I'm having the same issues.
Is there an update to this issue?

@chriscarpenter12
Copy link

chriscarpenter12 commented Apr 2, 2024

I was having the same issue, but realized through some testing that the current the script is executed in is the GITHUB_WORKSPACE. It was enough to change to a different directory and then run the deletions. In my case I used the RUNNER_TEMP and went up to the parent directory. Once out of the working directory it was able to be deleted.

#!/usr/bin/env pwsh

$ErrorActionPreference = "Stop"

Write-Host "PWD: $($PWD)"

$temp = $env:RUNNER_TEMP
$work = (Get-Item -Path $temp).Parent.FullName

Write-Host "Work: $($work)"
Set-Location -Path $work
Write-Host "PWD: $($PWD)"

Remove-Item -Path "$($env:RUNNER_WORKSPACE)" -Recurse -Force
Get-Item -Path "$($env:RUNNER_WORKSPACE)" -ErrorAction Continue

image

This could be remedied with by updating the documentation with this specific use case. This seems like a common task for the completed hook.

@jamesc-skyward
Copy link

I'm also experiencing this. @chriscarpenter12's suggestion didn't work for me.

In my findings, it seems like it's the actual GITHUB_WORKSPACE directory that is in use. I can delete the contents of that folder and any sibling folders, but not the actual GITHUB_WORKSPACE folder.

@heathhenley
Copy link

I had the same problem on a local windows runner with powershell job completed hooks. The proposed solution does not work for me. I don't know much about powershell atm but as far as I can tell, I think it's related to this issue PowerShell/PowerShell#17149 - the paths in the solution above look like linux paths so maybe it's a difference in windows / linux behavior with Set-Location.

For me - the script is being started in one of the directories I want to remove (can check with $PWD) - even changing dirs with Set-Location - the powershell process running the script still has a handle to the directory it was originally started in (I used "handle.exe" from "sysinternals" https://learn.microsoft.com/en-us/sysinternals/downloads/handle#installation to see what PID had the folder when delete failed - and it turned out to be the PID of the running script).

The solution that's working for me (which is why I think that it is related to PowerShell/PowerShell#17149 - is to call [System.IO.Directory]::SetCurrentDirectory($newDir) to change directory, as well as Set-Location, before trying to run the delete.

Here's a script that was playing with to figure out what was happening:

$dirToRemove = 'C:\path\to\dir\to\remove'
Write-Output "Originally in: $PWD"
Set-Location -Path 'c:\better\location'
Write-Output "CurrentDirectory:" ([System.IO.Directory]::GetCurrentDirectory())
[System.IO.Directory]::SetCurrentDirectory($PWD)
Write-Output "CurrentDirectory:" ([System.IO.Directory]::GetCurrentDirectory())
Write-Output "My PID: $PID"

if (Test-Path -Path $dirToRemove) {
    try {
        Remove-Item -Path $dirToRemove -Recurse -Force -ErrorAction Stop
        Write-Output "Directory removed successfully with Remove-Item"
    } catch {
        Write-Output "Remove-Item failed..."
        Write-Output $_
        # to see what pid has the file / folder still
        & "C:\Users\USER\Downloads\Handle\handle.exe" $dirToRemove
    }
} else {
    Write-Output "Directory doesn't exist."
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants