Skip to content

Commit

Permalink
Create Jenkinsfile
Browse files Browse the repository at this point in the history
added  Jenkinsfile to project
  • Loading branch information
abbos1711 authored Feb 6, 2024
1 parent e6ac608 commit 3b9dfbe
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pipeline {
agent any

environment {
DISCORD_WEBHOOK = credentials('discord-webhook')
GIT_URL = 'https://github.com/abbos1711/angular-project-ci-cd-gitlab-and-githubAction.git'

DOCKERHUB_CREDENTIALS = credentials('dockerhub')
CONTAINER_NAME = 'angular'
REGISTRY_URL = '10449'
SERVER_USERNAME = credentials('server-username')
SERVER_IP = credentials('server-ip')
SSH_CREDENTIALS = credentials('server-ssh')


GIT_TOKEN = credentials('git-token')
BRANCH_NAME = 'origin'
// BRANCH_NAME = 'main'
}
stages {
stage('Clean Workspace') {
steps {
cleanWs()
}
}
stage('Clone Repository') {
steps {
git branch: BRANCH_NAME, url: GIT_URL, credentialsId: 'git-token'
}
}
stage('Build Application') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
script {
def dockerlogin = "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
sh dockerlogin
sh """
docker build . -t ${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER} -f Dockerfile
docker tag ${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER} ${REGISTRY_URL}/${CONTAINER_NAME}:latest
docker push ${REGISTRY_URL}/${CONTAINER_NAME}:latest
docker push ${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER}
docker image rm -f ${REGISTRY_URL}/${CONTAINER_NAME}:latest
docker image rm -f ${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER}
"""
}
}
}
}
stage('Deploy Server') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
script {
sshagent (credentials: ['server-ssh']) {
sh """
ssh -o StrictHostKeyChecking=no ${SERVER_USERNAME}@${SERVER_IP} '\
docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} && \
docker pull ${REGISTRY_URL}/${CONTAINER_NAME}:latest && \
docker stop ${CONTAINER_NAME} || true && \
docker rm ${CONTAINER_NAME} || true && \
docker run -d -p 5200:5200 --name ${CONTAINER_NAME} --restart always ${REGISTRY_URL}/${CONTAINER_NAME}:latest '
"""
}
}
}
}
}
}
post {
always {
discordSend(
description: "Jenkins Pipeline Build ${currentBuild.currentResult}",
link: env.GIT_URL,
result: currentBuild.currentResult,
title: JOB_NAME,
webhookURL: env.DISCORD_WEBHOOK
)
}
}
}

0 comments on commit 3b9dfbe

Please sign in to comment.