-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
67 lines (58 loc) · 2.23 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
60
61
62
63
64
65
66
67
pipeline {
agent {
label 'linux'
}
environment {
// Define environment variables
DOCKERHUB_ORG = 'fruster'
APP_NAME = 'fruster-user-service'
}
stages {
stage('Checkout Code') {
steps {
checkout scm // Checks out source code from the configured repository
}
}
stage('Build and run test container') {
steps {
script {
sh "docker build -f Dockerfile.test -t ${APP_NAME}-test ."
sh "docker run --network host --rm ${APP_NAME}-test"
}
}
}
stage('Build and Push Docker Image') {
when {
anyOf {
branch 'develop'
branch 'main'
branch 'master'
}
}
steps {
script {
def gitCommit = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
def branch = env.BRANCH_NAME
// Tag will be in the format: develop-20210803120000-abc123
def newTag = "${branch}-${new Date().format('yyyyMMddHHmmss')}-${gitCommit}"
// Latest branch tag will be in the format: develop-latest
def latestBranchTag = "${branch}-latest"
// Build and push Docker image
sh "docker build -t ${DOCKERHUB_ORG}/${APP_NAME}:${newTag} ."
sh "docker tag ${DOCKERHUB_ORG}/${APP_NAME}:${newTag} ${DOCKERHUB_ORG}/${APP_NAME}:${latestBranchTag}"
withCredentials([usernamePassword(credentialsId: 'docker-hub-credentials', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) {
sh "echo ${DOCKERHUB_PASS} | docker login --username ${DOCKERHUB_USER} --password-stdin"
}
sh "docker push ${DOCKERHUB_ORG}/${APP_NAME}:${newTag}"
sh "docker push ${DOCKERHUB_ORG}/${APP_NAME}:${latestBranchTag}"
}
}
}
}
post {
always {
// Cleanup tasks, such as archiving results, notifications, etc.
echo "Build completed"
}
}
}