Jenkins
Concise, exam-focused study notes on Jenkins for CI/CD in AWS cloud applications: concepts, pipeline syntax, common errors, worked pipeline examples, and what to expect in exams.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Jenkins is a widely used open-source automation server for implementing CI/CD (Continuous Integration and Continuous Delivery). An engineer uses Jenkins to automate building, testing and delivering application code—e.g., compiling a Java project, running tests, producing artifacts, and deploying to AWS services such as EC2, ECR or Lambda as part of a repeatable pipeline.
Key ideas
Continuous Delivery (CD) — building on CI: every successful build is deployable to production-like environments. Definition: "deliver every change to a production-like environment and make deployment a low-risk, routine event." Requires automated deployment steps and safe rollbacks.
Job (or Project) — a configured task Jenkins runs (e.g., Freestyle job, Pipeline). A job has configuration: source repository, triggers, build steps, post actions.
Pipeline — code that defines a sequence of stages for building, testing and deploying software. It is recommended to store pipelines as code in the repository using a Jenkinsfile. A common job type for CI is a Multibranch Pipeline, which automatically discovers branches and pull requests in the SCM and creates runs per branch using the Jenkinsfile at each branch root.
- Declarative Pipeline — a higher-level, opinionated syntax for Jenkinsfile with defined blocks:
pipeline { agent { } stages { } post { } }. - Scripted Pipeline — Groovy-based, more flexible and imperative.
- Declarative Pipeline — a higher-level, opinionated syntax for Jenkinsfile with defined blocks:
Controller (formerly Master) — the core Jenkins process that provides UI, scheduling, and orchestration. It should not be overloaded with builds.
Agent (formerly called 'slave') — a separate machine or container where build steps run. Agents report to the controller and have a number of executors.
Executor — a build slot on an agent. If an agent has N executors it can run N builds concurrently (subject to labels and job restrictions).
Workspace — the directory on an agent where a job's source is checked out and build steps run.
Artifact — build outputs produced by a job (e.g., .jar, .war). Jenkins can archive files as artifacts for later retrieval; container images are typically pushed to a registry (e.g., ECR) rather than archived.
Plugin — an extension to Jenkins adding features (e.g., Git plugin, Pipeline plugin, AWS-related plugins). Installing incompatible or outdated plugins is a frequent source of problems.
Credentials — secure storage inside Jenkins for secrets (username/password, SSH keys, AWS access keys). Always prefer credentials plugin over hard-coding secrets.
Webhook — configured at the Git server to notify Jenkins of new commits; this is a common trigger for CI.
Diagram to draw for an exam: draw a central box labeled "Jenkins Controller (Master)" and several boxes labeled "Agent 1", "Agent 2 (Docker)" connected to controller. Label arrows: "job queue" from controller to agents, "build log" back to controller. Inside Agent draw "workspace" and list files: Jenkinsfile, source checkout, build artifacts. Also show Git repository -> webhook -> controller.
Assumptions: Jenkins runs on a reliable host with network access to source code and build agents; agents have required toolchain (JDK, Maven, Docker); IAM/auth is configured for AWS actions.
Formulas
For this programming/automation topic, remember these syntax templates and short rules:
Jenkinsfile (Declarative skeleton):
pipeline { agent any; environment { VAR = "value" }; stages { stage('Name') { steps { sh 'command' } } }; post { always { archiveArtifacts artifacts: 'file' } } }Scripted pipeline basic:
node('label') { stage('Build') { sh 'make' } }Agent executors rule:
max_concurrent_builds = sum(executors on each online node)Meaning: the upper bound equals the total executors across all online nodes (controller + agents), subject to labels and any job-level throttling or 'Disable Concurrent Build' settings (unit: builds).AWS ECR Docker login (syntax to remember inside a build step):
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.comArchive artifact step (Declarative):
archiveArtifacts artifacts: 'target/*.jar', fingerprint: true— archives files matching the pattern.Checkout step (Declarative):
checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[url: 'https://github.com/org/repo.git']]])— more commonly used viacheckout scmwhen Jenkinsfile is in the repo.
Worked examples
Example 1 — Freestyle job building a Maven Java project
Given: Git repo URL https://github.com/example/app.git, branch main, uses Maven to produce target/app.jar.
Steps (what you would configure in Jenkins UI):
- Create a new Freestyle job named
app-build. - In "Source Code Management" choose Git and enter repository URL and branch
main. - Under "Build Triggers" enable "GitHub hook trigger" (requires webhook on GitHub).
- Under "Build" add "Invoke top-level Maven targets" with
Goals=clean package -DskipTestsand setMaven Versionto installed tool. - Under "Post-build Actions" add "Archive the artifacts" with
Files to archive=target/*.jar.
Run trace when a commit arrives:
- GitHub sends webhook -> Jenkins schedules build -> agent assigned -> workspace created -> git clone runs ->
mvn clean package -DskipTestsexecutes ->target/app.jarcreated -> artifacts archived to Jenkins -> console log stored.
Final artifact: target/app.jar archived in Jenkins (this is what you report as the output).
Example 2 — Declarative Jenkinsfile building Docker image and pushing to AWS ECR
Jenkinsfile (explain each line in exam):
pipeline { agent any environment { ECR_REPO = "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app" } stages { stage('Checkout') { steps { checkout scm } } stage('Build') { steps { sh 'docker build -t my-app:latest .' } } stage('Tag & Push') { steps { withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-creds']]) { sh "aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com" sh "docker tag my-app:latest $ECR_REPO:latest" sh "docker push $ECR_REPO:latest" } } } } post { success { echo 'Pushed image to ECR' } } }
Trace (numbered):
- Jenkins agent checks out code (Jenkinsfile present).
- Docker build produces image
my-app:latestlocally on agent. withCredentialsinjects AWS keys (stored in Jenkins) into environment for AWS CLI.aws ecr get-login-passwordprovides a token piped todocker loginto authenticate to ECR.- Image tagged and pushed to ECR; result: image at 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest.
Notes: ensure IAM user/role has ECR permissions and that the agent has Docker and AWS CLI installed.
Common mistakes
- Forgetting to install required plugins (e.g., Git, Pipeline, Credentials Binding, AWS Credentials) or using incompatible plugin versions.
- Not cleaning workspace causing stale files / test flakiness.
- Assuming workspace path is the same on all agents (it can vary); use relative paths or workspace variables.
- Missing IAM permissions for the AWS operations called from pipeline (push to ECR, deploy to EC2, etc.).
- Not using
--password-stdinfor Docker login with ECR (older docs showget-loginwhich is deprecated).
For your exam
Types of questions you will see:
- Definitions: define Jenkins, pipeline, agent, executor, workspace, artifact, webhook, credentials.
- Short notes: explain Declarative vs Scripted pipeline, explain Jenkins master-agent architecture, describe how to securely store secrets.
- Diagrams: draw controller-agent diagram and show job flow from SCM webhook to artifact archive.
- Derivations / reasoning: show calculation of maximum concurrent builds using executors; reason why pipelines-as-code is better for reproducibility.
- Programs/config examples: provide a Jenkinsfile for building and publishing to AWS ECR or for running tests and archiving artifacts.
What a full-marks answer includes:
- Precise definitions (bold terms where required).
- Correct skeleton of a Jenkinsfile (declarative) with explanation of each block (agent, stages, steps, post).
- A working example trace showing inputs, commands run, and final artifact or deployment target.
- Security considerations: mention credentials store, avoid plaintext secrets, and mention IAM roles for AWS actions.
- A clear diagram or described diagram for architecture questions.
Quick check
- What block in a declarative Jenkinsfile lists build steps?
- Where should you store AWS access keys in Jenkins?
- How do you authenticate Docker to push to ECR inside a pipeline?
Answers: stage's steps block; in Jenkins Credentials store (use credentialsId and withCredentials); use aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account>.dkr.ecr.<region>.amazonaws.com.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?