-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
78 lines (68 loc) · 2.65 KB
/
Copy pathJenkinsfile
File metadata and controls
78 lines (68 loc) · 2.65 KB
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
// This Jenkinsfile defines a CI/CD pipeline for a Node.js application using Docker on Windows.
pipeline {
agent any
// Define the stages for the pipeline
stages {
// Checkout the source code from the Git repository
stage('Checkout Source') {
steps {
git branch: 'main', url: 'https://github.com/PAIshanMadusha/node-docker-jenkins-cicd-pipeline.git'
}
}
// Install project dependencies
stage('Install Dependencies') {
steps {
bat 'npm install'
}
}
// Run tests
stage('Run Tests') {
steps {
bat 'npm test'
}
}
// Replace your Docker Hub username and password with these Jenkins credentials
// Build Docker image
stage('Build Docker Image') {
steps {
bat "docker build -t ishanmadusha/node-docker-jenkins-cicd-pipeline:%BUILD_NUMBER% ."
}
}
// Login to Docker Hub
stage('Login to Docker Hub') {
steps {
// Docker Hub credentials are stored in Jenkins credentials
withCredentials([string(credentialsId: 'text-dockerhub-password', variable: 'text-dockerhub-pass')]) {
bat "echo %text-dockerhub-pass% | docker login -u ishanmadusha --password-stdin"
}
}
}
// Push Docker image
stage('Push Docker Image') {
steps {
bat "docker push ishanmadusha/node-docker-jenkins-cicd-pipeline:%BUILD_NUMBER%"
// Optional: also tag as 'latest'
bat "docker tag ishanmadusha/node-docker-jenkins-cicd-pipeline:%BUILD_NUMBER% ishanmadusha/node-docker-jenkins-cicd-pipeline:latest"
bat "docker push ishanmadusha/node-docker-jenkins-cicd-pipeline:latest"
}
}
// Deploy the container
stage('Deploy Container') {
// Running the container with secret .env file
steps {
withCredentials([file(credentialsId: 'nodeapp-env-file', variable: 'ENV_FILE')]) {
// Stop old container (ignore errors if not running)
bat 'docker rm -f node-crud-app || exit 0'
// Run new container with secret .env file
bat "docker run -d --name node-crud-app --env-file %ENV_FILE% -p 3000:3000 ishanmadusha/node-docker-jenkins-cicd-pipeline:%BUILD_NUMBER%"
}
}
}
}
post {
always {
// Clean up Docker resources
bat 'docker logout'
}
}
}