80.2 Project 68: Freestyle job build and test
Friends, freestyle jobs still matter for simple builds. Today we create a freestyle job, pull a sample Maven app, run tests, archive the JAR, and read the console log.
मित्रांनो, साध्या builds साठी freestyle अजून उपयुक्त. आज freestyle job, sample Maven app, tests, JAR archive, आणि console log.
दोस्तों, साधारण builds के लिए freestyle अभी भी ज़रूरी. आज freestyle job, sample Maven app, tests, JAR archive, और console log.
What you will build in this project
- Create a freestyle job.
- Add SCM and a Maven build step.
- Archive the JAR artifact.
- Read console output and test results.
1. Start from a real company story
At Infosys in Bengaluru, a hand-clicked build on a laptop shipped a JAR that skipped unit tests.
A null pointer hit production checkout. Rough cost: about ₹9 lakh in war-room time that night.
Ravi made freestyle on Jenkins the gate, with tests required before archive.
Today we build that freestyle path.
Ravindra Bagale's Tip
Students tick "Discard old builds" with a count of one and lose the only good JAR. Keep at least the last ten successful artifacts. Keep that habit.
Students old builds count 1 ठेवतात आणि चांगला JAR हरवतात. किमान दहा successful artifacts ठेवा. सवय ठेवा.
Students old builds count 1 रखते हैं और अच्छा JAR खो देते हैं. कम से कम दस successful artifacts रखें. आदत रखिए.
2. Start and prepare
Step 1: Why freestyle
Why: Simple UI jobs still ship many teams. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: echo scope. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 1: a1
Type this:
echo 'Freestyle = UI form with SCM, build steps, post actions'
You will see:
Freestyle = UI form with SCM, build steps, post actions
What it means: What.
Example 2: a2
Type this:
mkdir -p /tmp/jen68/{app,job} && ls /tmp/jen68
You will see:
app\njob
What it means: Folders.
Example 3: a3
Type this:
echo 'Today: freestyle, Maven test+package, archive JAR'
You will see:
Today: freestyle, Maven test+package, archive JAR
What it means: Scope.
Step 2: Sample Maven app
Why: Tiny project tree. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: printf pom and test. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 4: b1
Type this:
mkdir -p /tmp/jen68/app/src/main/java/shop /tmp/jen68/app/src/test/java/shop && printf '%s\n' 'package shop;' 'public class Cart { public int add(int a,int b){ return a+b; } }' > /tmp/jen68/app/src/main/java/shop/Cart.java && cat /tmp/jen68/app/src/main/java/shop/Cart.java
You will see:
public class Cart { public int add(int a,int b){ return a+b; } }
What it means: Cart.
Example 5: b2
Type this:
printf '%s\n' 'package shop;' 'import org.junit.jupiter.api.Test;' 'import static org.junit.jupiter.api.Assertions.assertEquals;' 'class CartTest { @Test void addWorks(){ assertEquals(5, new Cart().add(2,3)); } }' > /tmp/jen68/app/src/test/java/shop/CartTest.java && wc -l /tmp/jen68/app/src/test/java/shop/CartTest.java
You will see:
4 /tmp/jen68/app/src/test/java/shop/CartTest.java
What it means: Test.
Example 6: b3
Type this:
printf '%s\n' '<project><modelVersion>4.0.0</modelVersion><groupId>shop</groupId><artifactId>cart</artifactId><version>1.0.0</version><properties><maven.compiler.release>17</maven.compiler.release></properties></project>' > /tmp/jen68/app/pom.xml && grep artifactId /tmp/jen68/app/pom.xml
You will see:
<artifactId>cart</artifactId>
What it means: pom.
Step 3: Job form
Why: New freestyle item. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: job fields. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 7: c1
Type this:
printf '%s\n' 'New Item -> Freestyle' 'Name: cart-freestyle' 'Restrict where: maven' 'SCM: Git URL of the cart repo' > /tmp/jen68/job/FORM.txt && cat /tmp/jen68/job/FORM.txt
You will see:
Name: cart-freestyle
What it means: Form.
Example 8: c2
Type this:
echo 'Branches to build: */main'
You will see:
Branches to build: */main
What it means: Branch.
Example 9: c3
Type this:
echo 'Poll SCM can wait; webhook comes in project 70'
You will see:
Poll SCM can wait; webhook comes in project 70
What it means: Bridge tip.
3. Build the main pieces
Step 4: Maven build step
Why: Goals that matter. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: invoke Maven. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 10: d1
Type this:
printf '%s\n' 'Build step: Invoke Maven' 'Goals: -B test package' 'POM: pom.xml' > /tmp/jen68/job/MAVEN.txt && cat /tmp/jen68/job/MAVEN.txt
You will see:
Goals: -B test package
What it means: Goals.
Example 11: d2
Type this:
echo '-B is batch mode — quieter logs for CI'
You will see:
-B is batch mode — quieter logs for CI
What it means: Batch.
Example 12: d3
Type this:
echo 'JDK and Maven must exist on the maven-labelled agent'
You will see:
JDK and Maven must exist on the maven-labelled agent
What it means: Tools.
Step 5: Archive artifacts
Why: Keep the JAR. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: post-build. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 13: e1
Type this:
printf '%s\n' 'Post-build: Archive the artifacts' 'Files: target/*.jar' 'Exclude: target/*-sources.jar' > /tmp/jen68/job/ARCHIVE.txt && cat /tmp/jen68/job/ARCHIVE.txt
You will see:
Files: target/*.jar
What it means: Archive.
Example 14: e2
Type this:
echo 'Fingerprint artifacts so downstream jobs can find them'
You will see:
Fingerprint artifacts so downstream jobs can find them
What it means: Fingerprint.
Example 15: e3
Type this:
echo 'Keep build for 10 successful runs minimum'
You will see:
Keep build for 10 successful runs minimum
What it means: Retention.
Step 6: Publish tests
Why: JUnit results. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: surefire reports. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 16: f1
Type this:
printf '%s\n' 'Post-build: Publish JUnit test result report' 'Test report XMLs: target/surefire-reports/*.xml' > /tmp/jen68/job/JUNIT.txt && cat /tmp/jen68/job/JUNIT.txt
You will see:
target/surefire-reports/*.xml
What it means: JUnit.
Example 17: f2
Type this:
echo 'Red test trend graph means investigate before next release'
You will see:
Red test trend graph means investigate before next release
What it means: Trend.
Example 18: f3
Type this:
echo 'Do not archive JAR if tests fail — leave archive after test step'
You will see:
Do not archive JAR if tests fail — leave archive after test step
What it means: Order.
4. Automate and verify
Step 7: Run and console
Why: First build. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: build now. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 19: g1
Type this:
echo 'Click Build Now then open Console Output'
You will see:
Click Build Now then open Console Output
What it means: Run.
Example 20: g2
Type this:
echo 'Look for BUILD SUCCESS and Archived 1 artifact'
You will see:
Look for BUILD SUCCESS and Archived 1 artifact
What it means: Expect.
Example 21: g3
Type this:
printf '%s\n' 'Acceptance: blue ball, tests green, JAR downloadable' > /tmp/jen68/job/ACCEPT.txt && cat /tmp/jen68/job/ACCEPT.txt
You will see:
Acceptance: blue ball, tests green, JAR downloadable
What it means: Gate.
Step 8: Local mvn proof
Why: Same goals on laptop. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: run locally. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 22: h1
Type this:
echo 'cd /tmp/jen68/app && mvn -B -q test package'
You will see:
cd /tmp/jen68/app && mvn -B -q test package
What it means: Local.
Example 23: h2
Type this:
echo 'ls target/*.jar'
You will see:
ls target/*.jar
What it means: JAR tip.
Example 24: h3
Type this:
printf '%s\n' 'cart-1.0.0.jar' > /tmp/jen68/app/EXPECTED_JAR.txt && cat /tmp/jen68/app/EXPECTED_JAR.txt
You will see:
cart-1.0.0.jar
What it means: Name.
Step 9: Fail path
Why: Broken test stops archive. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: negative case. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 25: i1
Type this:
echo 'Change assertEquals(5, ...) to assertEquals(6, ...) and rebuild'
You will see:
Change assertEquals(5, ...) to assertEquals(6, ...) and rebuild
What it means: Break.
Example 26: i2
Type this:
echo 'Job must go red; no new JAR in Last Successful Artifacts'
You will see:
Job must go red; no new JAR in Last Successful Artifacts
What it means: Expect fail.
Example 27: i3
Type this:
echo 'Revert the test before you leave the lab'
You will see:
Revert the test before you leave the lab
What it means: Cleanup.
5. Harden and accept
Step 10: Story Infosys
Why: Laptop JAR. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 28: j1
Type this:
printf '%s\n' 'At Infosys in Bengaluru, a hand-clicked laptop JAR skipped unit tests and broke checkout.' 'Ravi made freestyle on Jenkins the only path, with tests before archive.' > /tmp/jen68/STORY.txt && wc -l /tmp/jen68/STORY.txt
You will see:
2 /tmp/jen68/STORY.txt
What it means: Story.
Example 29: j2
Type this:
echo 'Rough cost: about ₹9 lakh in war-room time that night'
You will see:
Rough cost: about ₹9 lakh in war-room time that night
What it means: Cost.
Example 30: j3
Type this:
echo 'Ravi: PASS'
You will see:
Ravi: PASS
What it means: Sign-off.
Step 11: Proof files
Why: Job notes exist. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ls. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 31: k1
Type this:
ls /tmp/jen68/job /tmp/jen68/app/src/main/java/shop
You will see:
/tmp/jen68/job:\nACCEPT.txt\nARCHIVE.txt\nFORM.txt\nJUNIT.txt\nMAVEN.txt\n\n/tmp/jen68/app/src/main/java/shop:\nCart.java
What it means: Tree.
Example 32: k2
Type this:
test -f /tmp/jen68/app/pom.xml && echo 'pom ok'
You will see:
pom ok
What it means: pom.
Example 33: k3
Type this:
echo 'Next: Declarative Jenkinsfile project 69'
You will see:
Next: Declarative Jenkinsfile project 69
What it means: Bridge.
Step 12: Acceptance
Why: Freestyle green with JAR. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 34: z1
Type this:
echo 'mvn test package green'
You will see:
mvn test package green
What it means: Tests.
Example 35: z2
Type this:
echo 'JAR archived on Jenkins'
You will see:
JAR archived on Jenkins
What it means: Artifact.
Example 36: z3
Type this:
echo 'Broken test keeps job red'
You will see:
Broken test keeps job red
What it means: Fail path.
Check yourself
Q1. Who fixed Infosys laptop JAR?
Ravi
Q2. Build tool?
Maven
Q3. Archive pattern?
target/*.jar
Q4. Must tests pass?
yes
Q5. Agent label?
maven
Q6. Old builds keep?
10
Friends, you built and tested with a freestyle job. Did you understand? Let us try it once more. Fail the job on purpose by breaking a test and prove the JAR is not archived.
मित्रांनो, तुम्ही freestyle job ने build आणि test केले. एक test मोडून job fail करा आणि JAR archive होत नाही हे सिद्ध करा.
दोस्तों, आपने freestyle job से build और test किया. एक test तोड़कर job fail करें और JAR archive न हो, यह साबित करें.
Lab: try at home
- Create freestyle job.
- Wire SCM and Maven.
- Archive artifacts.
- Read console and junit.
Recap
- Freestyle is fine for simple flows.
- Tests before archive.
- Keep last N artifacts.
- Console tells the truth.
- Labels pick the agent.