0
0
Jenkinsdevops~5 mins

Why jobs are Jenkins core unit - Why It Works

Choose your learning style9 modes available
Introduction
Jenkins helps automate tasks like building and testing software. The main way it does this is through jobs, which are the basic units that tell Jenkins what to do step-by-step.
When you want to automate running tests every time code changes.
When you need to build your software automatically after a developer pushes code.
When you want to deploy your app to a server without manual steps.
When you want to schedule regular tasks like backups or reports.
When you want to chain multiple steps like build, test, and deploy in order.
Commands
Starts the Jenkins server so you can create and run jobs through the web interface.
Terminal
java -jar jenkins.war
Expected OutputExpected
Running from: /home/user/jenkins.war 2024-06-01 12:00:00.000+0000 [id=1] INFO org.eclipse.jetty.server.Server#start: Started @12345ms Jenkins is fully up and running
Creates a new Jenkins job named 'example-job' by sending its configuration XML to Jenkins.
Terminal
curl -X POST http://localhost:8080/createItem?name=example-job --header "Content-Type: application/xml" --data-binary @job-config.xml
Expected OutputExpected
No output (command runs silently)
-X POST - Specifies the HTTP method to create the job
--header "Content-Type: application/xml" - Tells Jenkins the data format is XML
Fetches the details of the 'example-job' to verify it exists and see its status.
Terminal
curl http://localhost:8080/job/example-job/api/json
Expected OutputExpected
{"name":"example-job","color":"blue","url":"http://localhost:8080/job/example-job/"}
Triggers the 'example-job' to run its defined steps immediately.
Terminal
curl -X POST http://localhost:8080/job/example-job/build
Expected OutputExpected
No output (command runs silently)
-X POST - Sends a command to start the job build
Checks the status and result of the last run of 'example-job'.
Terminal
curl http://localhost:8080/job/example-job/lastBuild/api/json
Expected OutputExpected
{"building":false,"result":"SUCCESS","duration":12345}
Key Concept

Jobs are the main instructions Jenkins follows to automate tasks like building, testing, and deploying software.

Common Mistakes
Trying to run Jenkins commands without starting the Jenkins server first.
Jenkins must be running to accept commands and create jobs.
Always start Jenkins with 'java -jar jenkins.war' before creating or running jobs.
Sending job creation requests without proper XML configuration.
Jenkins needs a valid job configuration in XML to create the job correctly.
Prepare a complete and valid job-config.xml file before sending the createItem request.
Not checking job status after triggering a build.
You won't know if the job succeeded or failed without checking its build status.
Use the API to check the last build status and confirm the job ran successfully.
Summary
Start Jenkins server to enable job creation and execution.
Create jobs by sending XML configuration to Jenkins via API.
Trigger jobs to run automated tasks like build and test.
Check job status to confirm successful automation.