0
0
Jenkinsdevops~5 mins

Freestyle job creation in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Creating a freestyle job in Jenkins lets you automate tasks like building code or running scripts without writing complex code. It solves the problem of manually repeating these tasks by letting Jenkins do them automatically.
When you want to run a simple build or script without complex pipeline code
When you need to automate testing after every code change
When you want to schedule a task to run regularly, like backups
When you want to quickly set up a job to deploy an application
When you want to combine multiple build steps like compiling and archiving
Commands
This command creates a new freestyle job named 'example-job' in Jenkins using the job configuration file 'example-job-config.xml'.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 create-job example-job < example-job-config.xml
Expected OutputExpected
No output (command runs silently)
-s - Specifies the Jenkins server URL
This command starts a build of the 'example-job' freestyle job to run the configured tasks.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build example-job
Expected OutputExpected
Started example-job #1
-s - Specifies the Jenkins server URL
This command shows the console output of build number 1 of the 'example-job' to check the build progress and results.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console example-job 1
Expected OutputExpected
Building... Finished: SUCCESS
-s - Specifies the Jenkins server URL
Key Concept

If you remember nothing else from this pattern, remember: a freestyle job lets you automate simple tasks in Jenkins without writing pipeline code.

Common Mistakes
Trying to create a freestyle job without a valid XML config file
Jenkins requires a proper job configuration file to create the job; missing or invalid XML causes failure.
Generate or export a valid job config XML from Jenkins or create one following Jenkins XML schema.
Running build command before creating the job
The build command fails because the job does not exist yet on the Jenkins server.
Always create the job first using the create-job command before triggering builds.
Not specifying the Jenkins server URL with -s flag
The CLI cannot connect to Jenkins without knowing the server address, so commands fail.
Always include the -s flag with the Jenkins server URL in CLI commands.
Summary
Use the Jenkins CLI create-job command with a valid XML config to create a freestyle job.
Trigger the job build with the build command to run the automated tasks.
Check the build progress and results using the console command.