Create jenkinsfile
This commit is contained in:
parent
88a65bb374
commit
556c68ad29
1 changed files with 72 additions and 0 deletions
72
jenkinsfile
Normal file
72
jenkinsfile
Normal 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!'
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue