Update 'Jenkinsfile'

main
CIzz22 3 weeks ago
parent 24ecc2f5f4
commit 67df6053af

125
Jenkinsfile vendored

@ -2,116 +2,95 @@ pipeline {
agent any
environment {
// 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
// This creates DOCKER_AUTH_USR and DOCKER_AUTH_PSW
DOCKER_AUTH = credentials('aimodocker')
IMAGE_NAME = 'oh-service'
// Replace with your docker compose service name
SERVICE_NAME = 'oh-app'
// Variable for Git commit hash
GIT_COMMIT_HASH = ''
SERVICE_NAME = 'ahm-app'
// Replace with the SSH credentials for development server
// SSH_CREDENTIALS = credentials('backend-server-digitaltwin')
// SSH_CREDENTIALS_USR = 'aimo'
// SSH_SERVER_IP = '192.168.1.82'
SECURITY_PREFIX = 'security'
// Initialize variables to be updated in script blocks
GIT_COMMIT_HASH = ""
IMAGE_TAG = ""
SECONDARY_TAG = ""
}
stages {
stage('Checkout') {
stage('Checkout & Setup') {
steps {
script {
// Checkout and get git commit hash
checkout scm
def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
GIT_COMMIT_HASH = commitHash
echo "Git commit hash: ${GIT_COMMIT_HASH}"
GIT_COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
// Use env.BRANCH_NAME or logic to handle detached HEAD if necessary
def branch = env.BRANCH_NAME ?: 'unknown'
echo "Current Branch: ${branch}"
if (branch == 'main') {
IMAGE_TAG = GIT_COMMIT_HASH
SECONDARY_TAG = 'latest'
} else if (branch == 'oh_security') {
IMAGE_TAG = "${SECURITY_PREFIX}-${GIT_COMMIT_HASH}"
SECONDARY_TAG = "${SECURITY_PREFIX}-latest"
} else {
IMAGE_TAG = "temp-${GIT_COMMIT_HASH}"
SECONDARY_TAG = "" // Ensure it's empty for other branches
}
echo "Primary Tag: ${IMAGE_TAG}"
}
}
}
stage('Docker Login') {
steps {
sh '''
echo ${DOCKER_CREDENTIALS_PSW} | docker login -u ${DOCKER_CREDENTIALS_USR} --password-stdin
'''
// Fixed variable names based on the 'DOCKER_AUTH' environment key
sh "echo ${DOCKER_AUTH_PSW} | docker login -u ${DOCKER_AUTH_USR} --password-stdin"
}
}
stage('Build Docker Image') {
stage('Build & Tag') {
steps {
script {
// Build with commit hash tag
sh """
docker build -t ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest .
docker tag ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH}
"""
def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}"
sh "docker build -t ${fullImageName}:${IMAGE_TAG} ."
if (SECONDARY_TAG) {
sh "docker tag ${fullImageName}:${IMAGE_TAG} ${fullImageName}:${SECONDARY_TAG}"
}
}
}
}
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
"""
script {
def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}"
sh "docker push ${fullImageName}:${IMAGE_TAG}"
if (SECONDARY_TAG) {
sh "docker push ${fullImageName}:${SECONDARY_TAG}"
}
}
}
}
// stage('Watchtower Deployment') {
// steps {
// sh """
// # Push both tags
// docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH}
// docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
// """
// }
// }
// stage('Deploy') {
// steps {
// script {
// sshagent(credentials: ['backend-server-digitaltwin']) {
// sh """
// ssh -o StrictHostKeyChecking=no -p 12558 aimo@0.tcp.ap.ngrok.io '
// cd ~/digital-twin/Docker
// sudo docker compose pull ${SERVICE_NAME}
// sudo docker compose up -d ${SERVICE_NAME}
// '
// """
// }
// }
// }
// }
}
post {
always {
// Clean up
sh 'docker logout'
// Clean up local images
script {
try {
sh """
# Push both tags
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}"
sh 'docker logout'
def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}"
// Clean up images to save agent disk space
sh "docker rmi ${fullImageName}:${IMAGE_TAG} || true"
if (SECONDARY_TAG) {
sh "docker rmi ${fullImageName}:${SECONDARY_TAG} || true"
}
}
}
success {
echo "Successfully built, pushed, and deployed Docker image with tags: latest and ${GIT_COMMIT_HASH}"
}
failure {
echo 'Failed to build/push/deploy Docker image!'
echo "Successfully processed ${env.BRANCH_NAME}."
}
}
}
Loading…
Cancel
Save