Creating and Configuring Freestyle Jobs
1. Creating New Freestyle Job
Example: Create New Freestyle Job via UI
1. Navigate to Jenkins dashboard
2. Click "New Item"
3. Enter item name (e.g., "my-freestyle-job")
4. Select "Freestyle project"
5. Click "OK"
| Field | Description | Notes |
|---|---|---|
| Item Name | Name of the job | Must be unique within Jenkins |
| Description | Optional description for the job | Helps identify job purpose |
| Project Type | Freestyle project | Allows full customization of build steps |
Note: Freestyle jobs support a wide range of build steps including shell scripts, batch files, and Maven builds.
2. Configuring Source Code Management
| Option | Values | Description |
|---|---|---|
| Source Code Management | Git, Subversion, CVS, Perforce |
Choose the version control system to manage your project's source code. |
| Repository URL | https://github.com/example/project.git |
The URL of the repository to clone or check out. |
| Credentials | Username/Password, SSH Private Key |
Authentication method to access the repository. |
| Branches to Build | */main, */feature/* |
Specify which branches to build. Supports wildcards. |
Example: Git Configuration
git clone https://github.com/example/project.git
cd project
git checkout main
Note: Ensure Jenkins has the necessary permissions to access your repository. For private repositories, configure credentials in Jenkins' global configuration.
3. Setting Build Triggers
| Trigger | Description | Configuration | Notes |
|---|---|---|---|
| Build periodically | Triggers a build at specified intervals | SCM or Build Periodically in the job configuration |
Use cron-like syntax for scheduling |
| Poll SCM | Checks for changes in the source control management system | SCM or Poll SCM in the job configuration |
Can be configured with a cron schedule |
| Build when a change is pushed to SCM | Triggers a build on every commit or push to the repository | SCM or Build when a change is pushed to SCM in the job configuration |
Requires polling or webhook support from the SCM system |
| Build after other projects are built | Triggers a build after another project completes successfully | Build after other projects are built in the job configuration |
Can be configured to build only if a project succeeds or fails |
| Build after a delay | Triggers a build after a specified time interval | Build after a delay in the job configuration |
Useful for scheduling builds at specific times or after other events |
Example: Build Periodically
* * * * *
Note: The cron syntax is used to define the schedule for periodic builds.
4. Adding Build Steps
| Step Type | Description | Configuration | Notes |
|---|---|---|---|
| Execute Shell | Run shell commands on the build agent | echo "Building project..." |
Supports Unix/Linux and macOS agents |
| Execute Windows Batch | Run Windows batch commands on the build agent | echo Building project... |
Supports Windows agents |
| Invoke Ant | Execute Apache Ant build scripts | build.xml |
Requires Ant installed on the agent |
| Invoke Gradle | Execute Gradle build scripts | build.gradle |
Requires Gradle installed on the agent |
| Maven | Execute Maven goals | mvn clean install |
Requires Maven installed on the agent |
| Archive Artifacts | Archive files produced by the build | **/*.jar |
Files are stored in Jenkins for later retrieval |
Example: Execute Shell Step
#!/bin/bash
set -e
echo "Starting build..."
npm install
npm test
echo "Build completed successfully."
Note: Build steps are executed in the order they appear in the job configuration. Each step can be configured to run conditionally or fail the build if it returns a non-zero exit code.
5. Configuring Build Environment
| Property | Value | Description |
|---|---|---|
| workspace | workspace |
Directory where the build runs and files are stored |
| node | node('label') |
Specify the node or label to run the build on |
| environment | VAR=value |
Set environment variables for the build |
| properties | buildDiscarder(logRotator(...)) |
Configure build retention policy |
Example: Setting Environment Variables
environment {
JAVA_HOME = "/usr/lib/jvm/java-11-openjdk"
PATH = "${JAVA_HOME}/bin:${PATH}"
}
Note: The
workspace directory is automatically created and managed by Jenkins. It's recommended to avoid modifying it directly.
6. Using Build Parameters
| Parameter Name | Type | Description | Default Value |
|---|---|---|---|
| environmentVariable | String | Environment variable to set | |
| buildNumber | Boolean | Whether to build with a specific number | false |
| gitBranch | String | Git branch to build from | main |
Note: Build parameters allow dynamic configuration of jobs at runtime.
7. Adding Post-Build Actions
| Action | Description | Configuration |
|---|---|---|
| Archive the artifacts | Archives build artifacts for later use. | artifact-pattern: Glob pattern to match files to archive |
| Email Notification | Sends email on build success/failure. | recipients, subject, body |
| Publish JUnit test results report | Reports JUnit test results. | test-results: Path to JUnit XML files |
| Publish HTML reports | Publishes HTML reports (e.g., from SonarQube). | report-dir, index-file |
| Build other projects | Triggers builds of other Jenkins jobs. | projects-to-build, trigger-when |
Example: Email Notification
mail {
to 'dev-team@example.com'
subject 'Build ${build.number} is ${build.result}'
body '''Build completed with result: ${build.result}
Log: ${build.url}console'''
}
Note: Post-build actions run after the build completes, regardless of success or failure.
8. Configuring Build Retention
| Option | Values | Description |
|---|---|---|
| Build History | 0 to 999 |
Number of builds to keep in history. Set to 0 to disable. |
| Days to Keep Builds | 0 to 365 |
Number of days to keep builds before deletion. Set to 0 to disable. |
| Builds to Keep | 0 to 999 |
Number of builds to keep regardless of age. |
Example: Configure Build Retention in Jenkins Job
pipeline {
agent any
post {
always {
// Retain last 5 builds and keep builds for up to 30 days
buildDiscarder(logRotator(daysToKeep: 30, numToKeep: 5))
}
}
}
Note: Build retention settings can be configured via the Jenkins UI under
Build History or programmatically using the buildDiscarder directive in a Pipeline script.9. Setting Concurrent Builds
| Option | Values | Notes |
|---|---|---|
| concurrentBuilds | false |
Prevents multiple builds from running simultaneously |
| maxConcurrentBuilds | 1 |
Maximum number of concurrent builds allowed |
Example: Configure Concurrent Builds in Jenkins Job
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
post {
always {
echo 'Build completed'
}
}
}
Note: Setting
maxConcurrentBuilds to a value greater than 1 allows multiple builds to run in parallel, which can improve throughput but may require additional resource management.
10. Using Workspace Cleanup
Workspace cleanup in Jenkins helps to remove unnecessary files and directories from the build workspace, ensuring a clean environment for each build. This is particularly useful in preventing issues caused by stale files or artifacts from previous builds.
| Option | Values | Description |
|---|---|---|
| Clean before build | true / false |
If enabled, Jenkins will delete all files in the workspace before starting the build. |
| Clean after build | true / false |
If enabled, Jenkins will delete all files in the workspace after completing the build. |
| Clean up after build | Build / Build and archive |
Determines whether to clean up after the build or after archiving artifacts. |
Example: Configuring Workspace Cleanup
<project>
<build>
<cleanBeforeBuild>true</cleanBeforeBuild>
<cleanAfterBuild>true</cleanAfterBuild>
</build>
</project>
Note: Enabling
cleanBeforeBuild ensures a fresh workspace for each build, which is especially useful in CI/CD environments where consistency is key.