-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
101 lines (93 loc) · 3.35 KB
/
Copy pathJenkinsfile
File metadata and controls
101 lines (93 loc) · 3.35 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
pipeline {
agent { label 'nodejs-agent' }
environment {
DOCKER_IMAGE_TAG = "${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
DOCKER_IMAGE_TAG_LATEST = "${env.BRANCH_NAME}-latest"
}
stages {
stage('Install npm dependencies') {
steps {
cache(defaultBranch: 'dev',
maxCacheSize: 2048,
caches: [
arbitraryFileCache(
path: "node_modules",
includes: "**/*",
cacheValidityDecidingFile: "package-lock.json"
)
]) {
sh "npm install"
}
}
}
stage('Run automation tests') {
steps {
script {
def firstTestResult = sh(script: 'npm run test:ci', returnStatus: true)
if (firstTestResult != 0) {
def retryResult = sh(script: 'npm run test:ci-retry-failed', returnStatus: true)
if (retryResult != 0) {
error("Tests failed after retry")
}
}
}
}
post {
always {
recordCoverage(tools: [[parser: 'COBERTURA', pattern: '**/cobertura-coverage.xml']])
junit allowEmptyResults: true, checksName: 'Unit Tests', stdioRetention: 'FAILED', testResults: 'junit.xml'
}
}
}
stage('Build and Push Docker Image') {
agent { label 'docker-agent' }
when {
anyOf {
branch 'main'
branch 'dev'
branch 'prod'
}
}
steps {
withCredentials([string(credentialsId: 'alt-docker-image-name-prefix', variable: 'IMAGE_NAME_PREFIX')]) {
script {
def image = docker.build("${IMAGE_NAME_PREFIX}-api:${DOCKER_IMAGE_TAG}")
docker.withRegistry('https://index.docker.io/v1/', 'alt-dockerhub') {
image.push()
image.push("${DOCKER_IMAGE_TAG_LATEST}")
}
}
}
}
}
stage('Notify server') {
when {
anyOf {
branch 'main'
branch 'dev'
branch 'prod'
}
}
steps {
withCredentials([
string(credentialsId: 'alt-server-webhook-secret', variable: 'WEBHOOK_SECRET'),
string(credentialsId: 'alt-server-webhook-url', variable: 'WEBHOOK_URL')
]) {
script {
def payload = """{"name": "api", "tag": "${env.BRANCH_NAME}"}"""
withEnv(["PAYLOAD=${payload}"]) {
sh(script: '''
SIGNATURE=$(echo "$PAYLOAD" | openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" | sed 's/^.* //')
curl -s -o /dev/null -X POST "$WEBHOOK_URL" \
-H "Content-Type: application/json" \
-H "X-Hub-Signature: sha256=$SIGNATURE" \
-d "$PAYLOAD" \
--insecure
''', label: 'Notify server')
}
}
}
}
}
}
}