-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
104 lines (92 loc) · 3.1 KB
/
Jenkinsfile
File metadata and controls
104 lines (92 loc) · 3.1 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
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
@Library('ak-libraries') _
pipeline {
agent any
parameters {
string(name: 'GIT_BRANCH', defaultValue: 'dev', description: 'Git branch to clone')
choice(name: 'DEPLOY_ENV', choices: ['dev', 'qa', 'prod'], description: 'Select deployment environment')
booleanParam(name: 'PUSH_TO_DOCKERHUB', defaultValue: true, description: 'Push image to Docker Hub?')
}
environment {
REPO_URL = 'https://github.com/ak-devops-anurag-org/simple-nodejs-server-with-dockerFile.git'
IMAGE_NAME = 'simple-node-img-jenkins'
IMAGE_TAG = 'latest'
DOCKER_USER = 'akdevanurag'
}
stages {
stage('Hello') {
steps {
echo "👋 Hi! - Deploying for environment: ${params.DEPLOY_ENV}"
}
}
stage('Clone Repo') {
steps {
withCredentials([string(credentialsId: 'repoUrl', variable: 'repoURL')]) {
script {
// cloneRepo(repoURL, params.GIT_BRANCH)
cloneRepo(env.REPO_URL, params.GIT_BRANCH)
}
}
}
}
stage('Who am I') {
steps {
echo "Current users"
sh 'whoami'
}
}
stage('Build Docker Image') {
steps {
script {
dockerBuild(env.IMAGE_NAME, env.IMAGE_TAG, env.DOCKER_USER)
}
}
}
stage('Push to Docker Hub') {
when {
expression { return params.PUSH_TO_DOCKERHUB }
}
steps {
echo '📦 Pushing image to Docker Hub...'
withCredentials([usernamePassword(
credentialsId: "dockerHubCreds",
usernameVariable: "dockerHubUsername",
passwordVariable: "dockerHubPass")]) {
sh '''
echo "$dockerHubPass" | docker login -u "$dockerHubUsername" --password-stdin
docker push ${dockerHubUsername}/${IMAGE_NAME}:${IMAGE_TAG}
'''
}
}
}
stage('Deploy Container') {
when {
anyOf {
expression { params.DEPLOY_ENV == 'dev' }
expression { params.DEPLOY_ENV == 'qa' }
}
}
steps {
echo "🚀 Deploying container for ${params.DEPLOY_ENV} environment"
sh 'docker compose up -d'
}
}
}
post {
always {
echo '📦 Cleaning workspace...'
cleanWs()
}
success {
echo '✅ Pipeline succeeded!'
// Clean old Docker images after successful run
sh '''
echo "🧹 Cleaning up old Docker images..."
docker image prune -f
docker container prune -f
'''
}
failure {
echo '❌ Pipeline failed! Check logs for details.'
}
}
}