Creating an empty test folder in the Jenkins workspace is straightforward using either a simple shell command or a dedicated plugin. The most reliable method involves adding a build step to execute
mkdir -p for your desired directory path, ensuring it exists before any test scripts run. This guarantees a clean slate for your automation framework without manual intervention.
Understanding the Workspace Context
Every Jenkins job operates within a specific workspace directory on the agent machine where the build takes place. This location is dynamic and changes with each job configuration. When you need to structure your project, such as separating source code from test artifacts, creating directories programmatically is essential. Unlike local development environments where you might manually create folders, CI/CD pipelines require explicit instructions to establish this structure at runtime.
The workspace is typically located at
${WORKSPACE} relative to the current build context. If you are running tests that expect a specific folder structure, such as
test-results or
output, failing to create these directories can cause script failures. For instance, Python or Java test runners often throw exceptions if they attempt to write logs to a non-existent path. Therefore, establishing this hierarchy early in the build process prevents downstream errors.
Method 1: Using Shell Scripts
The most common approach for Linux-based agents is to use a shell execution step. This method provides immediate control over the file system and requires no additional plugins. You can add a "Execute shell" build step at the beginning of your pipeline or freestyle project. Inside the script, use the
mkdir command with the
-p flag. This flag ensures that parent directories are created if they do not exist and suppresses errors if the directory already exists.
For example, to create a folder named
tests inside your workspace, you would enter the following command:
mkdir -p ${WORKSPACE}/tests
This command is robust because it handles nested paths automatically. If you need deeper structures like
tests/unit and
tests/integration, the same command works seamlessly. After execution, you can verify the creation by listing the contents of the workspace using
ls -la. This visual confirmation helps debug issues during initial setup.
If you are using Windows agents, the syntax differs slightly. You should use the PowerShell or Command Prompt build steps instead. In PowerShell, the equivalent command is
New-Item -ItemType Directory -Force -Path "${env:WORKSPACE}\tests". The
-Force parameter mimics the behavior of
-p in Unix systems, ensuring idempotent execution. Always ensure your agent has the necessary permissions to write to the workspace directory.
Method 2: Using Pipeline Syntax
For teams utilizing Jenkins Pipelines (Jenkinsfile), the logic is embedded directly into the Groovy script. This approach offers better version control and reusability across multiple jobs. You can define a stage specifically for environment setup before your actual test execution stage. This keeps your pipeline organized and makes the intent clear to other developers reviewing the code.
In a declarative pipeline, you might structure your code like this:
pipeline {
agent any
stages {
stage('Setup') {
steps {
sh 'mkdir -p ${WORKSPACE}/test-output'
}
}
stage('Run Tests') {
steps {
// Your test execution commands here
sh './run_tests.sh'
}
}
}
}
By isolating the folder creation in its own stage, you can easily add conditional logic later. For example, you might only create the folder if a certain parameter is passed. This modularity is a key advantage of pipeline-as-code over freestyle projects. It also allows you to integrate with other tools more easily, as seen when
passing environmental variable URL strings to Gradle tasks, where setup steps must precede task execution.
Best Practices for Automation
Consistency is critical in automated testing. Relying on manual folder creation leads to drift between environments. Always automate the setup phase. Additionally, consider cleaning up old data. While creating new folders is important, removing stale results from previous runs prevents clutter. You can combine creation and cleanup in a single step using
rm -rf followed by
mkdir -p. However, be cautious with removal commands to avoid deleting unintended files.
Another best practice is to document the expected folder structure in your README or pipeline comments. This helps new team members understand why specific directories exist. It also aids in troubleshooting when tests fail to generate reports. If your test runner expects output in
./reports but you created
./test-reports, the build will fail silently or produce empty artifacts. Clear naming conventions reduce cognitive load and maintenance overhead.
For more advanced automation scenarios, such as integrating with version control systems, ensure your workspace is properly initialized. Understanding how to
connect Git to Jenkins server ensures that your code checkout happens before your folder creation steps, preventing race conditions. If your workflow involves complex batch operations, refer to guides on
batch files and commands to optimize your shell scripts for performance.
Frequently Asked Questions
What happens if the folder already exists?
Using the
-p flag with
mkdir prevents errors if the directory is present. The command becomes idempotent, meaning it does nothing if the target state is already achieved. This safety feature is crucial for continuous integration builds that run repeatedly.
Can I create folders in parallel stages?
Yes, Jenkins allows parallel execution of stages. However, be careful if multiple stages try to create the same directory simultaneously. While usually safe, it is better to centralize setup steps to avoid potential permission conflicts or race conditions.
How do I verify the folder was created?
You can add a subsequent shell step that lists the directory contents using
ls or
dir (on Windows). Checking the console output after the build completes confirms whether the path exists and contains the expected items.
Does this work with Docker agents?
Yes, as long as the container image includes the
mkdir utility. Most standard Linux images include core utilities. Ensure your Dockerfile installs any necessary tools before executing the Jenkins agent.
Should I commit these folders to Git?
No. The Jenkins workspace is ephemeral and typically excluded from version control via
.gitignore. Creating folders in the workspace is a runtime activity, not a source code change. Committing generated directories clutters the repository and causes merge conflicts.