-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
39 lines (38 loc) · 1.81 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
pipeline {
agent any // Runs the pipeline on any available agent
stages {
stage('SCM Checkout') { // Stage to check out the source code from GitHub
steps {
retry(3) { // Retries the git checkout up to 3 times if it fails
git branch: 'main', url: 'https://github.com/SuranSandeepa/Docker-Jenkins-CI-CD-Automation.git' // Clones the 'main' branch from the given repository
}
}
}
stage('Build Docker Image') { // Stage to build the Docker image
steps {
bat 'docker build -t suransandeepa/nodeapp-cicd:%BUILD_NUMBER% .' // Builds a Docker image with a tag using the Jenkins build number
}
}
stage('Login to Docker Hub') { // Stage to log in to Docker Hub
steps {
// Retrieves Docker Hub credentials stored in Jenkins
withCredentials([string(credentialsId: 'zero-dockerhub-pass', variable: 'zero-dockerhubpass')]) {
script {
// Logs in to Docker Hub using the username and the credentials passed as an environment variable
bat "docker login -u suransandeepa -p %zero-dockerhubpass%"
}
}
}
}
stage('Push Image') { // Stage to push the Docker image to Docker Hub
steps {
bat 'docker push suransandeepa/nodeapp-cicd:%BUILD_NUMBER%' // Pushes the Docker image with the current Jenkins build number to Docker Hub
}
}
}
post {
always { // Actions to always run after the pipeline finishes, regardless of success or failure
bat 'docker logout' // Logs out from Docker Hub to ensure the credentials are cleared
}
}
}