Jenkins and AWS / shere lib
2 min read
In this document, I write how I implemented Jenkins server on an AWS EC2 server and used Shared Library.
Here is my Peoject on Shared Library Structure
After setting up Jenkins on my virtual machine, I created a Shared Library to manage all my common pipeline functions. This library allowed me to reuse code like cloning repositories, building Docker images, and pushing them to Docker Hub across multiple projects.
Shared Library Structure
In my GitHub repository for the shared library, I created the following folder structure:
(shared-library-root)
├── vars/
│ ├── clone.groovy
│ ├── dockerPush.groovy
│ ├── docker_build.groovy
│ └── hello.groovy
└── src/
└── (optional for advanced classes or utils)
Each file inside the vars directory represents a reusable function.
For example:
here is my repo - https://github.com/DheerenGaud/Jenkins-shared-libraires
clone.groovy → handles Git repository cloning
docker_build.groovy → builds Docker images
dockerPush.groovy → pushes images to Docker Hub
hello.groovy → test function to verify the library setup
Pipeline Using Shared Library
Here’s the Jenkins pipeline where I used my shared library:
here is my repo link - https://github.com/DheerenGaud/Jenkins-learning/tree/declarative_pipeline
@Library("Shared") _
pipeline {
agent { label "dev" }
stages {
stage("Clone") {
steps {
script {
clone("https://github.com/DheerenGaud/Jenkins-learning.git", "declarative_pipeline")
}
}
}
stage("Build") {
steps {
echo "Code Build Stage"
script {
docker_build("node_jenkins_app", "latest")
}
}
}
stage("Push to DockerHub") {
steps {
echo "Pushing to Docker Hub"
script {
dockerPush("node_jenkins_app", "dheerengaud/node_jenkins_app", "dockerCredentials")
}
}
}
stage("Deploy") {
steps {
sh "docker compose up -d"
echo "Deploy done ✅"
}
}
}
}

Explanation
@Library("Shared") → connects the pipeline to the global shared library configured in Jenkins.
clone(...) → uses my
clone.groovyscript from the library to fetch code from GitHub.docker_build(...) → builds a Docker image for the project.
dockerPush(...) → authenticates and pushes the built image to Docker Hub.
Deploy Stage → uses Docker Compose to bring up containers on the VM.
This setup made my pipeline much cleaner and reusable — I can now use the same functions in multiple Jenkinsfiles without repeating the code.
Here is Output of job -
