From 2818496b7259b80d354905a9b2072d37d4c9cdcf Mon Sep 17 00:00:00 2001 From: RISHIRAJ MUKHERJEE <136720020+rishyym0927@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:12:19 +0530 Subject: [PATCH] Update Jenkinsfile Fixes #7 --- Jenkinsfile | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f51b1b6..09f1b87 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,14 +5,19 @@ pipeline { DOCKERHUB_CREDENTIALS = credentials('eb9c1cbf-8638-4a36-b866-dd6beb6471b0') BACKEND_IMAGE = 'sidharthsingh7/rusty_backend' DOCKER_TAG = 'latest' + DEPLOY_URL = 'http://ec2-3-7-69-234.ap-south-1.compute.amazonaws.com:3002/webhook' } stages { stage('Build') { steps { script { - // Build the backend Docker image - sh 'docker build -t $BACKEND_IMAGE:$DOCKER_TAG .' + try { + echo 'Building the Docker image...' + sh "docker build -t ${BACKEND_IMAGE}:${DOCKER_TAG} ." + } catch (Exception e) { + error "Docker build failed: ${e.message}" + } } } } @@ -20,20 +25,43 @@ pipeline { stage('Push') { steps { script { - docker.withRegistry('https://index.docker.io/v1/', 'eb9c1cbf-8638-4a36-b866-dd6beb6471b0') { - def backendImage = docker.image("$BACKEND_IMAGE:$DOCKER_TAG") - backendImage.push() + try { + echo 'Pushing Docker image to DockerHub...' + docker.withRegistry('https://index.docker.io/v1/', DOCKERHUB_CREDENTIALS) { + def backendImage = docker.image("${BACKEND_IMAGE}:${DOCKER_TAG}") + backendImage.push() + } + } catch (Exception e) { + error "Docker push failed: ${e.message}" } } } } - stage('Deploy') - { + + stage('Deploy') { steps { script { - sh 'curl -X POST "http://ec2-3-7-69-234.ap-south-1.compute.amazonaws.com:3002/webhook"' + try { + echo 'Triggering deployment...' + sh "curl -X POST '${DEPLOY_URL}'" + } catch (Exception e) { + error "Deployment failed: ${e.message}" + } } } } } + + post { + success { + echo 'Pipeline executed successfully!' + } + failure { + echo 'Pipeline execution failed. Please check the logs for details.' + } + always { + echo 'Cleaning up...' + sh 'docker system prune -f' + } + } }