|
|
|
|
@ -2,32 +2,81 @@ pipeline {
|
|
|
|
|
agent any
|
|
|
|
|
|
|
|
|
|
environment {
|
|
|
|
|
SERVER_IP = '192.168.1.82' // Replace with your server IP/hostname
|
|
|
|
|
SERVER_USER = 'aimo' // Replace with your server username
|
|
|
|
|
// Replace with your Docker Hub username/organization
|
|
|
|
|
DOCKER_HUB_USERNAME = 'aimodocker'
|
|
|
|
|
// Use credentials for Docker Hub
|
|
|
|
|
DOCKER_CREDENTIALS = credentials('aimodocker')
|
|
|
|
|
// Replace with your image name
|
|
|
|
|
IMAGE_NAME = 'oh-service'
|
|
|
|
|
// Variable for Git commit hash
|
|
|
|
|
GIT_COMMIT_HASH = ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stages {
|
|
|
|
|
stage('Test SSH Connection') {
|
|
|
|
|
stage('Checkout') {
|
|
|
|
|
steps {
|
|
|
|
|
sshagent(['backend-server-digitaltwin']) { // Use the credential ID you created
|
|
|
|
|
sh '''
|
|
|
|
|
# Print SSH key info
|
|
|
|
|
ssh-add -l
|
|
|
|
|
|
|
|
|
|
# Try verbose SSH connection
|
|
|
|
|
ssh -v -o StrictHostKeyChecking=no aimo@192.168.1.82 'echo test'
|
|
|
|
|
script {
|
|
|
|
|
// Checkout and get git commit hash
|
|
|
|
|
checkout scm
|
|
|
|
|
GIT_COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
|
|
|
|
echo "Git commit hash: ${GIT_COMMIT_HASH}"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stage('Docker Login') {
|
|
|
|
|
steps {
|
|
|
|
|
sh '''
|
|
|
|
|
echo ${DOCKER_CREDENTIALS_PSW} | docker login -u ${DOCKER_CREDENTIALS_USR} --password-stdin
|
|
|
|
|
'''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stage('Build Docker Image') {
|
|
|
|
|
steps {
|
|
|
|
|
script {
|
|
|
|
|
// Build with commit hash tag
|
|
|
|
|
sh '''
|
|
|
|
|
docker build -t ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH} .
|
|
|
|
|
docker tag ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH} ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
|
|
|
|
|
'''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stage('Push to Docker Hub') {
|
|
|
|
|
steps {
|
|
|
|
|
sh '''
|
|
|
|
|
# Push both tags
|
|
|
|
|
docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH}
|
|
|
|
|
docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
|
|
|
|
|
'''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post {
|
|
|
|
|
always {
|
|
|
|
|
// Clean up
|
|
|
|
|
sh 'docker logout'
|
|
|
|
|
|
|
|
|
|
// Clean up local images
|
|
|
|
|
script {
|
|
|
|
|
try {
|
|
|
|
|
sh '''
|
|
|
|
|
docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH}
|
|
|
|
|
docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
|
|
|
|
|
'''
|
|
|
|
|
} catch (err) {
|
|
|
|
|
echo "Failed to clean up images: ${err}"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
success {
|
|
|
|
|
echo 'SSH Connection test completed successfully!'
|
|
|
|
|
echo "Successfully built and pushed Docker image with tags: latest and ${GIT_COMMIT_HASH}"
|
|
|
|
|
}
|
|
|
|
|
failure {
|
|
|
|
|
echo 'SSH Connection test failed!'
|
|
|
|
|
echo 'Failed to build/push Docker image!'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|