Mock Jenkinsfile Generator
Generates an example declarative Jenkins pipeline for CI/CD
Generated 204 words · 1,995 characters
See 3 examples
- Example 1
pipeline { agent { docker { image 'node:18-alpine' args '-v /var/cache/highmoor/npm:/cache' } } environment { APP_NAME = 'profile-frontend' REGISTRY = 'ghcr.example.com/highmoor' AWS_REGION = 'eu-central-1' KUBE_NAMESPACE = 'highmoor-profile' SONAR_PROJECT_KEY = 'highmoor_profile-frontend' DEPLOY_CREDS = credentials('profile-frontend-deployer') } options { timeout(time: 60, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '30')) timestamps() } triggers { pollSCM('H/15 * * * *') } stages { stage('Install') { steps { sh 'npm ci' } } stage('Build') { steps { sh 'npm run build:prod' } } stage('Test') { steps { sh 'npm test -- --ci' } } stage('Container') { when { anyOf { branch 'master'; branch 'release/*' } } steps { sh "docker build -t ${REGISTRY}/profile-frontend:${GIT_COMMIT} ." sh "docker push ${REGISTRY}/profile-frontend:${GIT_COMMIT}" } } stage('Artifacts') { steps { archiveArtifacts artifacts: 'dist/**', fingerprint: true, onlyIfSuccessful: true } } stage('Contract Tests') { steps { sh 'docker compose -f ci/compose.yml -p profile-frontend-${BUILD_NUMBER} up --abort-on-container-exit' } } stage('Deploy') { when { anyOf { branch 'master'; buildingTag() } } steps { sh 'kubectl -n highmoor-profile apply -f k8s/' sh 'kubectl -n highmoor-profile rollout status deployment/profile-frontend --timeout=180s' } } } post { always { junit allowEmptyResults: true, testResults: 'reports/**/*.xml' cleanWs() } success { echo 'profile-frontend pipeline finished successfully' } failure { slackSend channel: '#profile-builds', color: 'danger', message: "profile-frontend #${env.BUILD_NUMBER} failed: ${env.BUILD_URL}" } } } - Example 2
pipeline { agent { docker { image 'node:20-alpine' label 'build-farm' } } environment { APP_NAME = 'scheduler-dashboard' REGISTRY = 'kestrel.registry.example.com' AWS_REGION = 'eu-west-1' } options { timeout(time: 12, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '30')) timestamps() } triggers { pollSCM('H/30 * * * *') } stages { stage('Install') { steps { sh 'yarn install --frozen-lockfile' } } stage('Lint') { steps { sh 'yarn lint' } } stage('Build') { steps { sh 'yarn build' } } stage('Test') { steps { sh 'yarn test --ci' } } stage('Package') { steps { archiveArtifacts artifacts: 'dist/**', fingerprint: true, onlyIfSuccessful: true } } stage('System Tests') { steps { sh 'docker compose -f compose.test.yaml -p scheduler-dashboard-${BUILD_NUMBER} up --abort-on-container-exit' } } stage('Static Analysis') { steps { withSonarQubeEnv('sonar') { sh 'sonar-scanner -Dsonar.projectKey=kestrel_scheduler-dashboard' } } } stage('Deploy') { when { branch 'main' } steps { sh './scripts/deploy.sh scheduler-dashboard production' } } } post { always { junit allowEmptyResults: true, testResults: 'reports/**/*.xml' cleanWs() } success { slackSend channel: '#scheduler-alerts', color: 'good', message: "scheduler-dashboard is green on ${env.BRANCH_NAME}" } failure { mail to: 'scheduler-team@kestrel.example.com', subject: "scheduler-dashboard build ${env.BUILD_NUMBER} failed", body: "See ${env.BUILD_URL}" } } } - Example 3
pipeline { agent { docker { image 'node:18-alpine' label 'linux' } } environment { APP_NAME = 'scheduler-service' REGISTRY = 'registry.example.com/northfield' KUBE_NAMESPACE = 'northfield-prod' DEPLOY_CREDS = credentials('northfield-deploy-key') } options { timeout(time: 15, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '40')) timestamps() } triggers { cron('H 2 * * 0') } stages { stage('Install') { steps { sh 'npm ci' } } stage('Lint') { steps { sh 'npm run lint' } } stage('Build') { steps { sh 'npm run build' } } stage('Test') { steps { sh 'npm test -- --ci' } } stage('Artifacts') { steps { archiveArtifacts artifacts: 'dist/**', fingerprint: true, onlyIfSuccessful: false } } stage('Build Image') { when { anyOf { branch 'release'; branch 'release/*' } } steps { sh "docker build -t ${REGISTRY}/scheduler-service:${GIT_COMMIT} ." sh "docker push ${REGISTRY}/scheduler-service:${GIT_COMMIT}" } } stage('Deploy') { when { branch 'release' } steps { sh "aws ecs update-service --cluster northfield-prod --service scheduler-service --force-new-deployment --region ${AWS_REGION}" } } } post { always { junit allowEmptyResults: true, testResults: 'reports/**/*.xml' cleanWs() } success { slackSend channel: '#scheduler-deploys', color: 'good', message: "scheduler-service #${env.BUILD_NUMBER} passed" } failure { slackSend channel: '#scheduler-deploys', color: 'danger', message: "scheduler-service broke on ${env.BRANCH_NAME}" } } }
About this generator
Jenkins declarative pipeline syntax is not immediately obvious, and a Jenkinsfile that mixes scripted and declarative syntax or puts credentials in the wrong place is a common source of CI failures. A concrete, correct example of the declarative structure — with stages, steps, a conditional when block, and a post section — is the fastest way to learn it or scaffold a new pipeline. This tool generates a complete declarative Jenkinsfile. Each run invents a project — an app name such as orders-api or loyalty-worker and an organization — and picks one of eight toolchains: npm, Yarn, pnpm, Maven, Gradle, Go, Python, or make. The file includes a Docker agent with a matching image, an environment block with the app name, registry, and optional region, namespace, or credentials, options with a timeout, build discarder, and timestamps, and a trigger. Install, Build, and Test stages use the toolchain's real commands, an optional Lint stage and one to three extra stages (security scan, SonarQube analysis, image build and push, artifact archiving, or integration tests) follow, and a Deploy stage gated by a when condition ships with Helm, kubectl, S3, Ansible, ECS, or a deploy script. A post block always publishes JUnit results, cleans the workspace, and notifies on success and failure. Copy the output into a Jenkinsfile at the root of your repository. Adapt the agent, build commands, and deploy steps to your own infrastructure. Store all credentials in Jenkins's credential store and reference them by id — never embed secrets in the Jenkinsfile itself, as it is checked into version control.
How to use
- Click Generate to produce a Jenkinsfile.
- Copy it into your repository.
- Adapt the build and test commands.
- Keep credentials in Jenkins, not the file.
Use Cases
- •Learning Jenkins pipelines
- •Scaffolding a CI/CD pipeline
- •Documenting a build process
- •Demoing pipeline structure
- •Testing a Jenkinsfile parser
Tips
- →Stages organize the pipeline.
- →Use when for conditional stages.
- →Keep secrets out of the file.
- →Adapt commands to your project.
FAQ
what does the generated jenkinsfile include
A declarative pipeline with a Docker agent, an environment block for the generated app, options (timeout, build discarder, timestamps), a trigger, Install, Build, and Test stages with commands for one of eight toolchains (npm, Yarn, pnpm, Maven, Gradle, Go, Python, or make), an optional Lint stage, one to three extra stages such as a security scan or image push, and a Deploy stage guarded by a when condition. The post block publishes JUnit results, cleans the workspace, and sends success and failure notifications.
what is the difference between declarative and scripted jenkins pipelines
Declarative pipelines use a structured DSL with a pipeline block, stages, and steps — easier to read, validate, and use with visual tools. Scripted pipelines use a Groovy node block with full programmatic control but less structure. Declarative is the recommended modern approach for most teams; scripted is reserved for complex cases that the declarative syntax cannot express.
what does the when condition do in a stage
A when block makes a stage conditional. The generated Deploy stage only runs on its release branch (for example when { branch 'main' }), sometimes combined with extra conditions such as not { changeRequest() }, so a pipeline triggered by a feature branch or pull request skips the deploy automatically. This lets a single Jenkinsfile handle pull requests and production releases without separate pipeline files.
how should I handle secrets in a jenkinsfile
Never hard-code credentials or tokens in the Jenkinsfile, since it lives in version control. Use the Jenkins Credentials store and reference secrets in pipeline code with withCredentials or the credentials() helper in the environment block. This keeps secrets out of the repository and injects them securely at runtime.
You might also like
Popular tools from other categories that share themes with this one.