-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Jenkinsfile to project
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |