Skip to content

Instantly share code, notes, and snippets.

@amitht007
Created February 10, 2025 05:46
Show Gist options
  • Save amitht007/925eb6cfded684f3a3808e24f5ee6a14 to your computer and use it in GitHub Desktop.
Save amitht007/925eb6cfded684f3a3808e24f5ee6a14 to your computer and use it in GitHub Desktop.
pipeline {
agent any
environment {
REGISTRY = "123knight"
GITHUB_CREDENTIALS = credentials('github-credentials')
DOCKERHUB_CREDENTIALS = credentials('dockerhub-credentials')
}
stages {
stage('Clone Repository') {
steps {
script {
try {
echo "πŸ”Ή Cloning repository..."
git branch: 'main', url: 'https://github.com/amitht007/dock.git', credentialsId: 'github-credentials'
echo "βœ… Repository cloned!"
} catch (Exception e) {
echo "❌ ERROR: Failed to clone repository!"
error("Stopping pipeline.")
}
}
}
}
stage('Build & Push Docker Images') {
steps {
script {
try {
echo "πŸ”Ή Building Docker images..."
sh 'docker build -t $REGISTRY/frontend:latest ./client'
echo "πŸ”Ή Pushing images to Docker Hub..."
withDockerRegistry([credentialsId: 'dockerhub-credentials', url: '']) {
sh 'docker push $REGISTRY/frontend:latest'
}
echo "βœ… Images pushed successfully!"
} catch (Exception e) {
echo "❌ ERROR: Docker build/push failed!"
error("Stopping pipeline.")
}
}
}
}
stage('Provision EC2 Instance') {
steps {
withCredentials([aws(credentialsId: 'aws-credentials')]) {
sh '''
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
echo "πŸš€ Starting EC2 provisioning..."
ansible-playbook -i localhost, ansible/provision_ec2.yml || { echo "❌ ERROR: EC2 provisioning failed!"; exit 1; }
echo "βœ… EC2 provisioning complete. Checking inventory..."
cat ansible/inventory.ini
'''
}
}
}
stage('Deploy Application with Ansible') {
steps {
script {
try {
echo "πŸš€ Deploying application using Ansible..."
sh '''
ansible-playbook -i ansible/inventory.ini ansible/deploy.yml || { echo "❌ ERROR: Deployment failed!"; exit 1; }
'''
echo "βœ… Deployment successful!"
} catch (Exception e) {
echo "❌ ERROR: Ansible deployment failed!"
error("Stopping pipeline.")
}
}
}
}
}
post {
success {
echo "πŸŽ‰ Deployment complete! Your app is live!"
}
failure {
echo "❌ Deployment failed. Check logs for errors."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment