Created
February 10, 2025 05:46
-
-
Save amitht007/925eb6cfded684f3a3808e24f5ee6a14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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