Jenkins Cheat Sheet

Basic Jenkins Commands



To start Jenkins sudo service jenkins start

To stop Jenkins sudo service jenkins stop

To restart Jenkins sudo service jenkins restart

To check jenkins status sudo service jenkins status

Url to access jenkins via web browser http://localhost:8080

Jenkins default password file. cat /var/lib/jenkins/secrets/initialAdminPassword

Types of Jenkins Jobs

Freestyle Project

Basic job configuration with flexible build steps and actions.

Pipeline Job

Defines the entire build process as code using Groovy in a Jenkinsfile.

Multibranch Pipeline

Automatically creates pipelines for each branch in a repository.

Folder

Organizes multiple jobs into logical groups to reduce UI clutter.

GitHub Organization

Automatically creates pipelines for all repositories in a GitHub organization.

Matrix Project

Runs jobs across multiple configurations, such as different platforms or environments.

External Job

Monitors and logs external processes run outside of Jenkins.

Maven Project

Specialized job for building Maven projects with tighter integration.

Multijob Project

Executes multiple jobs in phases, either sequentially or in parallel.

Declarative Pipeline

Pipeline as code using an easy-to-read, structured syntax.

Parameterized Job

Job that allows user input or dynamic parameters at build time.

Remote Trigger Job

Can be triggered externally using Jenkins API calls.

External Monitor Job

Monitors and tracks external processes without directly controlling them.

Freestyle Job Setup



  1. Go to New Item > Freestyle project.
  2. Configure project details.
  3. Add build steps: Shell script, Windows batch command, Invoke Gradle/Maven script.
  4. Configure post-build actions like archiving artifacts or sending email notifications.

Pipeline Syntax



Basic Pipeline Example (Declarative Pipeline)



pipeline { agent any stages { stage('Build') { steps { echo 'Building...' sh 'make build' // Use shell script for build } } stage('Test') { steps { echo 'Testing...' sh 'make test' } } stage('Deploy') { steps { echo 'Deploying...' sh 'make deploy' } } } }

Scripted Pipeline Example



node { stage('Build') { echo 'Building...' sh 'make build' } stage('Test') { echo 'Testing...' sh 'make test' } stage('Deploy') { echo 'Deploying...' sh 'make deploy' } }

Common Pipeline Syntax



Define Agent



To use any available agent. agent any

To use no agent, usually for matrix jobs. agent none

Stage

stage('Stage Name') { steps { sh 'shell command or script' } }

Environment Variables



environment { MY_ENV = 'Value' }

Post Conditions



post { always { echo 'Always executed!' } success { echo 'Executed on success!' } failure { echo 'Executed on failure!' } }

Jenkins File (Pipeline as Code)

pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } stage('Deploy') { steps { echo 'Deploying...' } } } }

Useful Plugins



  • Git Plugin: Integrates Git with Jenkins, allowing builds triggered from Git repositories.
  • GitHub Integration: For triggering builds and managing pull requests from GitHub.
  • Pipeline Plugin: Enables defining build steps using the Jenkinsfile for CI/CD pipelines.
  • Blue Ocean: Provides a modern UI for Jenkins.
  • Build Pipeline Plugin: Visualizes the chain of jobs.
  • Slack Notifications Plugin: Sends build status notifications to Slack channels.
  • Email Extension Plugin: Sends build notifications via email.

Trigger Build Options



Poll SCM

Triggers the build based on changes in the source code repository. H/5 * * * * # Poll SCM every 5 minutes

Build Periodically

Schedules periodic builds. H 22 * * 1-5 # Build at 10 PM, Monday through Friday

GitHub Webhooks

Trigger builds based on GitHub push events.

Manual Trigger

Manually trigger the build through the Jenkins UI.

Artifacts



Archiving Artifacts

After building, save files for later use. archiveArtifacts artifacts: 'build/output/**/*'

Saving Test Results

To save test result in xml. junit 'build/reports/**/*.xml'

Environment Variables in Jenkins



  • $WORKSPACE: The path to the current job’s workspace.
  • $BUILD_NUMBER: The build number.
  • $JOB_NAME: Name of the current job.
  • $BUILD_URL: URL of the current build.
  • $GIT_COMMIT: The current Git commit hash.

Shell Commands in Jenkins (for Freestyle Jobs)



Basic Shell Command echo "Hello, Jenkins!"

Running a Script ./scripts/deploy.sh

Setting Environment Variables in Shell export MY_VAR=example_value

Working with Credentials



Storing Credentials

Store sensitive information like tokens and passwords in Jenkins credentials manager.

Using Credentials in a Pipeline

pipeline { agent any environment { MY_SECRET = credentials('my-credential-id') } stages { stage('Example') { steps { echo "Secret value is ${MY_SECRET}" } } } }

Notifications



Email Notification

post { always { mail to: 'dev-team@example.com', subject: "Job ${env.JOB_NAME} Build ${env.BUILD_NUMBER}", body: "Build completed with status: ${currentBuild.currentResult}" } }

Parallel Execution



Running Jobs in Parallel

stage('Parallel Stage') { parallel { stage('Job 1') { steps { echo 'Running Job 1...' } } stage('Job 2') { steps { echo 'Running Job 2...' } } } }