forked from Michkov/node-express-hello
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJenkinsfile
More file actions
64 lines (63 loc) · 2.42 KB
/
Copy pathJenkinsfile
File metadata and controls
64 lines (63 loc) · 2.42 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
pipeline {
agent any
parameters {
// Define environment variables here
string(name: 'BRANCH', description: 'Branch to build')
string(name: 'GIT_URL', description: 'Git repository URL')
string(name: 'DOCKER_REGISTRY', description: 'Docker registry URL')
string(name: 'DOCKER_IMAGE', description: 'Docker image name')
string(name: 'CONTAINER_NAME', description: 'Docker container name')
//credentials(name: 'DOCKER_CREDENTIALS', description: 'Credentials for Docker registry')
}
stages {
stage('Clone Repository') {
steps {
script {
// Clone the repository
echo "Cloning repository from ${GIT_URL} on branch ${BRANCH}"
git branch: "${BRANCH}", url: "${GIT_URL}"
}
}
}
stage('Build and Tag Docker Image') {
steps {
script {
// Build the Docker image
echo "Building Docker image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
sh "docker build -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE} ."
}
}
}
stage('Push Docker Image') {
steps {
script {
// Push the Docker image to the registry
echo "Pushing Docker image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
//sh docker login ${DOCKER_REGISTRY} --username admin --password admin
sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
}
}
}
stage('Deploy and Test Docker Image'){
steps{
script{
echo "Running Docker image locally to verify"
sh "docker pull ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
sh "docker stop ${params.CONTAINER_NAME} || true"
sh "docker rm ${params.CONTAINER_NAME} || true"
sh "docker run -d -p 3000:3000 --name ${params.CONTAINER_NAME} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
sh "docker ps"
}
}
}
}
// Post actions
post {
always {
echo 'Pipeline execution completed.'
}
failure {
echo 'Pipeline execution failed!'
}
}
}