Delete the shared agent workspace each time the agent is leased

1 minute readKnowledge base

Issue

A common way for clients that desire a clean build agent workspace for every build would be to utilize an ephemeral agent architecture, for example using Kubernetes plugin.

If you have specialized hardware that is required for your build, and not enough of that hardware to dedicate to each controller in your cluster, you likely will want to utilize Shared agents. However, you may still want to have a clean workspace for each build.

Resolution

There are two solutions to this problem:

  1. Have developers to update their Jenkinsfile to add the deleteDir() step at the start of the Pipeline.

  2. Update the shared agent configuration to delete the workspace each time the agent is leased:

    1. Under the Launch method choose Launch build agents via SSH (Non-blocking I/O)

    2. Under the Advanced section, under Prefix Start Agent Command add the command: rm -rf /home/agent/*/workspace/ && (note to modify /home/agent to be the path you chose for the Remote FS root)

This /home/agent/*/workspace/ has a * in it, since for shared agents, each controller will get a random ID directory under the Remote FS root and the workspace is a subdirectory below that directory (if it was a permanent agent directly connected to a controller, the command would be rm -rf /home/agent/workspace/ && )

Tested product/plugin versions