How to actually clean up the repository on self-hosted runner after GitHub Actions completes?

Viewed 3682

At the end of my workflow in GitHub actions, a "hidden" "post" job seems to run, and I can't seem to figure out how to ignore or overwrite it. For example, the actions/checkout@v2 action seems to run the following when it's complete:

Post job cleanup.
/usr/bin/git version
git version 2.25.1
/usr/bin/git config --local --name-only --get-regexp core\.sshCommand
/usr/bin/git submodule foreach --recursive git config --local --name-only --get-regexp 'core\.sshCommand' && git config --local --unset-all 'core.sshCommand' || :
/usr/bin/git config --local --name-only --get-regexp http\.https\:\/\/github\.com\/\.extraheader
http.https://github.com/.extraheader
/usr/bin/git config --local --unset-all http.https://github.com/.extraheader
/usr/bin/git submodule foreach --recursive git config --local --name-only --get-regexp 'http\.https\:\/\/github\.com\/\.extraheader' && git config --local --unset-all 'http.https://github.com/.extraheader' || :

However, I just want to simply remove the entire repository from my self-hosted runner. Just a simple rm -rf in the directory.

I tried to just add this as a step in my .yml file, but that seems to break the actions/checkout@v2 step that runs after my entire workflow is finished.

How can I clean up the repository entirely on the self-hosted running after the workflow is complete?

2 Answers

I was able to remove the entire repository before actions/checkout@v2 using this:

   steps:

    - name: 'Cleanup build folder'
      run: |
        ls -la ./
        rm -rf ./* || true
        rm -rf ./.??* || true
        ls -la ./

    - uses: actions/checkout@v2

I've added the ls -la ./ before and after, so I could double-check in the action log if everything was really gone.

The trick is to delete all .??* files as well, since rm -rf ./* won't remove files that start with dot, which meas the .git folder won't be deleted.

By running rm -rf ./.??* we remove all hidden files that have a dot as first character, including the .git folder, and then actions/checkout@v2 will clone the repository properly, just like when running on github runners.

-H

Looking at the source of checkout action, I don't see a way to override post job

Another approach is possible since Apr. 2022 (still in beta for now):

GitHub Actions: Job management hooks for self-hosted runners

If you manage self-hosted runners for GitHub Actions, you can now specify shell scripts that run before the runner starts running a job from a workflow, and after a job completes.

This allows you to perform a task on your self-hosted runner before a job starts and after a job ends, so you can set up your execution environment and clean up after workflow runs to ensure a consistent state on the runner itself, without requiring users to add that to their workflows.

Learn more about running scripts before or after a job

In your case, the "execution environment clean up" step can now be a pre-job script.

Related