pipeline { agent any environment { TRIVY_CRITICAL_THRES=333 NEXUS_REG_IP='10.182.0.16' NEXUS_REG_PORT=8082 REPO_NAME="appointme-admin-api" DOCK_IMG_NAME="${NEXUS_REG_IP}:${NEXUS_REG_PORT}/${REPO_NAME}:${BUILD_NUMBER}" // K8S_IMG_NAME needed by the SED command to replace a literal value in the template with a new image (with "slace" as part of it) // K8S_IMG_NAME="${NEXUS_REG_IP}:${NEXUS_REG_PORT}\\/${REPO_NAME}:${BUILD_NUMBER}" //The image deployed in DEV should have the tag updated to include the string DEV // ADD_DEV_TAG="${DOCK_IMG_NAME}"+"-DEV" DEV_TAGGED_IMGLST="$HOME/workspace/dev-param.lst" date_format = new Date().format('dd-MM-yy-HHmm') } stages { stage("Create Docker image") { steps { withCredentials([usernamePassword(credentialsId: 'nexus-secret', passwordVariable: 'pass', usernameVariable: 'username')]) { sh ''' sudo docker build -t ${DOCK_IMG_NAME} . sudo docker login -u ${username} -p ${pass} ${NEXUS_REG_IP}:${NEXUS_REG_PORT} sudo docker push ${DOCK_IMG_NAME} ''' } } } stage("Trivy Docker scan") { steps { sh ''' sudo trivy image ${DOCK_IMG_NAME} 2>/dev/null |grep "Total: [0-9]" for TH in `sudo trivy image ${DOCK_IMG_NAME} 2>/dev/null |grep "Total: [0-9]" |cut -d ":" -f7|cut -d ")" -f1` do if [ -z "${TH}" ] || [ $TH -gt ${TRIVY_CRITICAL_THRES} ] then echo "FAILED:Critical: "${TH} " has Crossed the Critical Threshold Limit of "${TRIVY_CRITICAL_THRES} !!!!! exit 1 fi done ''' } } stage("Deploy into Dev") { steps { echo "K8S_IMG_NAME=${K8S_IMG_NAME}" sh ''' sed 's//'${K8S_IMG_NAME}'/g' k8s-deploy-template.yml > k8-deploy.yaml kubectl apply -f k8-deploy.yaml -n dev # Add a check to see the APP is working ####### ''' withCredentials([usernamePassword(credentialsId: 'nexus-secret', passwordVariable: 'pass', usernameVariable: 'username')]) { sh ''' # Add a new tag to the deployed image in Nexus repository to mark it has been rolled out to Dev ######### sudo docker login -u ${username} -p ${pass} ${NEXUS_REG_IP}:${NEXUS_REG_PORT} sudo docker tag $DOCK_IMG_NAME ${ADD_DEV_TAG} sudo docker push ${ADD_DEV_TAG} echo "" echo "Images that are Deployed onto DEV......." echo "" curl http://${username}:${pass}@${NEXUS_REG_IP}:${NEXUS_REG_PORT}/v2/${REPO_NAME}/tags/list |sed 's/,/\\n/g'|sed 's/\\"//g'|grep "\\-DEV" > /tmp/dev-tagged.lst sed 's/^/'${REPO_NAME}':/g' /tmp/dev-tagged.lst > ${DEV_TAGGED_IMGLST} cat ${DEV_TAGGED_IMGLST} ''' } } } } }