forked from epam-msdp/cicd-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
104 lines (98 loc) · 3.45 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
pipeline {
agent any
environment {
DOCKER_CREDENTIALS_ID = 'ecff0b1f-c12c-4116-80da-251c97c5cae2'
IMAGE_NAME = 'epam-image'
}
stages {
stage('Checkout SCM') {
steps {
git(url: 'https://github.com/proxyvert/cicd-pipeline.git', branch: 'main')
}
}
stage('Application Build') {
agent {
docker {
image 'node:18'
reuseNode true
}
}
steps {
script {
try {
sh '''
echo "Starting application build..."
node --version
npm --version
chmod +x ./scripts/build.sh
./scripts/build.sh
'''
} catch (Exception e) {
error "Application build failed: ${e.getMessage()}"
}
}
}
}
stage('Tests') {
agent {
docker {
image 'node:18'
reuseNode true
}
}
steps {
script {
try {
sh '''
chmod +x ./scripts/test.sh
./scripts/test.sh
'''
} catch (Exception e) {
error "Tests failed: ${e.getMessage()}"
}
}
}
}
stage('Docker Image Build') {
steps {
script {
try {
withCredentials([usernamePassword(
credentialsId: DOCKER_CREDENTIALS_ID,
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
sh """
docker build -t \${DOCKER_USER}/${IMAGE_NAME}:${BUILD_NUMBER} .
docker tag \${DOCKER_USER}/${IMAGE_NAME}:${BUILD_NUMBER} \${DOCKER_USER}/${IMAGE_NAME}:latest
"""
}
} catch (Exception e) {
error "Docker image build failed: ${e.getMessage()}"
}
}
}
}
stage('Docker Image Push') {
steps {
script {
try {
withCredentials([usernamePassword(
credentialsId: DOCKER_CREDENTIALS_ID,
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
sh """
echo '$DOCKER_PASS' | docker login -u '$DOCKER_USER' --password-stdin
docker push \${DOCKER_USER}/${IMAGE_NAME}:${BUILD_NUMBER}
docker push \${DOCKER_USER}/${IMAGE_NAME}:latest
"""
}
} catch (Exception e) {
error "Docker image push failed: ${e.getMessage()}"
}
}
}
}
}
}