-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
59 lines (52 loc) · 1.61 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
pipeline {
agent any
environment {
DOCKER_REGISTRY = "olmarsh"
DOCKER_IMAGE = "${DOCKER_REGISTRY}/blog-app:latest"
}
stages {
stage('Install Dependencies') {
steps {
script {
// Install node dependencies
sh 'npm install'
}
}
}
stage('Build Docker Image') {
steps {
script {
// Build the Docker image
sh "docker build -t ${DOCKER_IMAGE} ."
}
}
}
stage('Push Docker Image') {
steps {
script {
// Use withCredentials block to securely pass Docker credentials
withCredentials([usernamePassword(credentialsId: 'dockerHub', usernameVariable: "DOCKER_USERNAME", passwordVariable: "DOCKER_PASSWORD")]) {
// Login to DockerHub
sh 'echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin'
// Push the Docker image to the registry
sh "docker push ${DOCKER_IMAGE}"
}
}
}
}
stage('Cleanup') {
steps {
script {
// Cleanup dangling Docker images
sh "docker rmi ${DOCKER_IMAGE}"
}
}
}
}
post {
always {
// Archive artifacts or clean up workspace if needed
cleanWs()
}
}
}