-
Notifications
You must be signed in to change notification settings - Fork 15
/
Jenkinsfile
79 lines (75 loc) · 3.3 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
)
}
}
}