How to create shared library lab

# automation# cicd# devops# tutorial
How to create shared library labAisalkyn Aidarova

🧠 LAB GOAL Create a Shared Library repository Add proper folder structure Configure it in...

🧠 LAB GOAL

  1. Create a Shared Library repository
  2. Add proper folder structure
  3. Configure it in Jenkins
  4. Create an application repo
  5. Use the library inside Jenkinsfile
  6. Run the pipeline

By the end, you will understand:

  • Who creates it
  • Who uses it
  • What DevOps controls
  • What happens internally

🟒 PART 1 β€” Create Shared Library Repository

Step 1 β€” Go to GitHub

Click New Repository

Repository name:

company-shared-lib
Enter fullscreen mode Exit fullscreen mode

Click Create.


Step 2 β€” Create Required Folder Structure

Inside that repository:

Click Add file β†’ Create new file

In the file name field write:

vars/buildApp.groovy
Enter fullscreen mode Exit fullscreen mode

This automatically creates the vars folder.


Step 3 β€” Write First Shared Function

Inside vars/buildApp.groovy, paste:

def call() {
    echo "Shared Library: Starting Build Stage"
    sh "echo Running build on $(hostname)"
}
Enter fullscreen mode Exit fullscreen mode

Click Commit.


Step 4 β€” Add Second Function

Click Add file β†’ Create new file

Name:

vars/deployApp.groovy
Enter fullscreen mode Exit fullscreen mode

Paste:

def call(String environment) {

    echo "Shared Library: Deploying to ${environment}"

    if (environment == "prod") {
        input "Approve Production Deployment?"
    }

    sh "echo Deployment to ${environment} completed"
}
Enter fullscreen mode Exit fullscreen mode

Click Commit.


βœ… Shared Library Repo Is Ready

Your repo should now look like:

company-shared-lib/
   └── vars/
        β”œβ”€β”€ buildApp.groovy
        └── deployApp.groovy
Enter fullscreen mode Exit fullscreen mode

Important:

  • vars folder name must be exact
  • File name becomes function name
  • def call() makes it callable like a function

🟒 PART 2 β€” Configure Shared Library in Jenkins

Now go to your Jenkins UI:

http://13.59.246.183:8080
Enter fullscreen mode Exit fullscreen mode

Step 1 β€” Go To:

Manage Jenkins
β†’ Manage System

Scroll down to:

Global Trusted Pipeline Libraries

Click Add

(We use Trusted because DevOps owns this library.)


Step 2 β€” Fill Configuration

Name:

company-lib
Enter fullscreen mode Exit fullscreen mode

Default version:

main
Enter fullscreen mode Exit fullscreen mode

Retrieval Method:

Modern SCM

SCM:

Git

Repository URL:

Paste your shared library GitHub URL

If private:
Add credentials.

Click Save.


βœ… Shared Library Is Now Connected

Jenkins now knows:

"Whenever someone writes @Library('company-lib'), load this repo."


🟒 PART 3 β€” Create Application Repository

Now create second GitHub repository.

Name:

sample-app
Enter fullscreen mode Exit fullscreen mode

Click Create.


Step 1 β€” Add Jenkinsfile

Inside sample-app:

Add file:

Jenkinsfile
Enter fullscreen mode Exit fullscreen mode

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")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Commit.


🟒 PART 4 β€” Create Pipeline Job in Jenkins

Go to Jenkins Dashboard.

Click:

New Item

Name:

sample-app-pipeline
Enter fullscreen mode Exit fullscreen mode

Select:

Pipeline

Click OK.


Step 1 β€” Configure SCM

Scroll to bottom.

Pipeline section:

Definition:

Pipeline script from SCM
Enter fullscreen mode Exit fullscreen mode

SCM:

Git
Enter fullscreen mode Exit fullscreen mode

Repository URL:
Paste sample-app GitHub URL

Branch:

main
Enter fullscreen mode Exit fullscreen mode

Script Path:

Jenkinsfile
Enter fullscreen mode Exit fullscreen mode

Click Save.


🟒 PART 5 β€” Run The Pipeline

Click:

Build Now

Watch Console Output.

You will see:

  • Shared Library loaded
  • Build stage executed
  • Dev deployment executed
  • Production stage waits for approval

Click Approve.

Build completes.


🧠 What Just Happened?

Step-by-step internally:

  1. Jenkins Controller loaded shared library repo
  2. It imported functions from vars
  3. It read Jenkinsfile
  4. It sent shell commands to linux agent
  5. Agent executed commands
  6. Controller saved logs

Controller = Brain
Agent = Worker


πŸ§‘β€πŸ’» Who Creates What in Real Company?

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.


πŸ” What DevOps Must Pay Attention To

Very important production topics:

  1. Version control shared library
  2. Protect Git branch
  3. Use PR approvals
  4. Never hardcode credentials
  5. Test library changes in dev Jenkins
  6. Monitor disk space on agents
  7. Pin production pipelines to specific library version

Example version pin:

@Library('company-lib@v1.0') _
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Final Architecture

EC2 (Controller):

  • Orchestrates
  • Stores history
  • Loads library

GitHub:

  • Shared library repo
  • App repo

Agents:

  • Execute build

Shared Library solves:

  • Duplicate pipeline logic
  • Standardization
  • Security control
  • Production safety
  • Centralized CI/CD

🧠 LAB GOAL

We will create:

Shared Library function:

buildAndPushECR(imageName, awsRegion)
Enter fullscreen mode Exit fullscreen mode

It will:

  1. Build Docker image
  2. Login to ECR
  3. Tag image
  4. Push to ECR

βš™οΈ PRE-REQUISITES (VERY IMPORTANT)

Before starting, make sure:

On Jenkins Linux Agent:

docker --version
aws --version
Enter fullscreen mode Exit fullscreen mode

If not installed:

sudo apt update
sudo apt install docker.io -y
sudo apt install awscli -y
sudo usermod -aG docker jenkins
Enter fullscreen mode Exit fullscreen mode

Restart agent if needed.


πŸ” IAM PERMISSION (BEST PRACTICE)

On EC2 Jenkins instance:

Attach IAM Role with permissions:

  • AmazonEC2ContainerRegistryFullAccess OR custom policy allowing:
ecr:GetAuthorizationToken
ecr:BatchCheckLayerAvailability
ecr:PutImage
ecr:InitiateLayerUpload
ecr:UploadLayerPart
ecr:CompleteLayerUpload
Enter fullscreen mode Exit fullscreen mode

Best practice: use IAM Role (not access keys).


🟒 PART 1 β€” Create ECR Repository

Go to AWS Console
β†’ ECR
β†’ Create Repository

Name:

demo-app
Enter fullscreen mode Exit fullscreen mode

Click Create.

Copy:

Repository URI

Example:

021399177326.dkr.ecr.us-east-2.amazonaws.com/demo-app
Enter fullscreen mode Exit fullscreen mode

Save this.


🟒 PART 2 β€” Create Shared Library Repo

Go to GitHub β†’ Create new repo:

company-shared-lib
Enter fullscreen mode Exit fullscreen mode

Step 1 β€” Create Folder

Create:

vars/buildAndPushECR.groovy
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Paste This Code

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}"
}
Enter fullscreen mode Exit fullscreen mode

Commit.


🟒 PART 3 β€” Configure Shared Library in Jenkins

Go to Jenkins:

Manage Jenkins
β†’ Manage System
β†’ Global Trusted Pipeline Libraries
β†’ Add

Fill:

Name:

company-lib
Enter fullscreen mode Exit fullscreen mode

Default Version:

main
Enter fullscreen mode Exit fullscreen mode

SCM: Git
Repository URL: your shared library repo

Save.


🟒 PART 4 β€” Create Application Repo

Create new GitHub repo:

docker-demo-app
Enter fullscreen mode Exit fullscreen mode

Step 1 β€” Add Dockerfile

Create file:

Dockerfile
Enter fullscreen mode Exit fullscreen mode

Paste:

FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Add index.html

<h1>Jenkins Shared Library ECR Demo</h1>
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Add Jenkinsfile

@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)
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Commit.


🟒 PART 5 β€” Create Jenkins Pipeline Job

Jenkins β†’ New Item

Name:

docker-ecr-pipeline
Enter fullscreen mode Exit fullscreen mode

Type:

Pipeline


Configure

Pipeline script from SCM
SCM: Git
Repository URL: docker-demo-app repo
Branch: main
Script Path: Jenkinsfile

Save.


▢️ RUN BUILD

Click Build Now.

Watch console.

You should see:

  • Docker build
  • ECR login
  • Image tag
  • Docker push

πŸ”Ž Verify in AWS

Go to AWS Console β†’ ECR β†’ demo-app

You will see image tag:

1
Enter fullscreen mode Exit fullscreen mode

If build number = 1


🧠 What Just Happened?

  1. Jenkins loaded shared library
  2. It executed Groovy function
  3. Agent built Docker image
  4. Agent authenticated using IAM role
  5. Agent pushed image to ECR
  6. Controller saved logs

🏒 Real Enterprise Architecture

Platform Team:

  • Writes shared library
  • Controls Docker logic
  • Controls tagging standard
  • Controls ECR login method
  • Controls security

Developers:

Only write:

buildAndPushECR("my-service", "us-east-2")
Enter fullscreen mode Exit fullscreen mode

They don’t handle login or credentials.


πŸ” What DevOps Must Pay Attention To

Very important:

  1. Never store AWS keys in Jenkinsfile
  2. Use IAM Role on EC2
  3. Protect shared library repo
  4. Version library
  5. Scan Docker image before push
  6. Clean old Docker images to save disk

Cleanup example inside library:

sh "docker system prune -f"
Enter fullscreen mode Exit fullscreen mode

🎯 Interview-Level Explanation

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.