Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
pipeline {
agent {
label 'ava-machine'
}

environment {
// Docker Hub credentials ID (configured in Jenkins credentials)
DOCKER_HUB_CREDENTIALS = 'docker-hub-credentials'
// Docker Hub repository name
DOCKER_HUB_REPO = 'shabarisv48/jenkins-sample'
// Image tag (you can use build number or git commit)
IMAGE_TAG = "${BUILD_NUMBER}"
// GitHub repository URL
GIT_REPO = 'https://github.com/shabari48/sample-python-app'
}

stages {
stage('Clone Repository') {
steps {
script {
try {
echo "🔄 Cloning repository: ${GIT_REPO}"
// Clone the GitHub repository
git "${GIT_REPO}"
echo "✅ Repository cloned successfully"

// Show some info about the repo
sh 'ls -la'
sh 'git log --oneline -5'

// Check if Dockerfile exists
if (fileExists('Dockerfile')) {
echo "✅ Dockerfile found"
} else {
error "❌ Dockerfile not found in repository"
}
} catch (Exception e) {
error "❌ Failed to clone repository: ${e.getMessage()}"
}
}
}
}

stage('Build Docker Image') {
steps {
script {
try {
echo "🔄 Building Docker image: ${DOCKER_HUB_REPO}:${IMAGE_TAG}"

// Build Docker image
def dockerImage = docker.build("${DOCKER_HUB_REPO}:${IMAGE_TAG}")

// Optional: Also tag as latest
dockerImage.tag("latest")

echo "✅ Docker image built successfully"

// Show image details
sh "docker images ${DOCKER_HUB_REPO}"

} catch (Exception e) {
error "❌ Failed to build Docker image: ${e.getMessage()}"
}
}
}
}

stage('Test Docker Image') {
steps {
script {
try {
echo "🔄 Testing Docker image"

// Test if image runs successfully
// sh "docker run --rm ${DOCKER_HUB_REPO}:${IMAGE_TAG} python --version || echo 'Basic test completed'"

echo "✅ Docker image test passed"

} catch (Exception e) {
echo "⚠️ Docker image test failed: ${e.getMessage()}"
echo "Continuing with deployment..."
}
}
}
}

stage('Push to Docker Hub') {
steps {
script {
try {
echo "🔄 Pushing image to Docker Hub: ${DOCKER_HUB_REPO}"

// Login to Docker Hub and push image
docker.withRegistry('https://registry.hub.docker.com', "${DOCKER_HUB_CREDENTIALS}") {
def dockerImage = docker.image("${DOCKER_HUB_REPO}:${IMAGE_TAG}")
dockerImage.push()
dockerImage.push("latest")
}

echo "✅ Image pushed successfully to Docker Hub"
echo "📦 Image: ${DOCKER_HUB_REPO}:${IMAGE_TAG}"
echo "📦 Image: ${DOCKER_HUB_REPO}:latest"

} catch (Exception e) {
error "❌ Failed to push to Docker Hub: ${e.getMessage()}"
}
}
}
}

stage('Clean Up') {
steps {
script {
try {
echo "🔄 Cleaning up local images"

// Remove local images to save space
sh "docker rmi ${DOCKER_HUB_REPO}:${IMAGE_TAG} || true"
sh "docker rmi ${DOCKER_HUB_REPO}:latest || true"

// Optional: Clean up dangling images
sh "docker image prune -f || true"

echo "✅ Cleanup completed"

} catch (Exception e) {
echo "⚠️ Cleanup failed: ${e.getMessage()}"
}
}
}
}
}

post {
success {

echo """
🎉 PIPELINE SUCCESS!
✅ Repository cloned from: ${GIT_REPO}
✅ Docker image built: ${DOCKER_HUB_REPO}:${IMAGE_TAG}
✅ Image pushed to Docker Hub successfully
📦 Available tags: ${IMAGE_TAG}, latest

🔗 View on Docker Hub: https://hub.docker.com/r/shabarisv48/jenkins-sample
"""

emailext(
to: "[email protected]",
subject: "Build Success",
body: "The build was successful!"
)

}

failure {
echo """
❌ PIPELINE FAILED!
Check the console output above for detailed error messages.

Common issues:
- Missing Dockerfile in repository
- Docker Hub credentials not configured
- Network connectivity issues
- Invalid Docker Hub repository name
"""

emailext(
to: "[email protected]",
subject: "Build Failed",
body: "The build was failure!"
)

}
always {
// Clean workspace
cleanWs()
echo "🧹 Workspace cleaned"
}
}
}
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
# Install Instructions:
# pip3 install -r requirements.txt

flask==1.1.1
flask==2.3.3
jinja2==3.1.2
markupsafe==2.1.3