Create jenkinsfile

This commit is contained in:
bodduNaidu 2025-10-30 02:15:53 +05:30 committed by GitHub
parent 88a65bb374
commit 556c68ad29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

72
jenkinsfile Normal file
View file

@ -0,0 +1,72 @@
pipeline {
agent any
environment {
IMAGE_NAME = "yourdockerhub/flask"
DOCKER_CREDENTIALS_ID = "dockerhub-creds"
DEPLOY_USER = "ubuntu"
DEPLOY_SERVER = "YOUR_EC2_IP"
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/yourusername/flask.git'
}
}
stage('Build and Test') {
steps {
sh '''
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
pytest --maxfail=1 --disable-warnings -q
'''
}
}
stage('Docker Build') {
steps {
sh "docker build -t ${IMAGE_NAME}:${BUILD_NUMBER} ."
}
}
stage('Push to Docker Hub') {
steps {
withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIALS_ID}", usernameVariable: "USER", passwordVariable: "PASS")]) {
sh '''
echo "$PASS" | docker login -u "$USER" --password-stdin
docker push ${IMAGE_NAME}:${BUILD_NUMBER}
docker tag ${IMAGE_NAME}:${BUILD_NUMBER} ${IMAGE_NAME}:latest
docker push ${IMAGE_NAME}:latest
'''
}
}
}
stage('Deploy on EC2') {
steps {
sshagent(['ec2-ssh-key']) {
sh """
ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} '
docker pull ${IMAGE_NAME}:latest &&
docker stop flask || true &&
docker rm flask || true &&
docker run -d --name flask -p 80:5000 ${IMAGE_NAME}:latest
'
"""
}
}
}
}
post {
success {
echo '✅ Deployment successful!'
}
failure {
echo '❌ Build failed!'
}
}
}