A Jenkinsfile is a text file, checked into your repository, that defines a Jenkins pipeline as code. Its significance is that the build, test and deploy steps are versioned, reviewed and reproducible together with the application source, instead of living in clicks inside the Jenkins web UI.
Jenkins reads the file from the root of a branch (the default name is Jenkinsfile) and runs the stages it describes on one or more agents. The same file works for a single pipeline job, a Multibranch Pipeline and an organization folder that scans a whole GitHub or GitLab organization.
Before pipeline as code, jobs were configured through the UI. That configuration was hard to review, impossible to diff and easy to lose when a Jenkins server was rebuilt. A Jenkinsfile fixes that:
main.Jenkins supports two syntaxes, both written in Groovy-based DSL.
| Aspect | Declarative | Scripted |
|---|---|---|
| Entry point | pipeline { ... } | node { ... } |
| Structure | Fixed sections: agent, stages, post | Free-form Groovy |
| Learning curve | Lower, validated before running | Higher, full language power |
| Typical use | Most projects | Complex dynamic logic |
Start with declarative. Drop into a script { ... } block inside a stage when you need a loop or conditional logic that declarative does not offer.
This example builds and tests a Maven project, archives the test reports and publishes the jar only from the main branch.
The agent block chooses where the work runs, here a Docker container with Maven and a JDK. Each stage appears as a column in the Stage View and Blue Ocean style dashboards. The when directive skips the Publish stage on every branch except main, and post runs cleanup or notifications depending on the result.
agent any uses any available executor. agent { label 'linux' } targets labelled nodes, and Docker or Kubernetes agents give each build a clean, reproducible environment.
Never hard-code secrets in a Jenkinsfile. Store them in the Jenkins credential store and read them with withCredentials or the credentials() helper in environment. Jenkins masks the values in the console log.
When many repositories need the same steps, move them into a Jenkins shared library and import it with @Library('my-lib') _. This keeps each Jenkinsfile short and the common logic in one place.
Jenkinsfile to the repository root and commit it.Jenkinsfile.No. The default is Jenkinsfile in the repository root, but the script path is configurable per job, so names like ci/Jenkinsfile.release work.
Use declarative for nearly everything. It is easier to read, validated up front and enough for most CI/CD flows. Use scripted only for logic that declarative cannot express.
Yes. With the Kubernetes plugin you declare a pod template in agent { kubernetes { ... } }, and Jenkins starts an ephemeral pod for each build.
Store them as Jenkins credentials and bind them with withCredentials. The file then references only a credential ID, which is safe to commit.
A Jenkinsfile turns your CI/CD process into reviewed, versioned code that lives next to the application. Prefer declarative syntax, keep secrets in the credential store, use Multibranch Pipelines with webhooks, and extract repeated logic into shared libraries.