Aisalkyn Aidarovaπ§ LAB GOAL Create a Shared Library repository Add proper folder structure Configure it in...
By the end, you will understand:
Click New Repository
Repository name:
company-shared-lib
Click Create.
Inside that repository:
Click Add file β Create new file
In the file name field write:
vars/buildApp.groovy
This automatically creates the vars folder.
Inside vars/buildApp.groovy, paste:
def call() {
echo "Shared Library: Starting Build Stage"
sh "echo Running build on $(hostname)"
}
Click Commit.
Click Add file β Create new file
Name:
vars/deployApp.groovy
Paste:
def call(String environment) {
echo "Shared Library: Deploying to ${environment}"
if (environment == "prod") {
input "Approve Production Deployment?"
}
sh "echo Deployment to ${environment} completed"
}
Click Commit.
Your repo should now look like:
company-shared-lib/
βββ vars/
βββ buildApp.groovy
βββ deployApp.groovy
Important:
vars folder name must be exactdef call() makes it callable like a functionNow go to your Jenkins UI:
http://13.59.246.183:8080
Manage Jenkins
β Manage System
Scroll down to:
Global Trusted Pipeline Libraries
Click Add
(We use Trusted because DevOps owns this library.)
Name:
company-lib
Default version:
main
Retrieval Method:
Modern SCM
SCM:
Git
Repository URL:
Paste your shared library GitHub URL
If private:
Add credentials.
Click Save.
Jenkins now knows:
"Whenever someone writes @Library('company-lib'), load this repo."
Now create second GitHub repository.
Name:
sample-app
Click Create.
Inside sample-app:
Add file:
Jenkinsfile
Paste:
@Library('company-lib') _
pipeline {
agent { label 'linux' }
stages {
stage('Build') {
steps {
buildApp()
}
}
stage('Deploy to Dev') {
steps {
deployApp("dev")
}
}
stage('Deploy to Prod') {
steps {
deployApp("prod")
}
}
}
}
Commit.
Go to Jenkins Dashboard.
Click:
New Item
Name:
sample-app-pipeline
Select:
Pipeline
Click OK.
Scroll to bottom.
Pipeline section:
Definition:
Pipeline script from SCM
SCM:
Git
Repository URL:
Paste sample-app GitHub URL
Branch:
main
Script Path:
Jenkinsfile
Click Save.
Click:
Build Now
Watch Console Output.
You will see:
Click Approve.
Build completes.
Step-by-step internally:
vars
Controller = Brain
Agent = Worker
| Component | Owner |
|---|---|
| Shared Library Repo | DevOps / Platform Team |
| Jenkins Configuration | DevOps |
| Application Code | Developers |
| Jenkinsfile in App | Usually DevOps template, developers minimal edits |
Developers should NOT control deployment logic.
Very important production topics:
Example version pin:
@Library('company-lib@v1.0') _
EC2 (Controller):
GitHub:
Agents:
Shared Library solves:
We will create:
Shared Library function:
buildAndPushECR(imageName, awsRegion)
It will:
Before starting, make sure:
On Jenkins Linux Agent:
docker --version
aws --version
If not installed:
sudo apt update
sudo apt install docker.io -y
sudo apt install awscli -y
sudo usermod -aG docker jenkins
Restart agent if needed.
On EC2 Jenkins instance:
Attach IAM Role with permissions:
ecr:GetAuthorizationToken
ecr:BatchCheckLayerAvailability
ecr:PutImage
ecr:InitiateLayerUpload
ecr:UploadLayerPart
ecr:CompleteLayerUpload
Best practice: use IAM Role (not access keys).
Go to AWS Console
β ECR
β Create Repository
Name:
demo-app
Click Create.
Copy:
Repository URI
Example:
021399177326.dkr.ecr.us-east-2.amazonaws.com/demo-app
Save this.
Go to GitHub β Create new repo:
company-shared-lib
Create:
vars/buildAndPushECR.groovy
def call(String imageName, String region) {
def accountId = sh(
script: "aws sts get-caller-identity --query Account --output text",
returnStdout: true
).trim()
def ecrRepo = "${accountId}.dkr.ecr.${region}.amazonaws.com/${imageName}"
def tag = "${env.BUILD_NUMBER}"
echo "Building Docker Image..."
sh "docker build -t ${imageName}:${tag} ."
echo "Logging into ECR..."
sh """
aws ecr get-login-password --region ${region} | \
docker login --username AWS --password-stdin ${accountId}.dkr.ecr.${region}.amazonaws.com
"""
echo "Tagging Image..."
sh "docker tag ${imageName}:${tag} ${ecrRepo}:${tag}"
echo "Pushing Image..."
sh "docker push ${ecrRepo}:${tag}"
echo "Image pushed successfully: ${ecrRepo}:${tag}"
}
Commit.
Go to Jenkins:
Manage Jenkins
β Manage System
β Global Trusted Pipeline Libraries
β Add
Fill:
Name:
company-lib
Default Version:
main
SCM: Git
Repository URL: your shared library repo
Save.
Create new GitHub repo:
docker-demo-app
Create file:
Dockerfile
Paste:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
<h1>Jenkins Shared Library ECR Demo</h1>
@Library('company-lib') _
pipeline {
agent { label 'linux' }
environment {
AWS_REGION = "us-east-2"
IMAGE_NAME = "demo-app"
}
stages {
stage('Build and Push to ECR') {
steps {
buildAndPushECR(IMAGE_NAME, AWS_REGION)
}
}
}
}
Commit.
Jenkins β New Item
Name:
docker-ecr-pipeline
Type:
Pipeline
Pipeline script from SCM
SCM: Git
Repository URL: docker-demo-app repo
Branch: main
Script Path: Jenkinsfile
Save.
Click Build Now.
Watch console.
You should see:
Go to AWS Console β ECR β demo-app
You will see image tag:
1
If build number = 1
Platform Team:
Developers:
Only write:
buildAndPushECR("my-service", "us-east-2")
They donβt handle login or credentials.
Very important:
Cleanup example inside library:
sh "docker system prune -f"
If asked:
βHow do you standardize Docker builds in Jenkins?β
Answer:
"I create a centralized shared library that handles Docker build and ECR push logic using IAM role authentication. This ensures consistent tagging, secure credential handling, and reuse across multiple microservices."
That is senior DevOps answer.